-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·133 lines (116 loc) · 3.61 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·133 lines (116 loc) · 3.61 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
#!/bin/sh
# Build git_pruner and install it onto the user's PATH. Run with --help for flags.
set -eu
BINARY=git_pruner
BINDIR=${BINDIR:-}
usage() {
cat <<EOF
Usage: ./install.sh [--bindir DIR]
Builds $BINARY and installs it to a directory on your PATH.
Options:
--bindir DIR Install into DIR (also settable via the \$BINDIR environment
variable). Relative paths resolve against the current
directory.
-h, --help Show this help.
Without --bindir, the first usable of ~/.local/bin or ~/bin wins, falling back
to /usr/local/bin — which needs sudo on most systems.
EOF
}
while [ $# -gt 0 ]; do
case $1 in
--bindir)
[ $# -ge 2 ] || { printf '%s\n' "install.sh: --bindir needs a directory" >&2; exit 2; }
BINDIR=$2
shift 2
;;
--bindir=*)
BINDIR=${1#--bindir=}
shift
;;
-h | --help)
usage
exit 0
;;
*)
printf '%s\n' "install.sh: unknown argument: $1" >&2
usage >&2
exit 2
;;
esac
done
command -v go >/dev/null 2>&1 || {
printf '%s\n' "install.sh: go is not on PATH (see https://go.dev/dl/)" >&2
exit 1
}
# git_pruner shells out to git for every operation, so a missing git makes the
# installed binary useless.
command -v git >/dev/null 2>&1 || {
printf '%s\n' "install.sh: git is not on PATH" >&2
exit 1
}
# Usable means writable without sudo: the directory is writable, or it is
# missing and the nearest existing ancestor is writable (mkdir -p creates the
# rest). Clobbers _dir/_parent, since POSIX sh has no `local`.
dir_is_usable() {
_dir=$1
while [ ! -d "$_dir" ]; do
_parent=$(dirname "$_dir")
if [ "$_parent" = "$_dir" ]; then
return 1 # walked up to / without finding an existing ancestor
fi
_dir=$_parent
done
[ -w "$_dir" ]
}
# HOME is unset in some headless and sudo environments, and set -u would abort
# on "$HOME"; skipping straight to the fallback is the right answer there.
if [ -z "$BINDIR" ] && [ -n "${HOME:-}" ]; then
for candidate in "$HOME/.local/bin" "$HOME/bin"; do
if dir_is_usable "$candidate"; then
BINDIR=$candidate
break
fi
done
fi
# No user-owned directory was usable: fall back to the conventional system
# location and let the sudo path below deal with the permissions.
[ -n "$BINDIR" ] || BINDIR=/usr/local/bin
# Resolve a relative BINDIR against the caller's cwd before the cd below moves us.
case $BINDIR in
/*) ;;
*) BINDIR=$PWD/$BINDIR ;;
esac
# Build from the repo root regardless of the caller's cwd; the build must run
# inside the git checkout for Go's VCS stamping (git_pruner version). CDPATH is
# cleared because an exported one would redirect this cd.
# shellcheck disable=SC1007 # the empty CDPATH= is a deliberate command prefix
CDPATH= cd -- "$(dirname -- "$0")"
tmpdir=$(mktemp -d)
# POSIX signal traps resume execution afterwards, so the signal handlers have to
# exit themselves; EXIT then does the cleanup exactly once.
trap 'rm -rf "$tmpdir"' EXIT
trap 'exit 130' INT
trap 'exit 129' HUP
trap 'exit 143' TERM
printf '%s\n' "Building $BINARY..."
go build -o "$tmpdir/$BINARY" .
SUDO=
if ! dir_is_usable "$BINDIR"; then
command -v sudo >/dev/null 2>&1 || {
printf '%s\n' "install.sh: $BINDIR is not writable and sudo is unavailable" >&2
exit 1
}
SUDO=sudo
printf '%s\n' "$BINDIR needs elevated permissions; using sudo."
fi
# Unquoted on purpose: empty $SUDO must expand to zero words, not an empty one.
$SUDO mkdir -p "$BINDIR"
$SUDO install -m 0755 "$tmpdir/$BINARY" "$BINDIR/$BINARY"
printf '%s\n' "Installed $BINDIR/$BINARY"
case ":$PATH:" in
*":$BINDIR:"*) ;;
*)
printf '\n%s\n' "Warning: $BINDIR is not on your PATH. Add this to your shell profile:"
printf '%s\n' " export PATH=\"$BINDIR:\$PATH\""
;;
esac