-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_from_github.sh
executable file
·291 lines (257 loc) · 8.62 KB
/
install_from_github.sh
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/bin/bash
# abc/install_from_github.sh - Install pipx and use to install abc from GitHub
set -e # Exit on error
set -u # Exit on undefined variable
# Default values
NO_PROMPT=false
NO_PROMPT_OPTION=
FORCE=false
FORCE_OPTION=
# List of providers to install/upgrade
PROVIDERS=(
"anthropic"
"aws_bedrock"
)
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
--no-prompt)
NO_PROMPT=true
NO_PROMPT_OPTION="--no-prompt"
shift
;;
--force)
FORCE=true
FORCE_OPTION="--force"
shift
;;
*)
error "Unknown argument: $1"
;;
esac
done
# Logging setup
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [abc] [INFO] $1"
}
error() {
echo "$(date '+%Y-%m-%d %H:%M:%S') [abc] [ERROR] $1" >&2
exit 1
}
is_interactive() {
# Check if stderr is connected to a terminal
# This works even when stdin is a pipe
[[ -t 2 ]]
}
prompt_user() {
local message=$1
local default=${2:-"yes"} # Default to yes if not specified
# Skip prompt if --no-prompt flag is set or not interactive
if $NO_PROMPT || ! is_interactive; then
echo "$default"
return
fi
# Prompt user on stderr to avoid pipe interference
local prompt
if [ "$default" = "yes" ]; then
prompt=" [YES/no/stop] "
else
prompt=" [yes/NO/stop] "
fi
echo -n "$message$prompt" >&2
read -r response </dev/tty # Read directly from terminal
# Add newline since we used echo -n above
echo >&2
response=${response,,} # Convert to lowercase
if [[ -z "$response" ]]; then
echo "$default"
elif [[ "$response" =~ ^(yes|y)$ ]]; then
echo "yes"
elif [[ "$response" =~ ^(no|n)$ ]]; then
echo "no"
else
log "Installation stopped by user"
exit 1 # Exit with error to prevent "Installation completed successfully!"
fi
}
# Helper function to show instructions and get confirmation
show_instructions_and_confirm() {
local description=$1
local commands=$2
local requires_sudo=${3:-false}
local default=${4:-"yes"}
echo
echo "$description"
if [[ "$requires_sudo" == true ]]; then
echo "(requires sudo access)"
fi
echo
echo "Commands to run:"
echo "---------------"
echo "$commands"
echo
local response
response=$(prompt_user "Would you like me to perform these changes? (\"no\" means you have done them)" "$default")
if [[ "$response" = "yes" ]]; then
return 0
elif [[ "$response" = "no" ]]; then
return 1
else
# Should never get here since prompt_user exits on stop
exit 1
fi
}
install_pipx() {
local package_manager=""
local install_command=""
local requires_sudo=false
local extra_command=""
local commands=""
# Detect OS and set up commands
if [[ "$OSTYPE" == "darwin"* ]]; then
if command -v brew &> /dev/null; then
package_manager="Homebrew"
commands="brew install pipx"
else
error "Homebrew not found. Please install Homebrew first."
fi
elif command -v apt &> /dev/null; then
package_manager="apt"
commands="sudo apt update\nsudo apt install -y pipx"
requires_sudo=true
extra_command="sudo apt update"
install_command="sudo apt install -y pipx"
elif command -v dnf &> /dev/null; then
package_manager="dnf"
commands="sudo dnf install -y pipx"
requires_sudo=true
install_command="sudo dnf install -y pipx"
elif command -v pacman &> /dev/null; then
package_manager="pacman"
commands="sudo pacman -S --noconfirm python-pipx"
requires_sudo=true
install_command="sudo pacman -S --noconfirm python-pipx"
elif command -v yum &> /dev/null; then
package_manager="yum"
commands="sudo yum install -y python3-pip\npython3 -m pip install --user pipx"
requires_sudo=true
install_command="sudo yum install -y python3-pip && python3 -m pip install --user pipx"
else
package_manager="pip"
commands="python3 -m pip install --user pipx"
install_command="python3 -m pip install --user pipx"
fi
commands+="\npipx ensurepath"
# Show instructions and get confirmation
if show_instructions_and_confirm \
"About to install pipx using $package_manager" \
"$(echo -e "$commands")" \
"$requires_sudo" \
"yes"; then
# Execute commands
if [[ -n "$extra_command" ]]; then
eval "$extra_command" || error "Failed to update package database"
fi
eval "$install_command" || error "Failed to install pipx"
fi
}
install_abc() {
local commands="pipx install git+https://github.com/alestic/abc.git"
commands+="\n\n# Then install providers:"
for provider in "${PROVIDERS[@]}"; do
commands+="\npipx inject abc-cli abc-provider-${provider}@git+https://github.com/alestic/abc.git#subdirectory=abc_provider_${provider}"
done
# Show instructions and get confirmation
if show_instructions_and_confirm \
"About to install abc and providers" \
"$(echo -e "$commands")" \
"false" \
"yes"; then
# Execute commands
output=$(pipx install git+https://github.com/alestic/abc.git $FORCE_OPTION 2>&1)
echo "$output"
if [ $? -eq 0 ]; then
log "Installation successful"
elif echo "$output" | grep -q "installed package abc-cli"; then
log "Installation successful despite pipx warning"
else
error "Failed to install abc via pipx"
fi
# Install providers
for provider in "${PROVIDERS[@]}"; do
log "Installing ${provider} provider..."
output=$(pipx inject abc-cli abc-provider-${provider}@git+https://github.com/alestic/abc.git#subdirectory=abc_provider_${provider} $FORCE_OPTION 2>&1)
echo "$output"
if [ $? -ne 0 ] && ! echo "$output" | grep -q "injected package"; then
error "Failed to install ${provider} provider"
fi
done
fi
}
upgrade_abc() {
local commands="pipx upgrade abc-cli"
commands+="\n\n# Then upgrade providers:"
for provider in "${PROVIDERS[@]}"; do
commands+="\npipx inject abc-cli abc-provider-${provider}@git+https://github.com/alestic/abc.git#subdirectory=abc_provider_${provider}"
done
# Show instructions and get confirmation
if show_instructions_and_confirm \
"About to upgrade abc and providers" \
"$(echo -e "$commands")" \
"false" \
"yes"; then
# Execute commands
output=$(pipx upgrade abc-cli 2>&1)
echo "$output"
if [ $? -eq 0 ]; then
log "Upgrade successful"
elif echo "$output" | grep -q "upgraded package abc-cli"; then
log "Upgrade successful despite pipx warning"
else
error "Failed to upgrade abc"
fi
# Upgrade providers
for provider in "${PROVIDERS[@]}"; do
log "Upgrading ${provider} provider..."
output=$(pipx inject abc-cli abc-provider-${provider}@git+https://github.com/alestic/abc.git#subdirectory=abc_provider_${provider} $FORCE_OPTION 2>&1)
echo "$output"
if [ $? -ne 0 ] && ! echo "$output" | grep -q "injected package"; then
error "Failed to upgrade ${provider} provider"
fi
done
fi
}
main() {
log "Starting abc installation from GitHub..."
# Check if pipx is already installed
if command -v pipx &> /dev/null; then
log "pipx is already installed"
else
log "pipx not found. Installation required."
install_pipx
fi
# Ensure pipx is in PATH
log "Ensuring pipx apps are in PATH..."
pipx ensurepath || error "Failed to ensure pipx apps are in PATH"
# Handle abc installation/upgrade
if pipx list 2>/dev/null | grep -q "abc-cli"; then
log "abc is already installed, attempting upgrade..."
upgrade_abc
else
log "Installing abc from GitHub..."
install_abc
fi
# Run abc setup in a login shell to get updated PATH
log "Running abc setup..."
if show_instructions_and_confirm \
"About to run abc setup" \
"bash -l -c \"abc_setup $NO_PROMPT_OPTION\"" \
"false" \
"yes"; then
bash -l -c "abc_setup $NO_PROMPT_OPTION" || error "Failed to run abc setup"
fi
log "Installation completed successfully!"
}
# Handle interrupts gracefully
trap 'echo -e "\nInstallation cancelled by user"; exit 1' INT
main "$@"