-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·162 lines (141 loc) · 3.54 KB
/
install.sh
File metadata and controls
executable file
·162 lines (141 loc) · 3.54 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
#!/usr/bin/env bash
set -euo pipefail
BIN_NAME="devwrap"
PREFIX="${PREFIX:-/usr/local}"
BIN_DIR="${BIN_DIR:-$PREFIX/bin}"
DEST="$BIN_DIR/$BIN_NAME"
TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT
usage() {
cat <<'EOF'
Usage: ./install.sh [-v VERSION]
Installs devwrap from GitHub releases.
Arguments:
-v, --version VERSION
Optional release version (with or without leading "v").
Omit to install the latest release.
Environment:
PREFIX Install prefix (default: /usr/local)
BIN_DIR Install bin dir (default: $PREFIX/bin)
EOF
}
VERSION_INPUT=""
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
-v|--version)
if [[ $# -lt 2 ]]; then
echo "error: $1 requires a value" >&2
usage
exit 1
fi
VERSION_INPUT="$2"
shift 2
;;
--version=*)
VERSION_INPUT="${1#*=}"
shift
;;
-* )
echo "error: unknown option: $1" >&2
usage
exit 1
;;
*)
if [[ -n "$VERSION_INPUT" ]]; then
echo "error: version provided more than once" >&2
usage
exit 1
fi
VERSION_INPUT="$1"
shift
;;
esac
done
detect_platform() {
local os arch
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "$os" in
darwin|linux) ;;
*)
echo "error: unsupported OS '$os'" >&2
exit 1
;;
esac
case "$arch" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
*)
echo "error: unsupported architecture '$arch'" >&2
exit 1
;;
esac
echo "${os}_${arch}"
}
need_cmd() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "error: required command not found: $1" >&2
exit 1
fi
}
need_cmd curl
need_cmd tar
need_cmd shasum
REPO="iterate/devwrap"
PLATFORM="$(detect_platform)"
ASSET="${BIN_NAME}_${PLATFORM}.tar.gz"
if [[ -z "$VERSION_INPUT" ]]; then
echo "Resolving latest release for ${REPO}..."
VERSION="$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' | head -n 1)"
if [[ -z "$VERSION" ]]; then
echo "error: failed to resolve latest release tag for ${REPO}" >&2
exit 1
fi
else
VERSION="${VERSION_INPUT#v}"
VERSION="v${VERSION}"
fi
BASE_URL="https://github.com/${REPO}/releases/download/${VERSION}"
ARCHIVE_PATH="${TMP_DIR}/${ASSET}"
CHECKSUMS_PATH="${TMP_DIR}/checksums.txt"
echo "Downloading ${ASSET} from ${VERSION}..."
curl -fL "${BASE_URL}/${ASSET}" -o "$ARCHIVE_PATH"
curl -fL "${BASE_URL}/checksums.txt" -o "$CHECKSUMS_PATH"
echo "Verifying checksum..."
expected="$(grep " ${ASSET}$" "$CHECKSUMS_PATH" | awk '{print $1}')"
if [[ -z "$expected" ]]; then
echo "error: checksum entry for ${ASSET} not found" >&2
exit 1
fi
actual="$(shasum -a 256 "$ARCHIVE_PATH" | awk '{print $1}')"
if [[ "$expected" != "$actual" ]]; then
echo "error: checksum mismatch for ${ASSET}" >&2
exit 1
fi
echo "Extracting..."
tar -xzf "$ARCHIVE_PATH" -C "$TMP_DIR"
EXTRACTED_BIN="${TMP_DIR}/${BIN_NAME}_${PLATFORM}"
if [[ ! -f "$EXTRACTED_BIN" ]]; then
echo "error: archive did not contain expected binary '${BIN_NAME}_${PLATFORM}'" >&2
exit 1
fi
echo "Installing to $DEST..."
if [[ ! -d "$BIN_DIR" ]]; then
if [[ -w "$(dirname "$BIN_DIR")" ]]; then
mkdir -p "$BIN_DIR"
else
sudo mkdir -p "$BIN_DIR"
fi
fi
if [[ -w "$BIN_DIR" ]]; then
install -m 0755 "$EXTRACTED_BIN" "$DEST"
else
sudo install -m 0755 "$EXTRACTED_BIN" "$DEST"
fi
echo "Installed: $DEST"
echo "Version: $VERSION"
echo "Run: $BIN_NAME proxy status"