Skip to content

Commit de72858

Browse files
Shannon Boothshannonbooth
authored andcommitted
install: default to ~/.local/bin and preserve existing install path
`curl -fsSL https://polar.sh/install.sh | bash` could fail on machines without `/usr/local/bin` because the installer tried to move the binary there before ensuring the directory existed. For example, on my machine: ``` ==> Detecting platform... ==> Platform: darwin-arm64 ==> Fetching latest version... ==> Version: v1.3.6 ==> Downloading polar v1.3.6... ==> Verifying checksum... ==> Checksum verified ==> Extracting... ==> Installing to /usr/local/bin... mv: rename /var/folders/km/xvqr8_156mn6dbxj17fsvcy80000gn/T/tmp.PNE7BmJFn8/polar to /usr/local/bin/polar: No such file or directory ``` Instead, following XDG conventions, default to `~/.local/bin` for fresh installs, create the install directory before moving the binary, and reuse the directory of an existing `polar` binary on upgrades so we do not install a new copy that is shadowed earlier in `PATH`.
1 parent 3cc653d commit de72858

1 file changed

Lines changed: 85 additions & 7 deletions

File tree

install.sh

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
set -euo pipefail
33

44
REPO="polarsource/cli"
5-
INSTALL_DIR="/usr/local/bin"
5+
DEFAULT_INSTALL_DIR="${HOME}/.local/bin"
6+
INSTALL_DIR="${INSTALL_DIR:-}"
67
BINARY_NAME="polar"
78

89
# Colors
@@ -59,9 +60,89 @@ get_archive_name() {
5960
esac
6061
}
6162

63+
resolve_install_dir() {
64+
if [ -n "$INSTALL_DIR" ]; then
65+
echo "$INSTALL_DIR"
66+
return
67+
fi
68+
69+
local current_binary
70+
current_binary="$(type -P "$BINARY_NAME" || true)"
71+
if [ -n "$current_binary" ]; then
72+
dirname "$current_binary"
73+
return
74+
fi
75+
76+
echo "$DEFAULT_INSTALL_DIR"
77+
}
78+
79+
check_path() {
80+
local install_dir="$1"
81+
local shell_name
82+
83+
case ":${PATH}:" in
84+
*":${install_dir}:"*) ;;
85+
*)
86+
warn "${install_dir} is not in your PATH."
87+
echo ""
88+
89+
shell_name="$(basename "${SHELL:-}")"
90+
case "$shell_name" in
91+
zsh)
92+
echo " Add it by running:"
93+
echo " echo 'export PATH=\"${install_dir}:\$PATH\"' >> ~/.zshrc && source ~/.zshrc"
94+
;;
95+
bash)
96+
echo " Add it by running:"
97+
echo " echo 'export PATH=\"${install_dir}:\$PATH\"' >> ~/.bashrc && source ~/.bashrc"
98+
;;
99+
fish)
100+
echo " Add it by running:"
101+
echo " fish_add_path ${install_dir}"
102+
;;
103+
*)
104+
echo " Add ${install_dir} to your PATH to use the polar command."
105+
;;
106+
esac
107+
echo ""
108+
;;
109+
esac
110+
}
111+
112+
ensure_install_dir() {
113+
local dir="$1"
114+
115+
if [ -d "$dir" ]; then
116+
return
117+
fi
118+
119+
info "Creating install directory ${dir}..."
120+
if mkdir -p "$dir" 2>/dev/null; then
121+
return
122+
fi
123+
124+
sudo mkdir -p "$dir"
125+
}
126+
127+
install_binary() {
128+
local source_path="$1"
129+
local target_dir="$2"
130+
local target_path="${target_dir}/${BINARY_NAME}"
131+
132+
if [ -w "$target_dir" ]; then
133+
mv "$source_path" "$target_path"
134+
chmod +x "$target_path"
135+
else
136+
sudo mv "$source_path" "$target_path"
137+
sudo chmod +x "$target_path"
138+
fi
139+
}
140+
62141
main() {
63142
local platform version url
64143

144+
INSTALL_DIR="$(resolve_install_dir)"
145+
65146
info "Detecting platform..."
66147
platform="$(detect_platform)"
67148
info "Platform: ${platform}"
@@ -111,12 +192,8 @@ main() {
111192
esac
112193

113194
info "Installing to ${INSTALL_DIR}..."
114-
if [ -w "$INSTALL_DIR" ]; then
115-
mv "${tmpdir}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
116-
else
117-
sudo mv "${tmpdir}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
118-
fi
119-
chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
195+
ensure_install_dir "$INSTALL_DIR"
196+
install_binary "${tmpdir}/${BINARY_NAME}" "$INSTALL_DIR"
120197

121198
local tokens_file="${HOME}/.polar/tokens.json"
122199
if [ -f "$tokens_file" ]; then
@@ -125,6 +202,7 @@ main() {
125202

126203
info "Polar CLI ${version} installed successfully!"
127204
echo ""
205+
check_path "$INSTALL_DIR"
128206
echo " Run 'polar --help' to get started."
129207
echo ""
130208
}

0 commit comments

Comments
 (0)