Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
176 lines (150 loc) · 4.75 KB
/
Copy pathinstall.sh
File metadata and controls
176 lines (150 loc) · 4.75 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
#!/bin/sh
set -eu
REPO="forkline/cli"
API_URL="${FORKLINE_GITHUB_API_URL:-https://api.github.com}"
DOWNLOAD_BASE="${FORKLINE_DOWNLOAD_BASE_URL:-https://github.com/${REPO}/releases/download}"
say() { printf '%s\n' "$*" >&2; }
fail() { say "error: $*"; exit 1; }
need() { command -v "$1" >/dev/null 2>&1 || fail "$1 is required but not installed"; }
detect_os() {
_uname_s="$(uname -s)"
case "$_uname_s" in
Linux) echo "linux" ;;
Darwin) echo "darwin" ;;
MINGW*|MSYS*|CYGWIN*) fail "Windows is not supported by this installer. Use npm (npm install -g @forkline/cli) or download the ZIP from https://github.com/${REPO}/releases" ;;
*) fail "unsupported OS: $_uname_s" ;;
esac
}
detect_arch() {
_uname_m="$(uname -m)"
case "$_uname_m" in
x86_64|amd64) echo "x86_64" ;;
aarch64|arm64) echo "aarch64" ;;
*) fail "unsupported architecture: $_uname_m" ;;
esac
}
detect_libc() {
if ldd --version 2>&1 | grep -qi musl; then
echo "musl"
elif ls /lib/ld-musl-* >/dev/null 2>&1; then
echo "musl"
else
echo "gnu"
fi
}
map_target() {
_os="$1"
_arch="$2"
case "$_os" in
linux)
_libc="$(detect_libc)"
echo "${_arch}-unknown-linux-${_libc}"
;;
darwin)
echo "${_arch}-apple-darwin"
;;
esac
}
resolve_version() {
if [ -n "${FORKLINE_VERSION:-}" ]; then
_v="$FORKLINE_VERSION"
_v="${_v#v}"
echo "$_v"
return
fi
_parse_tag() {
echo "$1" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -1
}
_url="${API_URL}/repos/${REPO}/releases/latest"
_json="$(curl -sS -H "Accept: application/vnd.github+json" "$_url" 2>/dev/null || true)"
_tag="$(_parse_tag "$_json")"
if [ -z "$_tag" ]; then
_url="${API_URL}/repos/${REPO}/releases?per_page=1"
_json="$(curl -sS -H "Accept: application/vnd.github+json" "$_url" 2>/dev/null || true)"
_tag="$(_parse_tag "$_json")"
fi
[ -n "$_tag" ] || fail "could not resolve a release tag from the GitHub API"
_tag="${_tag#v}"
echo "$_tag"
}
resolve_install_dir() {
if [ -n "${FORKLINE_INSTALL_DIR:-}" ]; then
echo "$FORKLINE_INSTALL_DIR"
return
fi
if [ -w /usr/local/bin ]; then
echo "/usr/local/bin"
else
echo "${HOME}/.local/bin"
fi
}
find_checksum() {
_file="$1"
_sums="$2"
_line="$(grep " ${_file}$" "$_sums" || true)"
[ -n "$_line" ] || fail "checksum for ${_file} not found in SHA256SUMS.txt"
echo "$_line" | awk '{print $1}'
}
verify_checksum() {
_file="$1"
_expected="$2"
if command -v sha256sum >/dev/null 2>&1; then
_actual="$(sha256sum "$_file" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
_actual="$(shasum -a 256 "$_file" | awk '{print $1}')"
else
fail "neither sha256sum nor shasum found; cannot verify checksum"
fi
if [ "$_actual" != "$_expected" ]; then
fail "checksum mismatch for $(basename "$_file"): expected $_expected, got $_actual"
fi
}
main() {
need curl
os="$(detect_os)"
arch="$(detect_arch)"
target="$(map_target "$os" "$arch")"
version="$(resolve_version)"
install_dir="$(resolve_install_dir)"
archive="forkline-${version}-${target}.tar.gz"
tag="v${version}"
base_url="${DOWNLOAD_BASE}/${tag}"
say "detected: ${os} ${arch} (${target})"
say "version: ${version}"
say "install dir: ${install_dir}"
tmpdir="$(mktemp -d)"
trap 'rm -rf "$tmpdir"' EXIT
say "downloading ${archive}..."
curl -sSfL -o "${tmpdir}/${archive}" "${base_url}/${archive}" || fail "failed to download ${archive}"
say "downloading SHA256SUMS.txt..."
curl -sSfL -o "${tmpdir}/SHA256SUMS.txt" "${base_url}/SHA256SUMS.txt" || fail "failed to download SHA256SUMS.txt"
expected_checksum="$(find_checksum "$archive" "${tmpdir}/SHA256SUMS.txt")"
say "verifying checksum..."
verify_checksum "${tmpdir}/${archive}" "$expected_checksum"
say "extracting..."
tar -xzf "${tmpdir}/${archive}" -C "$tmpdir" || fail "failed to extract archive"
binary=""
if [ -f "${tmpdir}/forkline-${version}-${target}/forkline" ]; then
binary="${tmpdir}/forkline-${version}-${target}/forkline"
else
binary="$(find "$tmpdir" -name forkline -type f | head -1)"
fi
[ -n "$binary" ] || fail "forkline binary not found in archive"
mkdir -p "$install_dir"
cp "$binary" "${tmpdir}/forkline"
chmod 0755 "${tmpdir}/forkline"
mv "${tmpdir}/forkline" "${install_dir}/forkline"
if [ -x "${install_dir}/forkline" ]; then
if "${install_dir}/forkline" --version >/dev/null 2>&1; then
say "$("${install_dir}/forkline" --version)"
fi
fi
case ":${PATH}:" in
*":${install_dir}:"*) ;;
*) say "hint: ${install_dir} is not in your PATH. Add it with:"
say " export PATH=\"${install_dir}:\$PATH\""
;;
esac
say "forkline ${version} installed to ${install_dir}/forkline"
}
main "$@"