Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·119 lines (105 loc) · 2.59 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·119 lines (105 loc) · 2.59 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
#!/bin/sh
# cans installer — Apple Silicon macOS. Drops a binary on PATH, then: cans doctor
set -eu
REPO="veronica-agent/cans"
DEST="${CANS_BIN:-}"
die() {
echo "cans-install: $*" >&2
exit 1
}
os=$(uname -s)
arch=$(uname -m)
[ "$os" = "Darwin" ] || die "macOS only"
[ "$arch" = "arm64" ] || die "Apple Silicon only (got $arch)"
if [ -z "$DEST" ]; then
if [ -d /opt/homebrew/bin ] && [ -w /opt/homebrew/bin ]; then
DEST=/opt/homebrew/bin
elif [ -d /usr/local/bin ] && [ -w /usr/local/bin ]; then
DEST=/usr/local/bin
else
DEST="${HOME}/.local/bin"
fi
fi
mkdir -p "$DEST"
github_curl() {
url=$1
dest=$2
hdr=""
if [ -n "${GITHUB_TOKEN:-}" ]; then
hdr="Authorization: Bearer ${GITHUB_TOKEN}"
elif command -v gh >/dev/null 2>&1; then
tok=$(gh auth token 2>/dev/null || true)
if [ -n "$tok" ]; then
hdr="Authorization: Bearer ${tok}"
fi
fi
if [ -n "$hdr" ]; then
curl -fsSL -H "$hdr" -o "$dest" "$url"
else
curl -fsSL -o "$dest" "$url"
fi
}
pick_archive() {
dir=$1
for f in "$dir"/*.tar.gz "$dir"/*.tgz; do
if [ -f "$f" ]; then
echo "$f"
return 0
fi
done
return 1
}
download_release() {
tmp=$(mktemp -d -t cans-install)
# shellcheck disable=SC2064
trap 'rm -rf "$tmp"' EXIT
if command -v gh >/dev/null 2>&1; then
gh release download -R "$REPO" -p "*darwin_arm64.tar.gz" -D "$tmp"
else
api="$tmp/latest.json"
github_curl "https://api.github.com/repos/${REPO}/releases/latest" "$api"
tag=$(sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p' "$api" | head -n 1)
[ -n "$tag" ] || return 1
ver=${tag#v}
github_curl "https://github.com/${REPO}/releases/download/${tag}/cans_${ver}_darwin_arm64.tar.gz" "$tmp/cans.tar.gz"
fi
archive=$(pick_archive "$tmp") || return 1
tar -xzf "$archive" -C "$tmp"
bin=""
if [ -f "$tmp/cans" ]; then
bin="$tmp/cans"
else
bin=$(find "$tmp" -type f -name cans | head -n 1)
fi
[ -n "$bin" ] && [ -f "$bin" ] || return 1
chmod 755 "$bin"
mv "$bin" "${DEST}/cans"
trap - EXIT
rm -rf "$tmp"
}
install_with_go() {
command -v go >/dev/null 2>&1 || return 1
go install "github.com/${REPO}/cmd/cans@main"
gobin=$(go env GOBIN)
[ -n "$gobin" ] || gobin="$(go env GOPATH)/bin"
[ -f "${gobin}/cans" ] || return 1
if [ "$gobin" != "$DEST" ]; then
cp "${gobin}/cans" "${DEST}/cans"
chmod 755 "${DEST}/cans"
fi
}
if download_release; then
:
elif install_with_go; then
echo "cans-install: installed from source (no GitHub release yet)" >&2
else
die "need a GitHub release (gh auth) or Go on PATH"
fi
echo "installed ${DEST}/cans"
case ":${PATH}:" in
*":${DEST}:"*) ;;
*)
echo "add ${DEST} to PATH" >&2
;;
esac
echo "next: cans doctor"