-
Notifications
You must be signed in to change notification settings - Fork 3
/
y-bin-download
executable file
·189 lines (167 loc) · 3.76 KB
/
y-bin-download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env bash
[ -z "$DEBUGDEBUG" ] || set -x
set -e
#set -eo pipefail
[ "$1" = "help" ] && echo '
Manages binaries through the following modes:
y-bin-download y-bin.optional.yaml # downloads missing and those with a new version
y-bin-download y-bin.optional.yaml [name] # downloads the named binary, if present as key in y-bin.optional.yaml
y-bin-dependency-download # used in old y-[bin] scripts, deprecated but still working, can be replaced by the previous example
And yes, we have been looking for an open source impl of this need :)
' && exit 0
[ -z "$YSTACK_HOME" ] && echo "The YSTACK_HOME variable is required" && exit 1
BINYAML=$1
binpath() {
echo "$YSTACK_HOME/bin"
}
# the one part of this script that modifies the host system
install() {
name=$1
version=$2
url=$3
sha256=$4
archivetool=$5
archivepath=$6
# translate to legacy format's variable env names
archivecompression=z
[ "$archivetool" != "tarxz" ] || archivecompression=J
[ "$archivetool" != "tarxz" ] || archivetool=tar
[ "$archivetool" != "tar" ] || bin_tar_path=$archivepath
[ "$archivetool" != "zip" ] || bin_zip_path=$archivepath
# the legacy format, reused until phased out, note the mandatory v prefix to version
bin_name=$name \
bin_version=v${version} \
Darwin_url=$url \
Darwin_sha256=$sha256 \
Linux_url=$url \
Linux_sha256=$sha256 \
bin_zip_path=$bin_zip_path \
bin_tar_path=$bin_tar_path \
bin_tar_compression=$archivecompression \
y-bin-dependency-download 1>&2 || exit $?
echo $version
}
gotemplate() {
tpl=$1
key=$2
value=$3
echo $tpl | sed "s|{{ \.$key }}|$value|g"
}
os() {
uname -s | tr '[:upper:]' '[:lower:]'
}
osx() {
os=$(os)
case $os in
darwin) echo 'osx' ;;
*) echo "$os" ;;
esac
}
osmac() {
os=$(os)
case $os in
darwin) echo 'macos' ;;
*) echo "$os" ;;
esac
}
osmac_() {
os=$(os)
case $os in
darwin) echo 'macos-' ;;
*) echo "" ;;
esac
}
Os() {
uname -s
}
xarch() {
x=$(uname -m)
case $x in
aarch64) echo 'arm64' ;;
*) echo $x ;;
esac
}
arch() {
x=$(xarch)
case $x in
x86_64) echo 'amd64' ;;
*) echo $x ;;
esac
}
aarch() {
x=$(uname -m)
case $x in
arm64) echo 'aarch64' ;;
*) echo $x ;;
esac
}
aarch64() {
x=$(aarch)
case $x in
x86_64) echo 'x64' ;;
*) echo $x ;;
esac
}
arm() {
x=$(xarch)
case $x in
arm64) echo 'arm' ;;
*) echo '' ;;
esac
}
xarm() {
x=$(xarch)
case $x in
arm64) echo 'arm' ;;
*) echo 'x' ;;
esac
}
names() {
yaml=$1
[ -f "$yaml" ] || { >&2 echo "bin yaml not found at path $yaml"; return 1; }
cat $yaml | grep -v '^$' | grep -v '#' | grep -v '^ ' | cut -d: -f1
}
# should extract yaml into something that's safe to eval
to_eval() {
yaml=$1
name=$2
[ -f "$yaml" ] || { >&2 echo "bin yaml not found at path $yaml">2; return 1; }
awk "/^$name:/{f=1;next} /^[a-z0-9_-]+/{f=0} f" $yaml | \
sed 's| *version: *|version=|' | \
sed 's| *tool: *|archivetool=|' | \
sed 's| *path: *|archivepath=|' | \
sed 's| *download: *\(.*\)|url="\1"|' | \
sed "s| *$(os)_$(arch): *|sha256=|" | \
grep -E '^[[:alnum:]]+='
}
run() {
yaml=$1
name=$2
os=$(os)
aarch=$(aarch)
aarch64=$(aarch64)
arch=$(arch)
xarch=$(xarch)
osx=$(osx)
osmac=$(osmac)
Os=$(Os)
arm=$(arm)
xarm=$(xarm)
eval $(to_eval $yaml $name)
install "$name" "$version" "$url" "$sha256" "$archivetool" "$archivepath"
}
# https://github.com/shellspec/shellspec#testing-shell-functions
[ "${__SOURCED__:+x}" ] && return 0
[ $# -eq 0 ] && >&2 echo "First arg must be an y-bin yaml path" && exit 1
yaml=$1
stat $yaml >/dev/null
if [ $# -eq 1 ]; then
for name in $(names $1); do
run $yaml $name
done
else
while [ $# -gt 1 ]; do
run $yaml $2
shift
done
fi