Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
233 lines (200 loc) · 6.51 KB
/
Copy pathinstall.sh
File metadata and controls
233 lines (200 loc) · 6.51 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env bash
#
# linko installer
#
# curl -fsSL https://raw.githubusercontent.com/Devehab/linko/main/install.sh | bash
#
# Environment variables:
# LINKO_VERSION version to install (default: latest)
# LINKO_INSTALL install directory (default: /usr/local/bin, or ~/.local/bin)
set -euo pipefail
REPO="${LINKO_REPO:-Devehab/linko}"
VERSION="${LINKO_VERSION:-latest}"
BINARY="linko"
DOCS_URL="https://github.com/${REPO}/blob/main/GUIDE.md"
red() { printf '\033[31m%s\033[0m\n' "$*"; }
green() { printf '\033[32m%s\033[0m\n' "$*"; }
dim() { printf '\033[2m%s\033[0m\n' "$*"; }
die() { red "error: $*" >&2; exit 1; }
# TMP must be global: the EXIT trap fires after main() returns, so a variable
# scoped to main() would be unset by then and `set -u` would abort.
TMP=""
cleanup() { [ -n "${TMP:-}" ] && rm -rf "$TMP"; return 0; }
trap cleanup EXIT
detect_platform() {
local os arch
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
# Return non-zero rather than dying: the caller falls back to building from
# source, which covers platforms we do not ship binaries for.
case "$os" in
linux|darwin) ;;
*) return 1 ;;
esac
case "$arch" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
*) return 1 ;;
esac
echo "${os}_${arch}"
}
# How to install Go on this machine, in the user's own package manager.
go_install_hint() {
case "$(uname -s)" in
Darwin)
echo " brew install go"
echo " # no Homebrew? download the .pkg from https://go.dev/dl/"
;;
Linux)
echo " sudo apt install golang-go # Debian / Ubuntu"
echo " sudo dnf install golang # Fedora"
echo " sudo pacman -S go # Arch"
echo " # or download the tarball from https://go.dev/dl/"
;;
*)
echo " https://go.dev/dl/"
;;
esac
}
find_go() {
command -v go >/dev/null 2>&1 && return 0
# The official macOS/Linux packages install here without touching PATH.
local c
for c in /usr/local/go/bin /opt/homebrew/bin "$HOME/go/bin"; do
if [ -x "$c/go" ]; then
PATH="$c:$PATH"; export PATH
dim "found go in $c (not on your PATH)"
return 0
fi
done
return 1
}
build_from_source() {
local dest="$1" ref="$VERSION"
[ "$ref" = "latest" ] && ref="latest"
if ! find_go; then
echo
red "! There is no prebuilt binary for $(uname -s)/$(uname -m),"
red " and building from source needs Go, which is not installed."
echo
echo " Install Go, then run this installer again:"
go_install_hint
echo
exit 1
fi
dim "building from source with $(go version | awk '{print $3}')"
GOBIN="$dest" go install "github.com/${REPO}@${ref}" \
|| die "build from source failed"
}
on_path() {
case ":${PATH}:" in
*":$1:"*) return 0 ;;
*) return 1 ;;
esac
}
# Prefer a directory that is ALREADY on the user's PATH, so `linko` works the
# moment this script finishes. Only fall back to ~/.local/bin — which is often
# not on PATH — when nothing better is writable, and patch the shell profile
# in that case.
choose_install_dir() {
local d
if [ -n "${LINKO_INSTALL:-}" ]; then
echo "$LINKO_INSTALL"; return
fi
for d in /opt/homebrew/bin /usr/local/bin "$HOME/.local/bin" "$HOME/bin"; do
if on_path "$d" && [ -d "$d" ] && [ -w "$d" ]; then echo "$d"; return; fi
done
for d in /opt/homebrew/bin /usr/local/bin; do
if [ -d "$d" ] && [ -w "$d" ]; then echo "$d"; return; fi
done
echo "$HOME/.local/bin"
}
# Append the PATH line to whichever shell profiles exist, once.
add_to_profiles() {
local dir="$1" line rc touched=0
line="export PATH=\"${dir}:\$PATH\""
for rc in "$HOME/.zshrc" "$HOME/.bashrc" "$HOME/.bash_profile" "$HOME/.profile"; do
[ -f "$rc" ] || continue
touched=1
grep -qF "$dir" "$rc" 2>/dev/null && continue
printf '\n# added by the linko installer\n%s\n' "$line" >> "$rc"
dim "added ${dir} to $(basename "$rc")"
done
if [ "$touched" = "0" ]; then
rc="$HOME/.profile"
case "${SHELL:-}" in
*/zsh) rc="$HOME/.zshrc" ;;
*/bash) rc="$HOME/.bashrc" ;;
esac
printf '# added by the linko installer\n%s\n' "$line" >> "$rc"
dim "created $(basename "$rc") with ${dir} on PATH"
fi
}
resolve_version() {
if [ "$VERSION" != "latest" ]; then
echo "$VERSION"
return
fi
local url tag
url="https://api.github.com/repos/${REPO}/releases/latest"
tag="$(curl -fsSL "$url" | grep -m1 '"tag_name"' | cut -d'"' -f4 || true)"
[ -n "$tag" ] || die "could not determine the latest version — set LINKO_VERSION"
echo "$tag"
}
main() {
command -v curl >/dev/null 2>&1 || die "curl is required"
command -v tar >/dev/null 2>&1 || die "tar is required"
local platform version tag archive url dest
dest="$(choose_install_dir)"
mkdir -p "$dest"
platform="$(detect_platform)" || platform=""
if [ -z "$platform" ]; then
dim "no prebuilt binary for $(uname -s)/$(uname -m)"
build_from_source "$dest"
else
tag="$(resolve_version)"
version="${tag#v}"
archive="${BINARY}_${version}_${platform}.tar.gz"
url="https://github.com/${REPO}/releases/download/${tag}/${archive}"
dim "Installing ${BINARY} ${tag} (${platform})"
TMP="$(mktemp -d)"
if curl -fsSL "$url" -o "$TMP/$archive"; then
tar -xzf "$TMP/$archive" -C "$TMP" \
|| die "could not unpack $archive"
[ -f "$TMP/$BINARY" ] || die "the archive does not contain a ${BINARY} binary"
chmod +x "$TMP/$BINARY"
if [ -w "$dest" ]; then
mv "$TMP/$BINARY" "$dest/$BINARY"
else
dim "elevating with sudo to write to $dest"
sudo mv "$TMP/$BINARY" "$dest/$BINARY"
fi
else
dim "no release archive at $url — building from source instead"
build_from_source "$dest"
fi
fi
[ -x "$dest/$BINARY" ] || die "installation failed: $dest/$BINARY is missing"
green "✓ ${BINARY} installed to ${dest}/${BINARY}"
echo
if on_path "$dest"; then
echo "Next step:"
echo " linko init"
else
add_to_profiles "$dest"
echo
echo "Next steps:"
echo " exec \$SHELL # reload your shell once"
echo " linko init"
fi
echo
echo "Before that you need a Cloudflare API token with BOTH permissions:"
echo " Zone -> DNS -> Edit"
echo " Account -> Cloudflare Tunnel -> Edit"
echo " Create it at https://dash.cloudflare.com/profile/api-tokens"
echo
echo "Full guide (install, token, commands, troubleshooting):"
green " ${DOCS_URL}"
echo " or run: linko docs"
}
main "$@"