Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
113 lines (91 loc) · 2.81 KB
/
Copy pathinstall.sh
File metadata and controls
113 lines (91 loc) · 2.81 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
#!/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"
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; }
detect_platform() {
local os arch
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
arch="$(uname -m)"
case "$os" in
linux|darwin) ;;
*) die "unsupported operating system: $os (Windows users: download the .zip from https://github.com/$REPO/releases)" ;;
esac
case "$arch" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
*) die "unsupported architecture: $arch" ;;
esac
echo "${os}_${arch}"
}
choose_install_dir() {
if [ -n "${LINKO_INSTALL:-}" ]; then
echo "$LINKO_INSTALL"
elif [ -w /usr/local/bin ] 2>/dev/null; then
echo /usr/local/bin
elif [ "$(id -u)" = "0" ]; then
echo /usr/local/bin
else
echo "$HOME/.local/bin"
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 tmp dest
platform="$(detect_platform)"
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)"
trap 'rm -rf "$tmp"' EXIT
curl -fsSL "$url" -o "$tmp/$archive" \
|| die "download failed: $url"
tar -xzf "$tmp/$archive" -C "$tmp"
[ -f "$tmp/$BINARY" ] || die "the archive does not contain a ${BINARY} binary"
chmod +x "$tmp/$BINARY"
dest="$(choose_install_dir)"
mkdir -p "$dest"
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
green "✓ ${BINARY} installed to ${dest}/${BINARY}"
if ! command -v "$BINARY" >/dev/null 2>&1; then
echo
red "! ${dest} is not on your PATH"
echo " Add this to your shell profile:"
echo " export PATH=\"${dest}:\$PATH\"
3535
;"
fi
echo
echo "Next step:"
echo " linko init"
}
main "$@"