Skip to content

Commit 5fe4e10

Browse files
committed
refactor: Move subcommands from single file to subcommands dir
1 parent dc9c71c commit 5fe4e10

File tree

4 files changed

+74
-68
lines changed

4 files changed

+74
-68
lines changed
Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,6 @@
11
# shellcheck shell=bash
22

3-
do_set() {
4-
local category="$1"
5-
local program="$2"
6-
local gui="$3"
7-
8-
# Get category
9-
if ! helper_get_category "$category" "$gui"; then
10-
log.die "$gui" "Could not get category"
11-
fi
12-
category="$REPLY"
13-
14-
# Get program
15-
if ! helper_get_program "$category" "$program" "$gui"; then
16-
log.die "$gui" "Could not get program"
17-
fi
18-
program="$REPLY"
19-
20-
# -------------------------- set ------------------------- #
21-
# Source category pre
22-
if [ -f "$db_dir/$category/set-pre.sh" ]; then
23-
if ! sh -e "$db_dir/$category/set-pre.sh" "$db_dir/$category" "$program"; then
24-
return 1
25-
fi
26-
fi
27-
28-
# Source program pre
29-
if [ -f "$db_dir/$category/$program/set-pre.sh" ]; then
30-
if ! sh -e "$db_dir/$category/$program/set-pre.sh" "$db_dir/$category" "$program"; then
31-
return 1
32-
fi
33-
fi
34-
35-
# Actually set
36-
printf "%s" "$program" >| "$db_dir/$category/_.current"
37-
38-
# Source program post
39-
if [ -f "$db_dir/$category/$program/set-post.sh" ]; then
40-
if ! sh -e "$db_dir/$category/$program/set-post.sh" "$db_dir/$category" "$program"; then
41-
return 1
42-
fi
43-
fi
44-
45-
# Source category post
46-
if [ -f "$db_dir/$category/set-post.sh" ]; then
47-
if ! sh -e "$db_dir/$category/set-post.sh" "$db_dir/$category" "$program"; then
48-
return 1
49-
fi
50-
fi
51-
52-
log.info "Category '$category' defaults to '$program'"
53-
}
54-
55-
do_launch() {
3+
choose-launch() {
564
local category="$1"
575
local gui="$2"
586

@@ -113,15 +61,3 @@ do_launch() {
11361
fi
11462
fi
11563
}
116-
117-
do_print() {
118-
local category="$1"
119-
120-
program="$(<"$db_dir/$category/_.current")"
121-
122-
# ensure variable (we already use 'ensure_has_dot_current' in
123-
# helper_get_category_filter; this is another safeguard)
124-
if [ -z "$program" ]; then
125-
log.die "$gui" "Program for '$category' is not set. Please set with 'choose set'"
126-
fi
127-
}

pkg/commands/choose-print.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# shellcheck shell=bash
2+
3+
choose-print() {
4+
local category="$1"
5+
6+
program="$(<"$db_dir/$category/_.current")"
7+
8+
# ensure variable (we already use 'ensure_has_dot_current' in
9+
# helper_get_category_filter; this is another safeguard)
10+
if [ -z "$program" ]; then
11+
log.die "$gui" "Program for '$category' is not set. Please set with 'choose set'"
12+
fi
13+
}

pkg/commands/choose-set.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# shellcheck shell=bash
2+
3+
choose-set() {
4+
local category="$1"
5+
local program="$2"
6+
local gui="$3"
7+
8+
# Get category
9+
if ! helper_get_category "$category" "$gui"; then
10+
log.die "$gui" "Could not get category"
11+
fi
12+
category="$REPLY"
13+
14+
# Get program
15+
if ! helper_get_program "$category" "$program" "$gui"; then
16+
log.die "$gui" "Could not get program"
17+
fi
18+
program="$REPLY"
19+
20+
# -------------------------- set ------------------------- #
21+
# Source category pre
22+
if [ -f "$db_dir/$category/set-pre.sh" ]; then
23+
if ! sh -e "$db_dir/$category/set-pre.sh" "$db_dir/$category" "$program"; then
24+
return 1
25+
fi
26+
fi
27+
28+
# Source program pre
29+
if [ -f "$db_dir/$category/$program/set-pre.sh" ]; then
30+
if ! sh -e "$db_dir/$category/$program/set-pre.sh" "$db_dir/$category" "$program"; then
31+
return 1
32+
fi
33+
fi
34+
35+
# Actually set
36+
printf "%s" "$program" >| "$db_dir/$category/_.current"
37+
38+
# Source program post
39+
if [ -f "$db_dir/$category/$program/set-post.sh" ]; then
40+
if ! sh -e "$db_dir/$category/$program/set-post.sh" "$db_dir/$category" "$program"; then
41+
return 1
42+
fi
43+
fi
44+
45+
# Source category post
46+
if [ -f "$db_dir/$category/set-post.sh" ]; then
47+
if ! sh -e "$db_dir/$category/set-post.sh" "$db_dir/$category" "$program"; then
48+
return 1
49+
fi
50+
fi
51+
52+
log.info "Category '$category' defaults to '$program'"
53+
}
54+

pkg/src/bin/choose.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,20 @@ main.choose() {
3030
local category="${argsCommands[1]}"
3131
local program="${argsCommands[2]}"
3232

33-
do_set "$category" "$program" "$gui"
33+
source "$BASALT_PACKAGE_DIR/pkg/commands/choose-set.sh"
34+
choose-set "$category" "$program" "$gui"
3435
;;
3536
launch)
3637
local category="${argsCommands[1]}"
3738

38-
do_launch "$category" "$gui"
39+
source "$BASALT_PACKAGE_DIR/pkg/commands/choose-launch.sh"
40+
choose-launch "$category" "$gui"
3941
;;
4042
print)
4143
local category="${argsCommands[1]}"
4244

43-
do_print "$category" "$gui"
45+
source "$BASALT_PACKAGE_DIR/pkg/commands/choose-print.sh"
46+
choose-print "$category" "$gui"
4447
;;
4548
*)
4649
log.die "Subcommand '${argsCommands[0]}' not found"

0 commit comments

Comments
 (0)