forked from owainlewis/push
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·116 lines (98 loc) · 2.68 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·116 lines (98 loc) · 2.68 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
#!/usr/bin/env sh
set -eu
repo="owainlewis/push"
bin_dir="${BIN_DIR:-$HOME/.local/bin}"
need() {
command -v "$1" >/dev/null 2>&1 || {
echo "push install: missing required command: $1" >&2
exit 1
}
}
need curl
need tar
sha256() {
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{ print $1 }'
elif command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{ print $1 }'
else
echo "push install: missing required command: shasum or sha256sum" >&2
exit 1
fi
}
os="$(uname -s)"
arch="$(uname -m)"
case "$os:$arch" in
Darwin:arm64) target="aarch64-apple-darwin" ;;
Darwin:x86_64) target="x86_64-apple-darwin" ;;
Linux:x86_64) target="x86_64-unknown-linux-gnu" ;;
Linux:aarch64|Linux:arm64) target="aarch64-unknown-linux-gnu" ;;
*)
echo "push install: unsupported platform $os/$arch" >&2
exit 1
;;
esac
tmp="$(mktemp -d)"
staged=""
cleanup() {
rm -rf "$tmp"
if [ -n "$staged" ]; then
rm -f "$staged"
fi
}
trap cleanup EXIT
trap 'exit 129' HUP
trap 'exit 130' INT
trap 'exit 143' TERM
api="https://api.github.com/repos/$repo/releases/latest"
asset_url="$(
curl -fsSL "$api" \
| sed -n 's/.*"browser_download_url": "\(.*push-v[^"]*-'"$target"'\.tar\.gz\)".*/\1/p' \
| head -n 1
)"
if [ -z "$asset_url" ]; then
echo "push install: no release asset found for $target" >&2
exit 1
fi
echo "Downloading $asset_url"
curl -fsSL "$asset_url" -o "$tmp/push.tar.gz"
curl -fsSL "${asset_url}.sha256" -o "$tmp/push.tar.gz.sha256"
expected="$(awk 'NR == 1 { print $1 }' "$tmp/push.tar.gz.sha256" | tr '[:upper:]' '[:lower:]')"
case "$expected" in
*[!0-9a-f]*|'')
echo "push install: release checksum is malformed" >&2
exit 1
;;
esac
if [ "${#expected}" -ne 64 ]; then
echo "push install: release checksum is malformed" >&2
exit 1
fi
actual="$(sha256 "$tmp/push.tar.gz")"
if [ "$actual" != "$expected" ]; then
echo "push install: release checksum verification failed" >&2
exit 1
fi
echo "Verified SHA-256 checksum"
tar -xzf "$tmp/push.tar.gz" -C "$tmp"
mkdir -p "$bin_dir"
source="$(find "$tmp" -type f -name push -perm -111 | head -n 1)"
if [ -z "$source" ]; then
echo "push install: release archive does not contain an executable" >&2
exit 1
fi
staged="$(mktemp "$bin_dir/.push.install.XXXXXX")"
cp "$source" "$staged"
chmod 755 "$staged"
if [ "$os" = "Darwin" ] && command -v xattr >/dev/null 2>&1; then
if xattr -p com.apple.provenance "$staged" >/dev/null 2>&1; then
xattr -d com.apple.provenance "$staged"
fi
fi
mv -f "$staged" "$bin_dir/push"
staged=""
echo "Installed push to $bin_dir/push"
case ":$PATH:" in
*":$bin_dir:"*) ;;
*) echo "Add $bin_dir to PATH to run push from any shell." ;;
esac