Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·119 lines (102 loc) · 3.33 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·119 lines (102 loc) · 3.33 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
#!/bin/sh
# openapi-generator installer.
#
# curl -fsSL https://raw.githubusercontent.com/xseman/openapi-generator/master/install.sh | sh
#
# OPENAPI_GENERATOR_VERSION pins a release (default: the latest), OPENAPI_GENERATOR_INSTALL_DIR the
# directory the binary lands in (default: ~/.local/bin).
set -eu
REPO="xseman/openapi-generator"
BIN="openapi-generator"
INSTALL_DIR="${OPENAPI_GENERATOR_INSTALL_DIR:-$HOME/.local/bin}"
main() {
echo ""
echo " ⌁ openapi-generator installer"
echo " github.com/$REPO"
echo ""
case "$(uname -s)" in
Linux) os="linux" ;;
Darwin) os="darwin" ;;
*) err "unsupported OS: $(uname -s) — openapi-generator runs on Linux and macOS" ;;
esac
case "$(uname -m)" in
x86_64 | amd64) arch="amd64" ;;
aarch64 | arm64) arch="arm64" ;;
*) err "unsupported architecture: $(uname -m)" ;;
esac
asset="${BIN}-${os}-${arch}"
log "detected ${os}/${arch}"
need curl
need awk
sha_tool
tag="${OPENAPI_GENERATOR_VERSION:-}"
case "$tag" in
"")
# The /releases/latest redirect names the newest tag, so the
# install needs no GitHub API token and hits no rate limit.
latest="$(curl -fsSLI -o /dev/null -w '%{url_effective}' "https://github.com/${REPO}/releases/latest")" ||
err "can't reach github.com — try again later"
tag="${latest##*/}"
;;
v*) ;;
*) tag="v${tag}" ;;
esac
[ -n "$tag" ] || err "could not work out the latest release"
base="https://github.com/${REPO}/releases/download/${tag}"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
log "fetching checksums for ${tag}..."
curl -fsSL --retry 3 --connect-timeout 10 --max-time 30 "${base}/CHECKSUMS.txt" -o "${tmp}/CHECKSUMS.txt" ||
err "release ${tag} has no CHECKSUMS.txt"
want="$(awk -v a="$asset" '$2 == a || $2 == "*" a { print $1; exit }' "${tmp}/CHECKSUMS.txt")"
[ "${#want}" -eq 64 ] || err "release ${tag} does not ship ${asset}"
log "downloading ${asset} ${tag}..."
curl -fSL --progress-bar --retry 3 --connect-timeout 10 --max-time 300 "${base}/${asset}" -o "${tmp}/${BIN}" ||
err "download failed from ${base}/${asset}"
got="$(sha256 "${tmp}/${BIN}")"
[ "$got" = "$want" ] || err "checksum mismatch: got ${got}, want ${want}"
mkdir -p "$INSTALL_DIR"
mv "${tmp}/${BIN}" "${INSTALL_DIR}/${BIN}"
chmod 0755 "${INSTALL_DIR}/${BIN}"
log "installed ${BIN} ${tag} to ${INSTALL_DIR}/${BIN}"
case ":${PATH}:" in
*":${INSTALL_DIR}:"*) ;;
*)
echo ""
warn "${INSTALL_DIR} is not in your PATH; add this to your shell config:"
echo ""
echo " export PATH=\"${INSTALL_DIR}:\$PATH\""
;;
esac
echo ""
log "run '${BIN} --help' to get started"
echo ""
}
log() { printf ' \033[32m>\033[0m %s\n' "$1"; }
warn() { printf ' \033[33m!\033[0m %s\n' "$1"; }
err() {
printf ' \033[31mx\033[0m %s\n' "$1" >&2
exit 1
}
need() {
command -v "$1" > /dev/null 2>&1 || err "requires '$1' — install it first"
}
# sha_tool picks whatever this machine has; the download is never installed
# unverified.
sha_tool() {
for t in sha256sum shasum openssl; do
if command -v "$t" > /dev/null 2>&1; then
SHA_TOOL="$t"
return
fi
done
err "requires sha256sum, shasum or openssl to verify the download"
}
sha256() {
case "$SHA_TOOL" in
sha256sum) sha256sum < "$1" | awk '{ print $1 }' ;;
shasum) shasum -a 256 < "$1" | awk '{ print $1 }' ;;
openssl) openssl dgst -sha256 < "$1" | awk '{ print $NF }' ;;
esac
}
main "$@"