]> git.immae.eu Git - perso/Immae/Config/Nix.git/blobdiff - fetch_version
multiple improvements
[perso/Immae/Config/Nix.git] / fetch_version
index db0af1b813a3c6e17e320e29b5072d663f31478c..cbbeb64468ea2e7fad064cffce9480e190593705 100755 (executable)
@@ -2,7 +2,7 @@
 
 usage() {
   echo "$0 file.json"
-  echo "$0 [-n|--name name] [-b|--branch branch] [-h|--help] (-u|--url) url"
+  echo "$0 [-n|--name name] [-b|--branch branch] [-f|--file out_file] [-h|--help] (-u|--url) url"
   exit
 }
 
@@ -17,6 +17,7 @@ while [[ $# -gt 0 ]]; do
       name="$(echo "$content" | jq -r ".meta.name")"
       url="$(echo "$content" | jq -r ".meta.url")"
       branch="$(echo "$content" | jq -r ".meta.branch")"
+      break
       ;;
     -n|--name)
       name=$1
@@ -30,6 +31,10 @@ while [[ $# -gt 0 ]]; do
       branch=$1
       shift
       ;;
+    -f|--file)
+      file=$1
+      shift
+      ;;
     -h|--help)
       usage
       ;;
@@ -75,26 +80,52 @@ function fetch_github () {
   tag=${rev:0:7}-$branch
 }
 
-fetch_github 2>/dev/null
+function fetch_other () {
+  rev="$(git ls-remote --refs $url refs/heads/$branch | head -n1 | cut -f1)"
+  sha="$(nix-prefetch-git --url $url --rev refs/heads/$branch | jq -r '.sha256')"
+  tag=${rev:0:7}-$branch
+}
 
-owner=$(echo "$url" | cut -d"/" -f4)
-repo=$(echo "$url" | cut -d"/" -f5)
+case "$url" in
+  https://*github.com/*)
+    fetch_github 2>/dev/null
+    owner=$(echo "$url" | cut -d"/" -f4)
+    repo=$(echo "$url" | cut -d"/" -f5)
 
-F='{
-  "tag": $tag,
-  "meta": {
-    "name": $name,
-    "url": $url,
-    "branch": $branch
-  },
-  "github": {
-    "owner": $owner,
-    "repo": $repo,
-    "rev": $rev,
-    "sha256": $sha,
-    "fetchSubmodules": true
-  }
-}'
+    F='{
+      "tag": $tag,
+      "meta": {
+        "name": $name,
+        "url": $url,
+        "branch": $branch
+      },
+      "github": {
+        "owner": $owner,
+        "repo": $repo,
+        "rev": $rev,
+        "sha256": $sha,
+        "fetchSubmodules": true
+      }
+    }'
+    ;;
+  *)
+    fetch_other 2>/dev/null
+    F='{
+      "tag": $tag,
+      "meta": {
+        "name": $name,
+        "url": $url,
+        "branch": $branch
+      },
+      "git": {
+        "url": $url,
+        "rev": $rev,
+        "sha256": $sha,
+        "fetchSubmodules": true
+      }
+    }'
+    ;;
+esac
 
 jq -n \
   --arg name "$name" \