-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall
More file actions
executable file
·183 lines (155 loc) · 4.57 KB
/
Copy pathinstall
File metadata and controls
executable file
·183 lines (155 loc) · 4.57 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
#!/usr/bin/env bash
set -euo pipefail
repo="${OPENKNOWLEDGE_REPO:-openknowledge-sh/openknowledge}"
version="${OPENKNOWLEDGE_VERSION:-latest}"
base_url="${OPENKNOWLEDGE_BASE_URL:-}"
install_dir="${OPENKNOWLEDGE_INSTALL_DIR:-$HOME/.local/bin}"
binary="openknowledge"
tmp=""
staging=""
cleanup() {
if [ -n "$staging" ]; then
rm -f -- "$staging"
fi
if [ -n "$tmp" ]; then
rm -rf -- "$tmp"
fi
}
trap cleanup EXIT
need() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "openknowledge install: missing required command: $1" >&2
exit 1
fi
}
detect_os() {
case "$(uname -s)" in
Darwin) echo "darwin" ;;
Linux) echo "linux" ;;
*)
echo "openknowledge install: unsupported OS: $(uname -s)" >&2
exit 1
;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64 | amd64) echo "amd64" ;;
arm64 | aarch64) echo "arm64" ;;
*)
echo "openknowledge install: unsupported architecture: $(uname -m)" >&2
exit 1
;;
esac
}
release_base() {
if [ -n "$base_url" ]; then
echo "${base_url%/}"
return
fi
if [ "$version" = "latest" ]; then
echo "https://github.com/$repo/releases/latest/download"
return
fi
tag="${version#v}"
echo "https://github.com/$repo/releases/download/v$tag"
}
checksum_line() {
checksums="$1"
asset="$2"
while read -r sum file extra; do
if [ "$file" = "$asset" ] && [ -z "${extra:-}" ]; then
if ! [[ "$sum" =~ ^[0-9a-fA-F]{64}$ ]]; then
echo "openknowledge install: invalid checksum for $asset" >&2
return 1
fi
printf '%s %s\n' "$sum" "$file"
return 0
fi
done < "$checksums"
return 1
}
verify_checksum() {
checksums="$1"
archive="$2"
asset="$3"
line="$(checksum_line "$checksums" "$asset" || true)"
if [ -z "$line" ]; then
echo "openknowledge install: checksum missing for $asset" >&2
exit 1
fi
if command -v sha256sum >/dev/null 2>&1; then
(cd "$(dirname "$archive")" && printf '%s\n' "$line" | sha256sum -c -)
return
fi
if command -v shasum >/dev/null 2>&1; then
(cd "$(dirname "$archive")" && printf '%s\n' "$line" | shasum -a 256 -c -)
return
fi
echo "openknowledge install: missing sha256sum or shasum for checksum verification" >&2
exit 1
}
need curl
need tar
need install
if [ "$version" != "latest" ] && ! [[ "${version#v}" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "openknowledge install: version must be latest or a semantic version" >&2
exit 1
fi
if [ -z "$base_url" ] && ! [[ "$repo" =~ ^[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+$ ]]; then
echo "openknowledge install: repository must use owner/name" >&2
exit 1
fi
os="$(detect_os)"
arch="$(detect_arch)"
base="$(release_base)"
asset="openknowledge_${os}_${arch}.tar.gz"
tmp="$(mktemp -d)"
archive="$tmp/$asset"
checksums="$tmp/checksums.txt"
echo "Installing openknowledge from $base"
curl_options=(--fail --silent --show-error --location --connect-timeout 15 --max-time 600 --retry 3)
if [ -z "$base_url" ]; then
curl_options+=(--proto '=https' --tlsv1.2)
fi
curl "${curl_options[@]}" "$base/$asset" -o "$archive"
curl "${curl_options[@]}" "$base/checksums.txt" -o "$checksums"
verify_checksum "$checksums" "$archive" "$asset"
candidate="$tmp/$binary.candidate"
if ! tar -xOzf "$archive" "$binary" > "$candidate"; then
echo "openknowledge install: archive did not contain $binary" >&2
exit 1
fi
if [ ! -s "$candidate" ]; then
echo "openknowledge install: extracted $binary is empty" >&2
exit 1
fi
mkdir -p "$install_dir"
destination="$install_dir/$binary"
if { [ -e "$destination" ] || [ -L "$destination" ]; } && [ ! -f "$destination" ] && [ ! -L "$destination" ]; then
echo "openknowledge install: destination is not a regular file: $destination" >&2
exit 1
fi
staging="$(mktemp "$install_dir/.openknowledge.install.XXXXXX")"
install -m 0755 "$candidate" "$staging"
if ! candidate_version="$("$staging" version)"; then
echo "openknowledge install: downloaded binary failed its version preflight" >&2
exit 1
fi
if ! [[ "$candidate_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "openknowledge install: downloaded binary reported an invalid version: $candidate_version" >&2
exit 1
fi
if [ "$version" != "latest" ] && [ "$candidate_version" != "${version#v}" ]; then
echo "openknowledge install: downloaded binary is $candidate_version, expected ${version#v}" >&2
exit 1
fi
mv -f -- "$staging" "$destination"
staging=""
echo "Installed $candidate_version to $destination"
case ":$PATH:" in
*":$install_dir:"*) ;;
*)
echo "Add $install_dir to PATH to run openknowledge from any shell."
;;
esac