-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-release.sh
More file actions
executable file
·164 lines (144 loc) · 4.02 KB
/
Copy pathinstall-release.sh
File metadata and controls
executable file
·164 lines (144 loc) · 4.02 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
163
164
#!/usr/bin/env bash
set -euo pipefail
REPO="${WHEROBOTS_CLI_REPO:-wherobots/wbc-cli}"
TAG="${WHEROBOTS_CLI_TAG:-latest}"
BINARY_NAME="${WHEROBOTS_CLI_BINARY:-wherobots}"
INSTALL_DIR="${INSTALL_DIR:-${HOME}/.local/bin}"
SKIP_CHECKSUM=0
usage() {
cat <<'EOF'
Install wherobots CLI from a GitHub release.
Requirements:
- gh CLI installed and authenticated with access to the repository.
Usage:
./scripts/install-release.sh [options]
Options:
--repo <owner/name> GitHub repository (default: wherobots/wbc-cli)
--tag <tag> Release tag (default: latest)
--install-dir <path> Install directory (default: ~/.local/bin)
--binary-name <name> Binary name/asset prefix (default: wherobots)
--skip-checksum Skip checksum verification
-h, --help Show help
Environment overrides:
WHEROBOTS_CLI_REPO
WHEROBOTS_CLI_TAG
WHEROBOTS_CLI_BINARY
INSTALL_DIR
EOF
}
while (($# > 0)); do
case "$1" in
--repo)
REPO="${2:?missing value for --repo}"
shift 2
;;
--tag)
TAG="${2:?missing value for --tag}"
shift 2
;;
--install-dir)
INSTALL_DIR="${2:?missing value for --install-dir}"
shift 2
;;
--binary-name)
BINARY_NAME="${2:?missing value for --binary-name}"
shift 2
;;
--skip-checksum)
SKIP_CHECKSUM=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
if ! command -v gh >/dev/null 2>&1; then
echo "gh CLI is required. Install from https://cli.github.com/" >&2
exit 1
fi
if ! command -v install >/dev/null 2>&1; then
echo "'install' command is required." >&2
exit 1
fi
if ! gh auth status >/dev/null 2>&1; then
echo "gh is not authenticated. Run: gh auth login" >&2
exit 1
fi
if ! gh repo view "$REPO" >/dev/null 2>&1; then
echo "Unable to access repository $REPO with current gh credentials." >&2
exit 1
fi
case "$(uname -s)" in
Linux) OS="linux" ;;
Darwin) OS="darwin" ;;
*)
echo "Unsupported OS: $(uname -s)" >&2
exit 1
;;
esac
case "$(uname -m)" in
x86_64|amd64) ARCH="amd64" ;;
arm64|aarch64) ARCH="arm64" ;;
*)
echo "Unsupported architecture: $(uname -m)" >&2
exit 1
;;
esac
ASSET="${BINARY_NAME}_${OS}_${ARCH}"
TMP_DIR="$(mktemp -d)"
cleanup() { rm -rf "$TMP_DIR"; }
trap cleanup EXIT
echo "Downloading ${ASSET} from ${REPO}@${TAG}..."
gh release download "$TAG" --repo "$REPO" --pattern "$ASSET" --dir "$TMP_DIR" --clobber
if [[ "$SKIP_CHECKSUM" -eq 0 ]]; then
echo "Verifying checksum..."
gh release download "$TAG" --repo "$REPO" --pattern "checksums.txt" --dir "$TMP_DIR" --clobber
EXPECTED="$(awk -v file="$ASSET" '$2 == file { print $1 }' "$TMP_DIR/checksums.txt" | head -n1)"
if [[ -z "$EXPECTED" ]]; then
echo "Could not find checksum entry for $ASSET" >&2
exit 1
fi
if command -v sha256sum >/dev/null 2>&1; then
ACTUAL="$(sha256sum "$TMP_DIR/$ASSET" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
ACTUAL="$(shasum -a 256 "$TMP_DIR/$ASSET" | awk '{print $1}')"
else
echo "No SHA-256 tool available (need sha256sum or shasum)." >&2
exit 1
fi
if [[ "$EXPECTED" != "$ACTUAL" ]]; then
echo "Checksum mismatch for $ASSET" >&2
exit 1
fi
fi
TARGET="${INSTALL_DIR}/${BINARY_NAME}"
# Ensure the install directory exists; create without sudo when possible.
if [[ ! -d "$INSTALL_DIR" ]]; then
if ! mkdir -m 0755 -p "$INSTALL_DIR" 2>/dev/null; then
if command -v sudo >/dev/null 2>&1; then
sudo mkdir -p "$INSTALL_DIR"
else
echo "Cannot create $INSTALL_DIR and sudo is unavailable." >&2
exit 1
fi
fi
fi
if [[ -w "$INSTALL_DIR" ]]; then
install -m 0755 "$TMP_DIR/$ASSET" "$TARGET"
else
if command -v sudo >/dev/null 2>&1; then
sudo install -m 0755 "$TMP_DIR/$ASSET" "$TARGET"
else
echo "No write access to $INSTALL_DIR and sudo is unavailable." >&2
exit 1
fi
fi
echo "Installed: $TARGET"
echo "If needed, add to PATH: export PATH=\"${INSTALL_DIR}:\$PATH\""