-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
129 lines (104 loc) · 2.58 KB
/
Copy pathinstall.sh
File metadata and controls
129 lines (104 loc) · 2.58 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
#!/usr/bin/env sh
set -eu
REPO="${DISKD_REPO:-diskd-ai/diskd-cli}"
INSTALL_DIR="${DISKD_INSTALL_DIR:-}"
VERSION="${DISKD_VERSION:-}"
fail() {
printf '%s\n' "diskd install: $*" >&2
exit 1
}
need() {
command -v "$1" >/dev/null 2>&1 || fail "missing required command: $1"
}
detect_target() {
os="$(uname -s)"
arch="$(uname -m)"
case "$os" in
Linux)
os_part="unknown-linux-musl"
;;
Darwin)
os_part="apple-darwin"
;;
*)
fail "unsupported OS: $os"
;;
esac
case "$arch" in
x86_64 | amd64)
arch_part="x86_64"
;;
arm64 | aarch64)
arch_part="aarch64"
;;
*)
fail "unsupported architecture: $arch"
;;
esac
printf '%s-%s\n' "$arch_part" "$os_part"
}
resolve_version() {
if [ -n "$VERSION" ]; then
printf '%s\n' "$VERSION"
return
fi
need curl
latest_url="$(curl -fsSL -o /dev/null -w '%{url_effective}' "https://github.com/$REPO/releases/latest")"
case "$latest_url" in
*/releases/tag/*)
version="${latest_url##*/releases/tag/}"
version="${version%%\?*}"
version="${version%%\#*}"
printf '%s\n' "$version"
;;
*)
fail "could not resolve latest release from $latest_url"
;;
esac
}
resolve_install_dir() {
if [ -n "$INSTALL_DIR" ]; then
printf '%s\n' "$INSTALL_DIR"
return
fi
if [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then
printf '%s\n' "/usr/local/bin"
return
fi
printf '%s\n' "$HOME/.local/bin"
}
verify_checksum() {
asset="$1"
checksum="$2"
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 -c "$checksum"
return
fi
if command -v sha256sum >/dev/null 2>&1; then
sha256sum -c "$checksum"
return
fi
fail "missing checksum command: shasum or sha256sum"
}
main() {
need curl
need tar
target="$(detect_target)"
version="$(resolve_version)"
[ -n "$version" ] || fail "could not resolve release version"
asset="diskd-${version}-${target}.tar.gz"
base_url="https://github.com/$REPO/releases/download/$version"
tmp_dir="$(mktemp -d)"
install_dir="$(resolve_install_dir)"
trap 'rm -rf "$tmp_dir"' EXIT INT TERM
printf '%s\n' "Installing diskd $version for $target"
curl -fsSL "$base_url/$asset" -o "$tmp_dir/$asset"
curl -fsSL "$base_url/$asset.sha256" -o "$tmp_dir/$asset.sha256"
(cd "$tmp_dir" && verify_checksum "$asset" "$asset.sha256")
tar -xzf "$tmp_dir/$asset" -C "$tmp_dir"
mkdir -p "$install_dir"
cp "$tmp_dir/diskd" "$install_dir/diskd"
chmod 0755 "$install_dir/diskd"
printf '%s\n' "diskd installed to $install_dir/diskd"
}
main "$@"