-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
222 lines (195 loc) · 4.92 KB
/
Copy pathinstall.sh
File metadata and controls
222 lines (195 loc) · 4.92 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env bash
set -euo pipefail
repo="Jcardif/azimg-cli"
version="latest"
install_dir="$HOME/.local/bin"
force="false"
dry_run="false"
usage() {
cat <<'USAGE'
Usage: install.sh [options]
Options:
--version <version> Release tag or version. Default: latest.
--install-dir <path> Install directory. Default: $HOME/.local/bin.
--force Overwrite an existing azimg executable.
--dry-run Print what would happen without changing files.
-h, --help Show this help.
USAGE
}
require_value() {
option="$1"
if [ "$#" -lt 2 ] || [ -z "$2" ]; then
echo "$option requires a value." >&2
usage >&2
exit 1
fi
case "$2" in
-*)
echo "$option requires a value." >&2
usage >&2
exit 1
;;
esac
}
while [ "$#" -gt 0 ]; do
case "$1" in
--version)
require_value "$1" "${2:-}"
version="$2"
shift 2
;;
--install-dir)
require_value "$1" "${2:-}"
install_dir="$2"
shift 2
;;
--force)
force="true"
shift
;;
--dry-run)
dry_run="true"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 1
;;
esac
done
detect_rid() {
os_name="$(uname -s)"
arch_name="$(uname -m)"
case "$os_name:$arch_name" in
Darwin:arm64|Darwin:aarch64)
echo "osx-arm64"
;;
Linux:x86_64|Linux:amd64)
echo "linux-x64"
;;
*)
echo "Unsupported platform: $os_name $arch_name" >&2
exit 1
;;
esac
}
download_file() {
url="$1"
output="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL "$url" -o "$output"
elif command -v wget >/dev/null 2>&1; then
wget -q "$url" -O "$output"
else
echo "Install requires curl or wget." >&2
exit 1
fi
}
sha256_file() {
file="$1"
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$file" | awk '{ print $1 }'
else
sha256sum "$file" | awk '{ print $1 }'
fi
}
json_escape() {
printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'
}
rid="$(detect_rid)"
asset="azimg-$rid.tar.gz"
if [ "$version" = "latest" ]; then
release_base="https://github.com/$repo/releases/latest/download"
release_label="latest"
else
case "$version" in
v*) tag="$version" ;;
*) tag="v$version" ;;
esac
release_base="https://github.com/$repo/releases/download/$tag"
release_label="$tag"
fi
target="$install_dir/azimg"
metadata_dir="$HOME/.azimg"
metadata_path="$metadata_dir/metadata.json"
echo "azimg installer"
echo " release: $release_label"
echo " rid: $rid"
echo " target: $target"
if [ "$dry_run" = "true" ]; then
exit 0
fi
if [ -e "$target" ] && [ "$force" != "true" ]; then
echo "azimg already exists at $target. Re-run with --force to overwrite it." >&2
exit 3
fi
tmp_root="$(mktemp -d "${TMPDIR:-/tmp}/azimg-install.XXXXXX")"
trap 'rm -rf "$tmp_root"' EXIT
archive_path="$tmp_root/$asset"
sums_path="$tmp_root/SHA256SUMS"
manifest_path="$tmp_root/azimg-release.json"
extract_dir="$tmp_root/extract"
download_file "$release_base/$asset" "$archive_path"
download_file "$release_base/SHA256SUMS" "$sums_path"
download_file "$release_base/azimg-release.json" "$manifest_path"
installed_version="$(sed -n 's/.*"version"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$manifest_path" | head -n 1)"
if [ -z "$installed_version" ]; then
installed_version="$release_label"
fi
expected_hash="$(grep " $asset$" "$sums_path" | awk '{ print $1 }' | head -n 1)"
if [ -z "$expected_hash" ]; then
echo "Could not find $asset in SHA256SUMS." >&2
exit 1
fi
actual_hash="$(sha256_file "$archive_path")"
if [ "$expected_hash" != "$actual_hash" ]; then
echo "Checksum mismatch for $asset." >&2
echo "Expected: $expected_hash" >&2
echo "Actual: $actual_hash" >&2
exit 1
fi
mkdir -p "$extract_dir" "$install_dir" "$metadata_dir"
tar -xzf "$archive_path" -C "$extract_dir"
source_exe="$(find "$extract_dir" -type f -name azimg -print -quit)"
if [ -z "$source_exe" ]; then
echo "The archive did not contain azimg." >&2
exit 1
fi
cp "$source_exe" "$target"
chmod +x "$target"
escaped_target="$(json_escape "$target")"
escaped_rid="$(json_escape "$rid")"
escaped_version="$(json_escape "$installed_version")"
escaped_repo="$(json_escape "$repo")"
cat > "$metadata_path" <<EOF
{
"schemaVersion": 1,
"install": {
"schemaVersion": 1,
"installPath": "$escaped_target",
"rid": "$escaped_rid",
"installedVersion": "$escaped_version",
"sourceRepository": "$escaped_repo",
"installMethod": "install.sh",
"updatedAtUtc": "$(date -u +%Y-%m-%dT%H:%M:%SZ)"
},
"update": {
"schemaVersion": 1
}
}
EOF
echo "Installed azimg to $target"
case ":$PATH:" in
*":$install_dir:"*) ;;
*)
echo "Add $install_dir to PATH to run azimg from any shell."
echo "For zsh or bash, add this line to your shell profile:"
echo " export PATH=\"\$PATH:$install_dir\""
;;
esac
"$target" version