Skip to content

Commit 13d062c

Browse files
authored
Improv: Create uninstall mode to uninstall tools (#49)
1 parent dd633a6 commit 13d062c

File tree

8 files changed

+178
-71
lines changed

8 files changed

+178
-71
lines changed

__tests__/utils/log.spec.zsh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests=(
2+
'. src/utils/log.zsh; LOG_NAMESPACE=test1; Log'
3+
'. src/utils/log.zsh; LOG_NAMESPACE=test1; Log hello'
4+
)
5+
6+
results=(
7+
'[test1] ' # TODO: This should be empty
8+
'[test1] hello'
9+
)

__tests__/utils/prompt.spec.zsh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# TODO: use expect to simulate user input
2+
3+
# tests=(
4+
# valid response
5+
# '. src/utils/prompt.zsh; valid_opts=("y" "N"); echo y | Prompt "Are you sure?" "${valid_opts[@]}"'
6+
# invalid response
7+
# '. src/utils/prompt.zsh; valid_opts=("yeah" "nah"); echo nope | Prompt "You sure bruv" "${valid_opts[@]}"'
8+
# )
9+
10+
# results=(
11+
# '"y" "true"'
12+
# '"nope" "false"'
13+
# )

package.sh

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@ package() {
1313
. ${testFile}
1414

1515
testsCount="${#tests[@]}"
16+
TEST_PASSED=true
1617

1718
for ((i = 0; i < ${testsCount}; i++)); do
18-
test_result=$(zsh -c "${tests[$i]}")
19+
test_result=$(zsh -c "CONSOLE='/dev/null'; CLI_CONFIG_ROOT='$(pwd)'; ${tests[$i]}")
20+
echo
1921

2022
if [ "${test_result}" != "${results[$i]}" ]; then
21-
echo TEST FAILED: "zsh -c '${tests[$i]}'"
22-
echo "'${test_result}' != '${results[$i]}'"
23-
echo
23+
echo -n "🙅‍♂️ "
24+
TEST_PASSED=false
2425
ANY_TESTS_FAILED=1
26+
else
27+
echo -n ""
2528
fi
29+
30+
echo "$ zsh -c \"CLI_CONFIG_ROOT=$(pwd) ${tests[$i]}\""
31+
[ "${VERBOSE}" = '1' ] && [ "${TEST_PASSED}" == 'false' ] && echo "TEST FAILED: zsh -c '${tests[$i]}'" && echo "'${test_result}' != '${results[$i]}'"
2632
done
2733
done
2834

src/defs/install.zsh

-66
Original file line numberDiff line numberDiff line change
@@ -3,72 +3,6 @@
33
. ${CLI_CONFIG_ROOT}/src/utils/array.zsh
44

55
install() {
6-
# DEFAULT OPTIONS
7-
CCOPT_PROFILE=default
8-
CCOPT_NO_SUDO='sudo'
9-
CCOPT_DEBIAN_FRONTEND=''
10-
CCOPT_TOOLS=('antigen' 'ohmyposh' 'nvm' 'pyenv' 'dotnet' 'tfenv' 'gvm')
11-
12-
# READ OPTIONS
13-
# Todo: Move to separate file
14-
while [[ $# -gt 0 ]]; do
15-
case $1 in
16-
-h | --help)
17-
. $CLI_CONFIG_ROOT/src/defs/usage.zsh
18-
usage
19-
exit
20-
;;
21-
-c | --clean)
22-
CCOPT_CLEAN=true
23-
shift
24-
;;
25-
-p | --profile)
26-
profiles=($(ls -1 $CLI_CONFIG_ROOT/profiles))
27-
CCOPT_PROFILE=$2
28-
if [ "$(array_contains "$CCOPT_PROFILE" "${profiles[@]}")" = "false" ]; then
29-
echo "The specified profile '$CCOPT_PROFILE' does not exist. "
30-
echo "Available profiles: $(array_str ' | ' ${profiles[@]})"
31-
exit
32-
fi
33-
shift
34-
shift
35-
;;
36-
-n | --no-sudo)
37-
CCOPT_NO_SUDO=''
38-
shift
39-
;;
40-
-t | --tools)
41-
allowedPrograms=($(ls -1 $CLI_CONFIG_ROOT/src/installers | sed 's/\..*$//g' | sort | uniq))
42-
43-
if [ "$2" = "" ]; then
44-
Log "\"-t|--tools\" No tool selected, skipping installation."
45-
exit
46-
fi
47-
48-
CCOPT_TOOLS=($(echo $2 | sed 's/,/\n/g'))
49-
invalidPrograms=()
50-
for program in "${CCOPT_TOOLS[@]}"; do
51-
(! test -f $CLI_CONFIG_ROOT/src/installers/${program}.install.zsh) && invalidPrograms+=($program)
52-
done
53-
54-
if [ "${#invalidPrograms[@]}" -ne 0 ]; then
55-
Log "\"-t|--tools\" Found invalid tools, skipping installation - '$(array_str ',' ${invalidPrograms[@]})'"
56-
exit
57-
fi
58-
shift
59-
shift
60-
;;
61-
--ci)
62-
CCOPT_DEBIAN_FRONTEND='DEBIAN_FRONTEND=noninteractive'
63-
shift
64-
;;
65-
*)
66-
# Unrecognized option
67-
shift
68-
;;
69-
esac
70-
done
71-
726
# START INSTALL
737
Log "Starting install..."
748

src/defs/main.zsh

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@
33
. $CLI_CONFIG_ROOT/src/defs/usage.zsh
44
. $CLI_CONFIG_ROOT/src/utils/log.zsh
55
. $CLI_CONFIG_ROOT/src/defs/prereqs.zsh
6+
. $CLI_CONFIG_ROOT/src/utils/read-options.zsh
67

78
main() {
89
mode=$1
910

1011
prereqs "$@"
1112

1213
# check if mode is valid
13-
modes=('install' 'configure')
14+
modes=('install' 'configure' 'uninstall')
1415
IS_MODE_VALID='false'
1516
for i in ${modes[@]}; do
1617
if [ "${i}" = "${mode}" ]; then
@@ -31,6 +32,7 @@ main() {
3132

3233
. $CLI_CONFIG_ROOT/src/defs/${mode}.zsh
3334

35+
read_options "$@"
3436
$mode "$@"
3537
fi
3638
}

src/defs/uninstall.zsh

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
. ${CLI_CONFIG_ROOT}/src/utils/array.zsh
2+
. ${CLI_CONFIG_ROOT}/src/utils/log.zsh
3+
. ${CLI_CONFIG_ROOT}/src/utils/prompt.zsh
4+
5+
uninstall() {
6+
responseYesToAll=false
7+
confirmationOptions=('y' 'N' 'a')
8+
9+
for tool in "${CCOPT_TOOLS[@]}"; do
10+
if [ "${responseYesToAll}" = "false" ]; then
11+
while true; do
12+
response=($(echo $(Prompt "Are you sure you want to uninstall '${tool}'?" true "${confirmationOptions[@]}")))
13+
receivedValidResponse="${response[2]}"
14+
15+
enteredOption=$(echo "${response[1]}" | tr 'A-Z' 'a-z')
16+
17+
if [ "${enteredOption}" = "a" ]; then
18+
responseYesToAll=true
19+
break
20+
elif [ "${enteredOption}" = "" ] || [ "${receivedValidResponse}" = "true" ]; then
21+
break
22+
fi
23+
done
24+
else
25+
enteredOption="a"
26+
fi
27+
28+
if [ "${enteredOption}" = "" ] || [ "${enteredOption}" = "n" ]; then
29+
Log "Skipping uninstall of ${tool}."
30+
continue
31+
fi
32+
33+
if [ "${enteredOption}" = "y" ] || [ "${enteredOption}" = "a" ]; then
34+
Log "Running uninstall script for '${tool}'..."
35+
uninstallScriptPath="${CLI_CONFIG_ROOT}/src/installers/${tool}.uninstall.zsh"
36+
if [ -f "${uninstallScriptPath}" ]; then
37+
. ${uninstallScriptPath}
38+
else
39+
rm -rf ${CLI_CONFIG_ROOT}/current/${tool} 2>/dev/null
40+
rm -rf ${CLI_CONFIG_ROOT}/current/conf/${tool}.conf.sh
41+
fi
42+
Log "Done."
43+
fi
44+
done
45+
}

src/utils/prompt.zsh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
. ${CLI_CONFIG_ROOT}/src/utils/array.zsh
2+
3+
Prompt() {
4+
if [ "${CONSOLE}" = '' ]; then
5+
console='/dev/tty'
6+
else
7+
console="${CONSOLE}"
8+
fi
9+
10+
message=$1
11+
shift
12+
caseInsensitive=$1
13+
shift
14+
valid_opts=("$@")
15+
16+
valid_opts_str=$(array_str "/" "${valid_opts[@]}")
17+
echo -n "${message} (${valid_opts_str}) " >$console
18+
read response
19+
20+
if [ "${caseInsensitive}" = "true" ]; then
21+
valid_opts_lowered=()
22+
for opt in "${valid_opts[@]}"; do valid_opts_lowered+=($(echo "${opt}" | tr 'A-Z' 'a-z')); done
23+
responseLowered=$(echo "${response}" | tr 'A-Z' 'a-z')
24+
isValid=$(array_contains "${responseLowered}" "${valid_opts_lowered[@]}")
25+
else
26+
isValid=$(array_contains "${response}" "${valid_opts[@]}")
27+
fi
28+
29+
response="${response} \0 ${isValid}"
30+
echo "${response}"
31+
}

src/utils/read-options.zsh

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
read_options() {
2+
# DEFAULT OPTIONS
3+
CCOPT_PROFILE=default
4+
CCOPT_NO_SUDO='sudo'
5+
CCOPT_DEBIAN_FRONTEND=''
6+
CCOPT_TOOLS=('antigen' 'ohmyposh' 'nvm' 'pyenv' 'dotnet' 'tfenv' 'gvm')
7+
8+
# READ OPTIONS
9+
# Todo: Move to separate file
10+
while [[ $# -gt 0 ]]; do
11+
case $1 in
12+
-h | --help)
13+
. $CLI_CONFIG_ROOT/src/defs/usage.zsh
14+
usage
15+
exit
16+
;;
17+
-c | --clean)
18+
CCOPT_CLEAN=true
19+
shift
20+
;;
21+
-p | --profile)
22+
profiles=($(ls -1 $CLI_CONFIG_ROOT/profiles))
23+
CCOPT_PROFILE=$2
24+
if [ "$(array_contains "$CCOPT_PROFILE" "${profiles[@]}")" = "false" ]; then
25+
echo "The specified profile '$CCOPT_PROFILE' does not exist. "
26+
echo "Available profiles: $(array_str ' | ' ${profiles[@]})"
27+
exit
28+
fi
29+
shift
30+
shift
31+
;;
32+
-n | --no-sudo)
33+
CCOPT_NO_SUDO=''
34+
shift
35+
;;
36+
-t | --tools)
37+
allowedPrograms=($(ls -1 $CLI_CONFIG_ROOT/src/installers | sed 's/\..*$//g' | sort | uniq))
38+
39+
if [ "$2" = "" ]; then
40+
Log "\"-t|--tools\" No tool selected, skipping installation."
41+
exit
42+
fi
43+
44+
CCOPT_TOOLS=($(echo $2 | sed 's/,/\n/g'))
45+
invalidPrograms=()
46+
for program in "${CCOPT_TOOLS[@]}"; do
47+
(! test -f $CLI_CONFIG_ROOT/src/installers/${program}.install.zsh) && invalidPrograms+=($program)
48+
done
49+
50+
if [ "${#invalidPrograms[@]}" -ne 0 ]; then
51+
Log "\"-t|--tools\" Found invalid tools, skipping installation - '$(array_str ',' ${invalidPrograms[@]})'"
52+
exit
53+
fi
54+
shift
55+
shift
56+
;;
57+
--ci)
58+
CCOPT_DEBIAN_FRONTEND='DEBIAN_FRONTEND=noninteractive'
59+
shift
60+
;;
61+
*)
62+
# Unrecognized option
63+
shift
64+
;;
65+
esac
66+
done
67+
}

0 commit comments

Comments
 (0)