-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·150 lines (129 loc) · 5.14 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·150 lines (129 loc) · 5.14 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
#!/bin/sh
set -eu
DEFAULT_REPOSITORY="Tomyail/herdr-connect"
say() {
printf '%s\n' "$*"
}
fail() {
printf 'herdr-connect installer: %s\n' "$*" >&2
exit 1
}
require_command() {
command -v "$1" >/dev/null 2>&1 || fail "required command not found: $1"
}
normalize_os() {
case "$1" in
Darwin | darwin) printf '%s\n' "darwin" ;;
Linux | linux) printf '%s\n' "linux" ;;
*) fail "unsupported operating system: $1 (Windows users should download the release zip)" ;;
esac
}
normalize_arch() {
case "$1" in
arm64 | aarch64) printf '%s\n' "arm64" ;;
x86_64 | amd64) printf '%s\n' "amd64" ;;
*) fail "unsupported CPU architecture: $1" ;;
esac
}
checksum_file() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
else
fail "sha256sum or shasum is required to verify the download"
fi
}
# resolve_latest_version 用来在没有显式指定 HERDR_CONNECT_VERSION 时找最新
# release。不能用 GitHub 的 /releases/latest 接口——它明确排除 prerelease,
# 而这个仓库目前所有 tag 都是 vX.Y.Z-preview.N,会被发布流水线自动标成
# prerelease,命中 /releases/latest 只会拿到 404。改用列表接口(按创建时间
# 倒序)取第一条的 tag_name。
resolve_latest_version() {
releases_url="https://api.github.com/repos/${repository}/releases"
# Capture the full response before grepping it: piping curl straight into
# `grep -m1` makes grep close the pipe as soon as it finds a match, which
# sends curl a SIGPIPE and prints a spurious "Failure writing output" error
# even though the lookup succeeded.
response="$(curl -fsSL --retry 3 --proto '=https' --tlsv1.2 \
-H "Accept: application/vnd.github+json" "$releases_url")"
tag="$(printf '%s\n' "$response" | grep -m1 '"tag_name"' | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')"
[ -n "$tag" ] || fail "could not determine the latest release from $releases_url"
printf '%s\n' "$tag"
}
repository="${HERDR_CONNECT_REPOSITORY:-$DEFAULT_REPOSITORY}"
require_command curl
require_command grep
require_command sed
if [ -n "${HERDR_CONNECT_VERSION:-}" ]; then
version="$HERDR_CONNECT_VERSION"
else
say "No HERDR_CONNECT_VERSION given; looking up the latest release..."
version="$(resolve_latest_version)"
fi
case "$version" in
v*) ;;
*) version="v$version" ;;
esac
install_dir="${HERDR_CONNECT_INSTALL_DIR:-${HOME:?HOME is required}/.local/bin}"
os="$(normalize_os "${HERDR_CONNECT_OS:-$(uname -s)}")"
arch="$(normalize_arch "${HERDR_CONNECT_ARCH:-$(uname -m)}")"
release_version="${version#v}"
archive_name="herdr-connect_${release_version}_${os}_${arch}"
asset_name="${archive_name}.tar.gz"
release_base_url="${HERDR_CONNECT_DOWNLOAD_BASE_URL:-https://github.com/${repository}/releases/download/${version}}"
require_command tar
require_command awk
require_command mktemp
temp_dir="$(mktemp -d "${TMPDIR:-/tmp}/herdr-connect-install.XXXXXX")"
cleanup() {
rm -rf "$temp_dir"
}
trap cleanup EXIT HUP INT TERM
say "Downloading Herdr Connect ${version} for ${os}/${arch}..."
curl -fsSL --retry 3 --proto '=https' --tlsv1.2 \
-o "$temp_dir/$asset_name" "$release_base_url/$asset_name"
curl -fsSL --retry 3 --proto '=https' --tlsv1.2 \
-o "$temp_dir/SHA256SUMS" "$release_base_url/SHA256SUMS"
expected_checksum="$(awk -v asset="$asset_name" '$2 == asset || $2 == "./" asset { print $1; exit }' "$temp_dir/SHA256SUMS")"
case "$expected_checksum" in
"" | *[!0-9a-fA-F]*) fail "release checksum not found for $asset_name" ;;
esac
[ "${#expected_checksum}" -eq 64 ] || fail "invalid release checksum for $asset_name"
actual_checksum="$(checksum_file "$temp_dir/$asset_name")"
[ "$actual_checksum" = "$expected_checksum" ] || fail "checksum verification failed for $asset_name"
tar -xzf "$temp_dir/$asset_name" -C "$temp_dir"
binary_path="$temp_dir/$archive_name/herdr-connect"
[ -f "$binary_path" ] || fail "downloaded archive does not contain herdr-connect"
mkdir -p "$install_dir"
if command -v install >/dev/null 2>&1; then
install -m 0755 "$binary_path" "$install_dir/herdr-connect"
else
cp "$binary_path" "$install_dir/herdr-connect"
chmod 0755 "$install_dir/herdr-connect"
fi
say "Installed Herdr Connect ${version} to $install_dir/herdr-connect"
case ":${PATH:-}:" in
*":$install_dir:"*) ;;
*) say "Add $install_dir to PATH, or run the binary by its full path." ;;
esac
say ""
say "Next:"
say " 1. Check readiness:"
say " $install_dir/herdr-connect doctor"
say " 2. Install and start the background service:"
say " $install_dir/herdr-connect service install"
say " 3. Check service health:"
say " $install_dir/herdr-connect service status"
service_config="${HERDR_CONNECT_SERVICE_CONFIG:-}"
if [ -z "$service_config" ]; then
case "$os" in
darwin) service_config="${HOME:?HOME is required}/Library/LaunchAgents/com.tomyail.herdr-connect.plist" ;;
linux) service_config="${HOME:?HOME is required}/.config/systemd/user/herdr-connect.service" ;;
esac
fi
if [ -f "$service_config" ]; then
say ""
say "An existing service was detected. Restart it to use the new binary:"
say " $install_dir/herdr-connect service restart"
fi