-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuninstall.sh
More file actions
executable file
·180 lines (160 loc) · 4.43 KB
/
uninstall.sh
File metadata and controls
executable file
·180 lines (160 loc) · 4.43 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
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
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="$ROOT_DIR/.venv"
LOCAL_BIN_DIR="${HOME}/.local/bin"
COMPLETION_DIR="${HOME}/.local/share/bash-completion/completions"
ENV_FILE="$ROOT_DIR/.env"
REMOVE_ENV=0
REMOVE_STATE=0
PURGE_VENV=0
YES=0
BASHRC_FILE="${HOME}/.bashrc"
BASHRC_MARKER_START="# >>> o2switch-cli >>>"
BASHRC_MARKER_END="# <<< o2switch-cli <<<"
COMPLETION_MARKER_START="# >>> o2switch-cli completion >>>"
COMPLETION_MARKER_END="# <<< o2switch-cli completion <<<"
remove_bashrc_block_by_markers() {
local start_marker="$1"
local end_marker="$2"
local tmp_file
if [[ ! -f "$BASHRC_FILE" ]] || ! grep -Fq "$start_marker" "$BASHRC_FILE"; then
return
fi
tmp_file="$(mktemp)"
awk -v start="$start_marker" -v end="$end_marker" '
$0 == start { skip = 1; next }
$0 == end { skip = 0; next }
skip != 1 { print }
' "$BASHRC_FILE" > "$tmp_file"
mv "$tmp_file" "$BASHRC_FILE"
}
remove_bashrc_path_block() {
if [[ ! -f "$BASHRC_FILE" ]] || ! grep -Fq "$BASHRC_MARKER_START" "$BASHRC_FILE"; then
return
fi
remove_bashrc_block_by_markers "$BASHRC_MARKER_START" "$BASHRC_MARKER_END"
echo "==> Updated $BASHRC_FILE"
echo " Removed managed o2switch-cli PATH block"
}
remove_bashrc_completion_block() {
if [[ ! -f "$BASHRC_FILE" ]] || ! grep -Fq "$COMPLETION_MARKER_START" "$BASHRC_FILE"; then
return
fi
remove_bashrc_block_by_markers "$COMPLETION_MARKER_START" "$COMPLETION_MARKER_END"
echo "==> Updated $BASHRC_FILE"
echo " Removed managed o2switch-cli completion block"
}
print_shell_refresh_note() {
echo "==> Reload shell configuration"
echo " Run: source \"$BASHRC_FILE\" && hash -r"
}
default_audit_log_path() {
if [[ -x "$VENV_DIR/bin/python" ]]; then
"$VENV_DIR/bin/python" - <<'PY'
from pathlib import Path
from platformdirs import user_state_dir
print(Path(user_state_dir("o2switch-cli")) / "audit.jsonl")
PY
else
printf '%s\n' "${HOME}/.local/state/o2switch-cli/audit.jsonl"
fi
}
usage() {
cat <<'EOF'
Usage: ./uninstall.sh [OPTIONS]
Remove the local o2switch-cli installation created by ./install.sh.
Options:
--purge-venv Also remove the repository .venv. By default it is kept for fast reinstall.
--purge-config Also remove the repository .env file.
--purge-state Also remove the default audit log file and its state directory.
--yes Do not ask for confirmation.
--help Show this message and exit.
EOF
}
while [[ $# -gt 0 ]]; do
case "$1" in
--purge-venv)
PURGE_VENV=1
shift
;;
--purge-config)
REMOVE_ENV=1
shift
;;
--purge-state)
REMOVE_STATE=1
shift
;;
--yes)
YES=1
shift
;;
--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 2
;;
esac
done
AUDIT_LOG_PATH="$(default_audit_log_path)"
STATE_DIR="$(dirname "$AUDIT_LOG_PATH")"
if [[ "$YES" -ne 1 ]]; then
echo "This will remove:"
echo " $LOCAL_BIN_DIR/o2switch-cli"
echo " $COMPLETION_DIR/o2switch-cli"
echo " $LOCAL_BIN_DIR/o2switch_cli (legacy alias if present)"
echo " $COMPLETION_DIR/o2switch_cli (legacy alias if present)"
if [[ "$PURGE_VENV" -eq 1 ]]; then
echo " $VENV_DIR"
else
echo " $VENV_DIR (preserved)"
fi
if [[ "$REMOVE_ENV" -eq 1 ]]; then
echo " $ENV_FILE"
fi
if [[ "$REMOVE_STATE" -eq 1 ]]; then
echo " $STATE_DIR"
fi
read -r -p "Continue? [y/N] " reply
case "${reply:-}" in
y|Y|yes|YES) ;;
*)
echo "Uninstall cancelled."
exit 0
;;
esac
fi
if [[ -x "$VENV_DIR/bin/o2switch-cli" ]]; then
if ! "$VENV_DIR/bin/o2switch-cli" completion remove --shell bash >/dev/null; then
rm -f "$COMPLETION_DIR/o2switch-cli" "$COMPLETION_DIR/o2switch_cli"
remove_bashrc_completion_block
fi
else
rm -f "$COMPLETION_DIR/o2switch-cli" "$COMPLETION_DIR/o2switch_cli"
remove_bashrc_completion_block
fi
if [[ "$PURGE_VENV" -eq 1 ]]; then
rm -rf "$VENV_DIR"
fi
for launcher in "$LOCAL_BIN_DIR/o2switch-cli" "$LOCAL_BIN_DIR/o2switch_cli"; do
if [[ -e "$launcher" || -L "$launcher" ]]; then
rm -f "$launcher"
fi
done
remove_bashrc_path_block
if [[ "$REMOVE_ENV" -eq 1 ]]; then
rm -f "$ENV_FILE"
fi
if [[ "$REMOVE_STATE" -eq 1 ]]; then
rm -rf "$STATE_DIR"
fi
echo "==> Uninstall completed"
if [[ "$PURGE_VENV" -eq 0 ]]; then
echo " Preserved $VENV_DIR for faster future installs"
fi
print_shell_refresh_note