-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·172 lines (145 loc) · 5.64 KB
/
install.sh
File metadata and controls
executable file
·172 lines (145 loc) · 5.64 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
165
166
167
168
169
170
171
172
#!/bin/sh
set -e # Exit on error
set -u # Exit on using unset variable
echo "Installing HoundDog CLI ..."
cleanup() {
rm -rf /tmp/hounddog.tar.gz /tmp/hounddog.sha256 > /dev/null 2>&1
}
abort() {
cleanup
echo "$@" 1>&2
exit 1
}
usage() {
cat <<EOF
Usage: sh install.sh [--version VERSION]
Options:
-v, --version VERSION Install a specific release version.
Allowed formats: x.y.z, x.y.z-alpha, x.y.z-beta.
Do not include a leading "v".
Defaults to "latest".
-h, --help Show this help message.
EOF
}
VERSION="${HOUNDDOG_VERSION:-latest}"
while [ "$#" -gt 0 ]; do
case "$1" in
-v|--version)
[ "$#" -ge 2 ] || abort "Missing value for $1."
VERSION="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
abort "Unknown option: $1. Use --help to see supported options."
;;
esac
done
[ -n "$VERSION" ] || abort "Version cannot be empty."
# Check operating system.
OS=$(uname -s)
case "$OS" in
Linux*) OS="linux" ;;
Darwin*) OS="macos" ;;
*) abort "This HoundDog CLI installation script only supports Linux and macOS." ;;
esac
# Check CPU architecture.
ARCH=$(uname -m)
case "$ARCH" in
x86_64) ARCH="amd64" ;;
aarch64|arm64) ARCH="arm64" ;;
*) abort "Unsupported CPU architecture. HoundDog CLI requires a AMD64 or ARM64 processor." ;;
esac
# Check other prerequisites.
command -v curl >/dev/null 2>&1 || abort "Command 'curl' is required to install HoundDog CLI."
command -v tar >/dev/null 2>&1 || abort "Command 'tar' is required to install HoundDog CLI."
command -v awk >/dev/null 2>&1 || abort "Command 'awk' is required to install HoundDog CLI."
if [ "$OS" = "macos" ]; then
command -v shasum >/dev/null 2>&1 || abort "Command 'shasum' is required to install HoundDog CLI."
else
command -v sha256sum >/dev/null 2>&1 || abort "Command 'sha256sum' is required to install HoundDog CLI."
fi
if [ "$VERSION" != "latest" ]; then
printf '%s\n' "$VERSION" | awk 'BEGIN { ok = 0 } /^[0-9]+\.[0-9]+\.[0-9]+(-(alpha|beta))?$/ { ok = 1 } END { exit(ok ? 0 : 1) }' \
|| abort "Invalid version '${VERSION}'. Use x.y.z, x.y.z-alpha, or x.y.z-beta (without a leading 'v')."
fi
# Download the tarball and checksum files to a temporary directory.
download_release_assets() {
RELEASES_URL="https://github.com/hounddogai/hounddog/releases"
ARTIFACT="hounddog-${OS}-${ARCH}.tar.gz"
TAGS_TO_TRY=""
if [ "$VERSION" = "latest" ]; then
TAGS_TO_TRY="latest"
else
TAGS_TO_TRY="${VERSION} v${VERSION}"
fi
for TAG in $TAGS_TO_TRY; do
if [ "$TAG" = "latest" ]; then
DL_URL="${RELEASES_URL}/latest/download"
else
DL_URL="${RELEASES_URL}/download/${TAG}"
fi
if curl -fsSL "${DL_URL}/${ARTIFACT}" -o /tmp/hounddog.tar.gz \
&& curl -fsSL "${DL_URL}/${ARTIFACT}.sha256" -o /tmp/hounddog.sha256; then
return
fi
done
abort "Failed to download HoundDog CLI version '${VERSION}'. Ensure the release exists and uses x.y.z, x.y.z-alpha, or x.y.z-beta."
}
download_release_assets
# Verify checksum.
EXPECTED_CHECKSUM=$(awk '{print $1}' /tmp/hounddog.sha256)
if [ "$OS" = "macos" ]; then
ACTUAL_CHECKSUM=$(shasum -a 256 /tmp/hounddog.tar.gz | awk '{print $1}')
else
ACTUAL_CHECKSUM=$(sha256sum /tmp/hounddog.tar.gz | awk '{print $1}')
fi
[ "$EXPECTED_CHECKSUM" = "$ACTUAL_CHECKSUM" ] || abort "Checksum mismatch. Aborting installation."
# If the script is not running as root, install to ~/.hounddog/bin/hounddog.
if [ "$(id -u)" -ne 0 ]; then
# Detect shell configuration file.
SHELL_NAME=$(basename "${SHELL:-}")
case "$SHELL_NAME" in
bash) SHELL_RC="$HOME/.bashrc" ;;
zsh) SHELL_RC="$HOME/.zshrc" ;;
fish) SHELL_RC="$HOME/.config/fish/config.fish" ;;
*) abort "HoundDog CLI only supports Bash, Zsh, and Fish shells." ;;
esac
# Extract binary to ~/.hounddog/bin/hounddog
mkdir -p "${HOME}/.hounddog/bin"
tar -x -f /tmp/hounddog.tar.gz -C "${HOME}/.hounddog/bin" hounddog
chmod 755 "${HOME}/.hounddog/bin/hounddog"
# Add ~/.hounddog/bin to user's PATH in shell rc file.
if ! grep -q "export PATH=\$PATH:\$HOME/.hounddog/bin" "${SHELL_RC}"; then
echo "Adding ${HOME}/.hounddog/bin to PATH in ${SHELL_RC}..."
printf "\nexport PATH=\$PATH:\$HOME/.hounddog/bin\n" >> "${SHELL_RC}"
export PATH="${HOME}/.hounddog/bin:${PATH}"
fi
# Add ~/.hounddog/bin to PATH in current shell.
export PATH="${PATH}:${HOME}/.hounddog/bin"
cleanup
echo ""
echo "HoundDog CLI installed successfully."
[ "$VERSION" = "latest" ] || echo "Installed version: ${VERSION}"
echo "Please restart your shell and run 'hounddog --help' to get started."
# If the script is running as root, install to /usr/local/bin/hounddog.
else
# Check prerequisites.
[ -d "/usr/local/bin" ] || abort "Directory '/usr/local/bin' does not exist. Aborting installation."
[ -w "/usr/local/bin" ] || abort "No write permission to '/usr/local/bin'. Aborting installation."
case ":$PATH:" in
*:/usr/local/bin:*) ;;
*) abort "Directory '/usr/local/bin' is not in PATH. Aborting installation." ;;
esac
# Extract tarball to /usr/local/bin/hounddog
tar -x -f /tmp/hounddog.tar.gz -C /usr/local/bin hounddog
chmod 755 /usr/local/bin/hounddog
cleanup
echo ""
echo "HoundDog CLI has been installed successfully."
[ "$VERSION" = "latest" ] || echo "Installed version: ${VERSION}"
echo "Run 'hounddog --help' to get started."
fi