Skip to content

Commit 46f6b57

Browse files
committed
feat: Add --force flag to 'remove'. Closes #62
This ensures that packages can be removed, even if the `bpm.toml` file cannot be parsed
1 parent e0532f2 commit 46f6b57

File tree

7 files changed

+84
-9
lines changed

7 files changed

+84
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ See [Installation](./docs/installation.md) and [Getting Started](./docs/getting-
3838
- Local and user-wide package installation
3939
- Configure (optionally) exactly which directories are used for completions, binaries, or man pages
4040
- Works with essentially all popular Bash projects out of the box
41-
- 240+ Tests
41+
- 250+ Tests
4242

4343
## Alternatives Comparison
4444

completions/_bpm.zsh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ case $state in
5959
_describe -t commands "gem subcommand" subcommandOptions
6060
;;
6161
(remove)
62-
local -a subcommandOptions=(--all)
62+
local -a subcommandOptions=(--all --force)
6363
_describe -t commands "gem subcommand" subcommandOptions
6464
;;
6565
(upgrade)

completions/bpm.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ _bpm() {
8080
readarray -t COMPREPLY < <(IFS=' ' compgen -W "${subcommandOptions[*]}" -- "$currentWord")
8181
;;
8282
remove)
83-
subcommandOptions=(--all)
83+
subcommandOptions=(--all --force)
8484
readarray -t COMPREPLY < <(IFS=' ' compgen -W "${subcommandOptions[*]}" -- "$currentWord")
8585
;;
8686
upgrade)

completions/bpm.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ set -l subcommandOptions
3333
complete -c $cmd -f -n "__fish_seen_subcommand_from $subcmd" -a "$subcommandOptions (bpm complete package-path)"
3434

3535
set subcmd remove
36-
set -l subcommandOptions --all
36+
set -l subcommandOptions --all --force
3737
complete -c $cmd -f -n "__fish_seen_subcommand_from $subcmd" -a "$subcommandOptions (bpm complete upgrade)"
3838

3939
set subcmd upgrade

pkg/lib/commands/do-remove.sh

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
do-remove() {
44
local flag_all='no'
5+
local flag_force='no'
56

67
util.setup_mode
78

@@ -11,6 +12,9 @@ do-remove() {
1112
--all)
1213
flag_all='yes'
1314
;;
15+
--force)
16+
flag_force='yes'
17+
;;
1418
-*)
1519
die "Flag '$arg' not recognized"
1620
;;
@@ -20,6 +24,10 @@ do-remove() {
2024
esac
2125
done
2226

27+
if [[ $flag_all == yes && $flag_force == yes ]]; then
28+
die "Flags '--all' and '--force' are mutually exclusive"
29+
fi
30+
2331
if [ "$flag_all" = yes ]; then
2432
local bpm_toml_file="$BPM_ROOT/bpm.toml"
2533

@@ -34,7 +42,16 @@ do-remove() {
3442
do-remove "$pkg"
3543
done
3644
else
37-
log.warn "No dependencies specified in 'dependencies' key"
45+
case "$?" in
46+
1)
47+
log.warn "No dependencies specified in 'dependencies' key"
48+
;;
49+
2)
50+
if [ "$flag_force" = 'no' ]; then
51+
exit 1
52+
fi
53+
;;
54+
esac
3855
fi
3956

4057
return
@@ -44,6 +61,10 @@ do-remove() {
4461
die "At least one package must be supplied"
4562
fi
4663

64+
if [ "$flag_force" = yes ] && (( ${#pkgs[@]} > 1 )); then
65+
die "Only one package may be specified when --force is passed"
66+
fi
67+
4768
for repoSpec in "${pkgs[@]}"; do
4869
util.extract_data_from_input "$repoSpec"
4970
local site="$REPLY2"
@@ -55,7 +76,13 @@ do-remove() {
5576
fi
5677

5778
if [ -d "$BPM_PACKAGES_PATH/$site/$package" ]; then
58-
do_actual_removal "$site/$package"
79+
if [ "$flag_force" = yes ]; then
80+
log.info "Force removing '$site/$package'"
81+
rm -rf "${BPM_PACKAGES_PATH:?}/$site/$package"
82+
do-prune
83+
else
84+
do_actual_removal "$site/$package"
85+
fi
5986
elif [ -e "$BPM_PACKAGES_PATH/$site/$package" ]; then
6087
rm -f "$BPM_PACKAGES_PATH/$site/$package"
6188
else
@@ -83,5 +110,4 @@ do_actual_removal() {
83110
:
84111
fi
85112
fi
86-
87113
}

pkg/lib/util/util.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ util.get_toml_array() {
206206
if [[ ${REPLIES[$i]} =~ $regex ]]; then
207207
REPLIES[$i]="${BASH_REMATCH[1]}"
208208
else
209-
die "Array for key '$keyName' not valid"
209+
log.error "Array for key '$keyName' not valid"
210+
return 2
210211
fi
211212
done
212213
else
213-
die "Key '$keyName' in file '$tomlFile' must be set to an array that spans one line"
214+
log.error "Key '$keyName' in file '$tomlFile' must be set to an array that spans one line"
215+
return 2
214216
fi
215217
}
216218

tests/do-remove.bats

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,50 @@ load 'util/init.sh'
196196
assert_failure
197197
assert_line -p "Refs must be omitted when removing packages. Remove ref '@v0.1.0'"
198198
}
199+
200+
@test "--force works" {
201+
local site='github.com'
202+
local pkg='username/package'
203+
204+
test_util.setup_pkg "$pkg"; {
205+
# Invalid because 'binDirs' must be an array
206+
echo 'binDirs = "somebin"' > 'bpm.toml'
207+
}; test_util.finish_pkg
208+
test_util.mock_clone "$pkg" "$site/$pkg"
209+
210+
assert [ -d "$BPM_PACKAGES_PATH/$site/$pkg" ]
211+
212+
run do-remove --force "$pkg"
213+
214+
assert_success
215+
assert_line -p -n 0 "Force removing '$site/$pkg'"
216+
assert_line -p -n 1 "Info: Pruning packages"
217+
218+
assert [ ! -d "$BPM_PACKAGES_PATH/$site/$pkg" ]
219+
}
220+
221+
@test "fail if give --all and --force flags" {
222+
local site='github.com'
223+
local pkg='username/package'
224+
225+
test_util.create_package "$pkg"
226+
test_util.mock_clone "$pkg" "$site/$pkg"
227+
228+
run do-remove --all --force
229+
230+
assert_failure
231+
assert_line -p "Flags '--all' and '--force' are mutually exclusive"
232+
}
233+
234+
@test "fail if give --force and more than one package" {
235+
local site='github.com'
236+
local pkg='username/package'
237+
238+
test_util.create_package "$pkg"
239+
test_util.mock_clone "$pkg" "$site/$pkg"
240+
241+
run do-remove --force "$pkg" "$pkg"
242+
243+
assert_failure
244+
assert_line -p "Only one package may be specified when --force is passed"
245+
}

0 commit comments

Comments
 (0)