Skip to content

Commit ce00c52

Browse files
committed
Refactored base_wrapper function into an executable called 'base-wrapper' that would be used in the shebang line of Base scripts
The contract that a base CLI needs to adhere to is this: 1. Use "#!/usr/bin/env base-wrapper" as the shebang line 2. Have three functions: base_describe - To print one line description of what the script does base_help - To show help main - The entry point to the script base-wrapper gives these features out of the box: - automatically handles -h / --help / -help option to retrieve help text - automatically handles --describe / -describe option to retrieve one line summary of the script - automatically handles --debug option to enable debugging - imports lib/stdlib.sh More helper functions and automatic options may be added in the future.
1 parent 7814209 commit ce00c52

File tree

7 files changed

+82
-60
lines changed

7 files changed

+82
-60
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ In a typical setting, `.bashrc` sources in `$BASE_HOME/base_init.sh` which does
8080
# **Functions exported by base_init.sh**
8181

8282
* import - sources in libraries from any place under `BASE_HOME` directory
83-
* base_wrapper - initializes Base inside scripts
8483
* base_update - does a `git pull` on Base git directory; add it to `user/<user>.sh` to "auto update" Base
8584

8685
# **FAQ**

base_init.sh

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ base_deactivate() {
6262
unset _old_PATH _old_PS1 _old_vars_saved _old_BASE_HOME
6363
unset BASE_OS BASE_HOST BASE_DEBUG BASE_SOURCES
6464
unset -f check_bash_version do_init base_debug base_error set_base_home source_it \
65-
import_libs_and_profiles base_update base_wrapper base_main \
65+
import_libs_and_profiles base_update base_main \
6666
base_deactivate
6767
unset __base_init_sourced__
6868
fi
@@ -186,48 +186,6 @@ base_update() (
186186
}
187187
)
188188

189-
#
190-
# base_wrapper
191-
#
192-
# This function is meant to be called by scripts that are built on top of base.
193-
# base_wrapper is exported so that it is visible to sub processes started from the login shell.
194-
# It discovers base_init and sources it. It also looks at the command line arguments and interprets a few of those,
195-
# like --debug. It calls the main function the modified argument list. The main function is expected to be defined by
196-
# the calling script.
197-
#
198-
base_wrapper() {
199-
local grab_debug=0 arg args script
200-
[[ $1 = "-d" ]] && { grab_debug=1; shift; }
201-
[[ $BASE_HOME ]] || { printf '%s\n' "ERROR: BASE_HOME is not set" >&2; exit 1; }
202-
[[ -d $BASE_HOME ]] || { printf '%s\n' "ERROR: BASE_HOME '$BASE_HOME'is not a directory or is not readable" >&2; exit 1; }
203-
script=$BASE_HOME/base_init.sh
204-
[[ -f $script ]] || { printf '%s\n' "ERROR: base_init script '$script'is not present or is not readable" >&2; exit 1; }
205-
# shellcheck source=/dev/null
206-
source "$script"
207-
((grab_debug)) && {
208-
#
209-
# grab out '-debug' or '--debug' from argument list and set a global variable to turn on debug mode
210-
#
211-
for arg; do
212-
if [[ $arg = "-debug" || $arg = "--debug" ]]; then
213-
BASE_DEBUG=1
214-
elif [[ $arg = "-describe" || $arg = "--describe" ]]; then
215-
_describe
216-
exit $?
217-
elif [[ $arg = "-help" || $arg = "--help" ]]; then
218-
_help
219-
exit $?
220-
else
221-
args+=("$arg")
222-
fi
223-
done
224-
225-
set -- "${args[@]}"
226-
}
227-
228-
main "$@"
229-
}
230-
231189
base_main() {
232190
check_bash_version 4 2 || return $?
233191
do_init || return $?
@@ -243,7 +201,7 @@ base_main() {
243201
#
244202
# these functions need to be available to user's subprocesses
245203
#
246-
export -f base_update base_wrapper import base_activate base_deactivate
204+
export -f base_update import base_activate base_deactivate
247205
}
248206

249207
#

bin/base-wrapper

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# base_wrapper
5+
#
6+
# This script is meant to be used in the shebang line of Base scripts, like this:
7+
#
8+
# #!/usr/bin/env base-wrapper
9+
#
10+
# What does this wrapper do?
11+
#
12+
# It discovers base_init and sources it. It also looks at the command line options and interprets a few of those,
13+
# like --debug, --help, --describe etc. It calls the main function with the special options removed from the argument list.
14+
# The main function is expected to be defined by the wrapped script.
15+
#
16+
17+
_check_and_execute() {
18+
local cmd=$1; shift
19+
if command -v -- "$cmd" &>/dev/null; then
20+
"$cmd" "$@"
21+
exit $?
22+
else
23+
print_error "Function '$cmd' not implemented"
24+
exit 1
25+
fi
26+
}
27+
28+
base_wrapper() {
29+
local arg args script
30+
31+
##
32+
## Do Base set up
33+
##
34+
35+
[[ $1 = "-d" ]] && { grab_debug=1; shift; }
36+
[[ $BASE_HOME ]] || { printf '%s\n' "ERROR: BASE_HOME is not set" >&2; exit 1; }
37+
[[ -d $BASE_HOME ]] || { printf '%s\n' "ERROR: BASE_HOME '$BASE_HOME'is not a directory or is not readable" >&2; exit 1; }
38+
script=$BASE_HOME/base_init.sh
39+
[[ -f $script ]] || { printf '%s\n' "ERROR: base_init script '$script'is not present or is not readable" >&2; exit 1; }
40+
# shellcheck source=/dev/null
41+
source "$script"
42+
import lib/stdlib.sh
43+
44+
##
45+
## Execute the script after processing the special command line arguments
46+
##
47+
48+
source "$1"
49+
shift
50+
for arg; do
51+
if [[ $arg = "-debug" || $arg = "--debug" ]]; then
52+
BASE_DEBUG=1
53+
set -x
54+
elif [[ $arg = "-describe" || $arg = "--describe" ]]; then
55+
_check_and_execute base_describe
56+
elif [[ $arg = "-help" || $arg = "--help" || $arg = "-h" ]]; then
57+
_check_and_execute base_help
58+
else
59+
args+=("$arg")
60+
fi
61+
done
62+
63+
set -- "${args[@]}"
64+
_check_and_execute main "$@"
65+
}
66+
67+
base_wrapper "$@"

bin/caff

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1+
#!/usr/bin/env base-wrapper
2+
13
#
24
# caff: call caffeinate for a named process
35
#
46

5-
_help() {
7+
name=caff
8+
description="Caffeinate a named process"
9+
base_help() {
610
cat << EOUSAGE
711
$description
812
Usage: caff process_name
913
If process_name matches multiple running process, we pick up the first one.
1014
EOUSAGE
1115
}
1216

13-
_describe() {
17+
base_describe() {
1418
printf '%s\n' "$description"
1519
}
1620

@@ -27,7 +31,7 @@ main() {
2731

2832
(($# != 1)) && {
2933
print_error "need an argument"
30-
_help
34+
base_help
3135
exit 2
3236
}
3337

@@ -50,9 +54,3 @@ main() {
5054
caffeinate -iw "$vpid" & disown
5155
exit $?
5256
}
53-
54-
description="Caffeinate a named process"
55-
base_wrapper -d "$@" || {
56-
printf '%s\n' "You need to initialize Base before running this script." >&2
57-
exit 1
58-
}

company/bin/test_command1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env base-wrapper
2+
13
#
24
# test command
35
#
@@ -6,5 +8,3 @@ main() {
68
# do something
79
log_info "Finished"
810
}
9-
10-
base_wrapper "$@" || { printf '%s\n' "ERROR: base environment not initialized; make sure you have sourced base_init.sh before invoking this script" >&2; exit 1; }

team/test-team1/bin/test_command1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env base-wrapper
2+
13
#
24
# test command for team
35
#
@@ -6,5 +8,3 @@ main() {
68
# do something
79
log_info "Finished"
810
}
9-
10-
base_wrapper "$@" || { printf '%s\n' "ERROR: base environment not initialized; make sure you have sourced base_init.sh before invoking this script" >&2; exit 1; }

team/test-team2/bin/test_command1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env base-wrapper
2+
13
#
24
# test command for team
35
#
@@ -6,5 +8,3 @@ main() {
68
# do something
79
log_info "Finished"
810
}
9-
10-
base_wrapper "$@" || { printf '%s\n' "ERROR: base environment not initialized; make sure you have sourced base_init.sh before invoking this script" >&2; exit 1; }

0 commit comments

Comments
 (0)