]> git.immae.eu Git - perso/Immae/Config/Nix/NUR.git/blob - scripts/fetch_version
Add monitoring host
[perso/Immae/Config/Nix/NUR.git] / scripts / fetch_version
1 #!/bin/bash
2
3 usage() {
4 echo "$0 file.json"
5 echo "$0 [-n|--name name] [-b|--branch branch_or_rev] [-f|--file out_file] [-h|--help] (-u|--url) url"
6 exit
7 }
8
9 branch="master"
10 while [[ $# -gt 0 ]]; do
11 a="$1"
12 shift
13 case "$a" in
14 *.json)
15 file=$a
16 content=$(cat $a)
17 name="$(echo "$content" | jq -r ".meta.name")"
18 url="$(echo "$content" | jq -r ".meta.url")"
19 branch="$(echo "$content" | jq -r ".meta.branch")"
20 ;;
21 -n|--name)
22 name=$1
23 shift
24 ;;
25 -u|--url)
26 url=$1
27 shift
28 ;;
29 -b|--branch)
30 branch=$1
31 shift
32 ;;
33 -f|--file)
34 file=$1
35 shift
36 ;;
37 -h|--help)
38 usage
39 ;;
40 esac
41 done
42 if [ -z "$url" ]; then
43 usage
44 fi
45 if [ -z "$name" ]; then
46 name=$(echo "$url" | cut -d"/" -f5)
47 fi
48 if [ -z "$file" ]; then
49 file=$name.json
50 fi
51
52 # function fetch_ledger () {
53 # pushd $HOME/projets/ledger >/dev/null 2>/dev/null
54 # git fetch origin
55 # tag="$(git describe origin/next | sed -e "s/^v//")"
56 # rev="$(git show-ref -s refs/remotes/origin/next)"
57 # sha="$(nix-prefetch-url --unpack file://<(git archive --format=tar.gz HEAD) 2>/dev/null)"
58 # popd >/dev/null 2>/dev/null
59 # }
60
61 # awk_describe='BEGIN {
62 # FS = "[ /^]+"
63 # while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) {
64 # if (!sha)
65 # sha = substr($0, 1, 7)
66 # tag = $3
67 # }
68 # while ("curl -s " ARGV[1] "/releases/tag/" tag | getline)
69 # if ($3 ~ "commits")
70 # com = $2
71 # printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha
72 # }'
73
74 function get_ref () {
75 case "$1" in
76 refs/*)
77 echo "$1"
78 ;;
79 *)
80 echo "refs/heads/$1"
81 ;;
82 esac
83 }
84
85 function get_name () {
86 branch="$1"
87 rev="$2"
88 minirev=${rev:0:7}
89
90 case "$branch" in
91 refs/tags/*)
92 b="${branch#refs/tags/}"
93 echo "${b//\//-}"
94 ;;
95 refs/heads/*)
96 b=${branch#refs/heads/}
97 echo "$minirev-${b//\//-}"
98 ;;
99 refs/*)
100 b=${branch#refs/}
101 echo "$minirev-${b//\//-}"
102 ;;
103 *)
104 echo "$minirev-${branch//\//-}"
105 ;;
106 esac
107 }
108
109 function fetch_github () {
110 rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
111 sha="$(nix-prefetch-url --unpack $url/archive/$rev.tar.gz)"
112 # Différent du git-describe et github-spécifique
113 #tag=$(echo "$awk_describe" | awk -f - $url | sed -e "s/^v//")
114 tag=$(get_name $branch $rev)
115 }
116
117 function fetch_other () {
118 rev="$(git ls-remote --refs $url $(get_ref $branch) | head -n1 | cut -f1)"
119 sha="$(nix-prefetch-git --url $url --rev $(get_ref $branch) | jq -r '.sha256')"
120 tag=$(get_name $branch $rev)
121 }
122
123 case "$url" in
124 https://*github.com/*)
125 fetch_github 2>/dev/null
126 owner=$(echo "$url" | cut -d"/" -f4)
127 repo=$(echo "$url" | cut -d"/" -f5)
128
129 F='{
130 "tag": $tag,
131 "meta": {
132 "name": $name,
133 "url": $url,
134 "branch": $branch
135 },
136 "github": {
137 "owner": $owner,
138 "repo": $repo,
139 "rev": $rev,
140 "sha256": $sha,
141 "fetchSubmodules": true
142 }
143 }'
144 ;;
145 *)
146 fetch_other 2>/dev/null
147 F='{
148 "tag": $tag,
149 "meta": {
150 "name": $name,
151 "url": $url,
152 "branch": $branch
153 },
154 "git": {
155 "url": $url,
156 "rev": $rev,
157 "sha256": $sha,
158 "fetchSubmodules": true
159 }
160 }'
161 ;;
162 esac
163
164 jq -n \
165 --arg name "$name" \
166 --arg owner "$owner" \
167 --arg repo "$repo" \
168 --arg tag "$tag" \
169 --arg rev "$rev" \
170 --arg url "$url" \
171 --arg branch "$branch" \
172 --arg sha "$sha" \
173 "$F" > $file