-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·133 lines (107 loc) · 3.18 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·133 lines (107 loc) · 3.18 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
#!/bin/sh
set -e
REPO="vaughnbosu/cws-cli"
BINARY="cws"
BASE_URL="https://github.com/${REPO}/releases/latest/download"
# Allow custom install directory via env var
INSTALL_DIR="${CWS_INSTALL_DIR:-}"
main() {
os=$(detect_os)
arch=$(detect_arch)
archive="${BINARY}_${os}_${arch}.tar.gz"
printf "Installing cws (%s/%s)...\n" "$os" "$arch"
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
download "$BASE_URL/$archive" "$tmpdir/$archive"
download "$BASE_URL/checksums.txt" "$tmpdir/checksums.txt"
verify_checksum "$tmpdir" "$archive" "$os"
tar xzf "$tmpdir/$archive" -C "$tmpdir" "$BINARY"
install_dir=$(resolve_install_dir)
install_binary "$tmpdir/$BINARY" "$install_dir"
printf "Successfully installed %s to %s/%s\n" "$BINARY" "$install_dir" "$BINARY"
check_path "$install_dir"
}
detect_os() {
case "$(uname -s)" in
Linux*) echo "linux" ;;
Darwin*) echo "darwin" ;;
*) printf "Error: unsupported OS: %s\n" "$(uname -s)" >&2; exit 1 ;;
esac
}
detect_arch() {
case "$(uname -m)" in
x86_64|amd64) echo "amd64" ;;
aarch64|arm64) echo "arm64" ;;
*) printf "Error: unsupported architecture: %s\n" "$(uname -m)" >&2; exit 1 ;;
esac
}
download() {
url="$1"
dest="$2"
if command -v curl >/dev/null 2>&1; then
curl -fsSL -o "$dest" "$url"
elif command -v wget >/dev/null 2>&1; then
wget -qO "$dest" "$url"
else
printf "Error: curl or wget is required\n" >&2
exit 1
fi
}
verify_checksum() {
dir="$1"
file="$2"
os="$3"
expected=$(awk -v file="$file" '$2 == file { print $1; exit }' "$dir/checksums.txt")
if [ -z "$expected" ]; then
printf "Error: checksum not found for %s\n" "$file" >&2
exit 1
fi
if [ "$os" = "darwin" ]; then
actual=$(shasum -a 256 "$dir/$file" | awk '{print $1}')
else
actual=$(sha256sum "$dir/$file" | awk '{print $1}')
fi
if [ "$expected" != "$actual" ]; then
printf "Error: checksum verification failed\n" >&2
printf " expected: %s\n" "$expected" >&2
printf " actual: %s\n" "$actual" >&2
exit 1
fi
}
resolve_install_dir() {
if [ -n "$INSTALL_DIR" ]; then
mkdir -p "$INSTALL_DIR"
echo "$INSTALL_DIR"
return
fi
if [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then
echo "/usr/local/bin"
return
fi
fallback="$HOME/.local/bin"
mkdir -p "$fallback"
echo "$fallback"
}
install_binary() {
src="$1"
dir="$2"
if [ ! -w "$dir" ]; then
printf "Error: %s is not writable\n" "$dir" >&2
printf "Set CWS_INSTALL_DIR to a writable directory:\n" >&2
printf ' curl -fsSL https://vaughnbosu.github.io/cws-cli/install.sh | CWS_INSTALL_DIR="$HOME/.local/bin" sh\n' >&2
exit 1
fi
install -m 755 "$src" "$dir/$BINARY"
}
check_path() {
dir="$1"
case ":$PATH:" in
*":$dir:"*) ;;
*)
printf "\nWarning: %s is not in your PATH\n" "$dir"
printf "Add it to your shell profile:\n"
printf " export PATH=\"%s:\$PATH\"\n" "$dir"
;;
esac
}
main