-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_from_github.sh
executable file
·187 lines (159 loc) · 5.44 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
#!/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=
# 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:-"y"} # Default to yes
# Skip prompt if --no-prompt flag is set or not interactive
if $NO_PROMPT || ! is_interactive; then
if [ "$default" = "y" ]; then
echo "yes"
else
echo "no"
fi
return
fi
# Prompt user on stderr to avoid pipe interference
local prompt
if [ "$default" = "y" ]; then
prompt=" [Y/n] "
else
prompt=" [y/N] "
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
response=$default
fi
if [[ "$response" =~ ^(yes|y)$ ]]; then
echo "yes"
else
echo "no"
fi
}
install_pipx() {
# Detect OS
if [[ "$OSTYPE" == "darwin"* ]]; then
log "Detected macOS - Installing pipx via Homebrew..."
if command -v brew &> /dev/null; then
if [[ "$(prompt_user "About to install pipx via Homebrew. Continue?" "y")" = "yes" ]]; then
brew install pipx || error "Failed to install pipx via Homebrew"
else
log "Installation cancelled by user"
exit 0
fi
else
error "Homebrew not found. Please install Homebrew first."
fi
elif command -v apt &> /dev/null; then
log "Detected Debian/Ubuntu - Installing pipx via apt..."
if [[ "$(prompt_user "About to install pipx via apt. This requires sudo access. Continue?" "y")" = "yes" ]]; then
sudo apt update || error "Failed to update apt"
sudo apt install -y pipx || error "Failed to install pipx via apt"
else
log "Installation cancelled by user"
exit 0
fi
elif command -v dnf &> /dev/null; then
log "Detected RedHat/Fedora - Installing pipx via dnf..."
if [[ "$(prompt_user "About to install pipx via dnf. This requires sudo access. Continue?" "y")" = "yes" ]]; then
sudo dnf install -y pipx || error "Failed to install pipx via dnf"
else
log "Installation cancelled by user"
exit 0
fi
elif command -v pacman &> /dev/null; then
log "Detected Arch Linux - Installing pipx via pacman..."
if [[ "$(prompt_user "About to install pipx via pacman. This requires sudo access. Continue?" "y")" = "yes" ]]; then
sudo pacman -S --noconfirm python-pipx || error "Failed to install pipx via pacman"
else
log "Installation cancelled by user"
exit 0
fi
elif command -v yum &> /dev/null; then
log "Detected older RedHat/CentOS - Installing pipx via yum..."
if [[ "$(prompt_user "About to install pipx via yum. This requires sudo access. Continue?" "y")" = "yes" ]]; then
sudo yum install -y python3-pip || error "Failed to install python3-pip via yum"
python3 -m pip install --user pipx || error "Failed to install pipx via pip"
else
log "Installation cancelled by user"
exit 0
fi
else
log "Unable to detect package manager. Installing pipx via pip..."
if [[ "$(prompt_user "About to install pipx via pip. Continue?" "y")" = "yes" ]]; then
python3 -m pip install --user pipx || error "Failed to install pipx via pip"
else
log "Installation cancelled by user"
exit 0
fi
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 -q || error "Failed to ensure pipx apps are in PATH"
# Install abc
log "Installing abc from GitHub..."
if [[ "$(prompt_user "About to install abc via pipx. Continue?" "y")" = "yes" ]]; then
pipx install git+https://github.com/alestic/abc.git $FORCE_OPTION || error "Failed to install abc via pipx"
else
log "Installation cancelled by user"
exit 0
fi
# Run abc setup
log "Running abc setup..."
abc_setup $NO_PROMPT_OPTION || error "Failed to run abc setup"
log "Installation completed successfully!"
}
# Handle interrupts gracefully
trap 'echo -e "\nInstallation cancelled by user"; exit 1' INT
main "$@"