-
Notifications
You must be signed in to change notification settings - Fork 4
/
install
128 lines (110 loc) · 3.16 KB
/
install
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
dimmed=$'\e[2m'
bold=$(tput bold)
cyan=$'\e[96m'
normal=$(tput sgr0)
SCRIPT_NAME="ks"
SCRIPT_DOWNLOAD_URL="https://raw.githubusercontent.com/loteoo/ks/main/ks"
REQUIRES_RESTART="false"
SYSTEM_BIN_PATH="/usr/local/bin"
LOCAL_BIN_PATH="$HOME/.local/bin"
KEYCHAIN="${KS_DEFAULT_KEYCHAIN:-Secrets}"
KEYCHAIN_FILE="$KEYCHAIN.keychain"
info() {
# shellcheck disable=SC2145
echo "${dimmed}$@${normal}" 1>&2
}
success() {
# shellcheck disable=SC2145
echo "${cyan}==> $@${normal}"
}
throw() {
echo "$@" 1>&2
exit 1
}
yn() {
read -r -n 1 -p "$1 [y/n]: " yn
echo
if [[ "$yn" != [Yy]* ]]; then
return 1
fi
}
# Validate macOS
if [[ "$OSTYPE" != "darwin"* ]]; then
throw "Unfortunately $SCRIPT_NAME is for macOS only as it relies on it's native keychain features. Installation aborted."
else
info "✓ On macOS."
fi
# Validate security command
if ! command -v security &> /dev/null; then
throw "\"security\" command not available. Installation aborted."
else
info "✓ \"security\" is available."
fi
# Warn reinstall
if command -v $SCRIPT_NAME &> /dev/null; then
if ! yn "$SCRIPT_NAME already exists. Remove and reinstall?"; then
throw "Installation aborted."
fi
existing="$(which $SCRIPT_NAME)"
rm "$existing"
success "Removed old install."
fi
echo "Select install location:
1) Local: For $USER user only. Location: $LOCAL_BIN_PATH.
2) System: For whole system. Location: $SYSTEM_BIN_PATH. Requires sudo."
read -r -n 1 -p "Pick one: " num
echo
case "$num" in
1) BIN_PATH="$LOCAL_BIN_PATH";;
2) BIN_PATH="$SYSTEM_BIN_PATH";;
*) throw "Invalid choice.";;
esac
# Create bin directory if needed.
if [[ ! -d "$BIN_PATH" ]]; then
if ! yn "Need to create $BIN_PATH. Continue?"; then
throw "Installation aborted."
fi
mkdir -p "$BIN_PATH"
success "Created $BIN_PATH folder."
else
info "✓ Directory exists."
fi
# Add to bin directory to $PATH if needed.
if ! echo "$PATH" | tr ':' $'\n' | grep -q "$BIN_PATH"; then
if ! yn "$BIN_PATH has not been added to \$PATH. Add to path? (requires sudo privileges)"; then
throw "Installation aborted."
fi
sudo bash -c "echo '$BIN_PATH' >> /etc/paths"
REQUIRES_RESTART="true"
success "$BIN_PATH was added to \$PATH."
else
info "✓ Directory is in \$PATH."
fi
SCRIPT_PATH="$BIN_PATH/$SCRIPT_NAME"
info "Downloading $SCRIPT_DOWNLOAD_URL..."
curl -fsSL "$SCRIPT_DOWNLOAD_URL" -o "$SCRIPT_PATH"
success "Script added under $SCRIPT_PATH"
if [[ ! -x "$SCRIPT_PATH" ]]; then
chmod +x "$SCRIPT_PATH"
success "Script made executable."
else
info "✓ Script is executable."
fi
if [[ "$REQUIRES_RESTART" == "true" ]]; then
echo "${bold}Done! 🎉${normal}"
echo "Please restart your terminal to make $SCRIPT_NAME available, then run \"$SCRIPT_NAME init\"."
info "Give $SCRIPT_NAME it a star on Github if you like it! 🙏"
exit
fi
if ! security show-keychain-info "$KEYCHAIN_FILE" > /dev/null 2>&1; then
$SCRIPT_NAME init
else
info "✓ Keychain \"$KEYCHAIN\" exists."
fi
info "Give $SCRIPT_NAME it a star on Github if you like it! 🙏"
echo "${bold}Installation completed! 🎉${normal}"
info "Running \"$SCRIPT_NAME help\"."
eval "$SCRIPT_NAME help"