Skip to content

Commit cfa70f2

Browse files
committed
fix: some improvements
- Fix typo 'wiht' to 'with'. - Change `script_output` variable scope from local to global. - Fix incorrect logic for making options readonly. - Reduce redundant printf calls inside `colour_init()` function. - Change `path_entry` variable scope from global to local. - Silent failure is OK for cleanup.
1 parent 0bfdeb8 commit cfa70f2

6 files changed

Lines changed: 199 additions & 232 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,6 @@ jobs:
5555
--target main \
5656
--generate-notes \
5757
--latest \
58-
source.sh script.sh template.sh
58+
source.sh script.sh template.sh clone_bash_template.fish
5959
env:
6060
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
## DESCRIPTION : Build script that merge source.sh and script.sh into one
55
## CREATED : #~TIME~#
66
## TEMVER : v2.1.1
7+
## TEMURL : https://github.com/Silverbullet069/bash-script-template
78
## AUTHOR : ralish (https://github.com/ralish/)
89
## CONTRIBUTOR : Silverbullet069 (https://github.com/Silverbullet069/)
910
## LICENSE : MIT License

clone_bash_template.fish

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,29 @@ function clone_bash_template --description 'Clone Bash template to specified des
22
# Check for exactly one argument
33
if test (count $argv) -ne 1
44
echo "Error: clone_bash_template requires exactly one directory path!" 1>&2
5-
return 1
5+
return 2
66
end
77

8-
set -l target_file $argv[1]
9-
set -l template_path "/home/$USER/LocalRepository/bash-script-template/script.sh"
8+
set -l target_file "$(realpath "$argv[1]")"
9+
set -l template_path "/home/$USER/LocalRepository/bash-script-template/template.sh"
1010

1111
# Verify template exists
12-
if not test -f $template_path
12+
if not test -f "$template_path"
1313
echo "Error: Template file not found at $template_path" 1>&2
14-
return 2
14+
return 1
1515
end
1616

1717
# Check file extension
1818
if string match -q "*.*" $target_file
1919
and not string match -q "*.sh" $target_file
2020
and not string match -q "*.bash" $target_file
2121
echo 'Error: Extension not supported. Please use ".sh" or ".bash" extension.' 1>&2
22-
return 3
22+
return 1
2323
end
2424
# Extensionless is perfectly fine
2525

26-
# Copy template
27-
if not cp $template_path $target_file
28-
echo "Error: Failed to copy template to $target_file" 1>&2
29-
return 4
30-
end
26+
# Copy template, if existed prompt to override
27+
cp -i "$template_path" "$target_file"
3128

3229
# Make the file executable
3330
chmod +x "$target_file"

script.sh

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
## DESCRIPTION : General Bash script template
55
## CREATED : #~TIME~#
66
## TEMVER : v2.1.1
7+
## TEMURL : https://github.com/Silverbullet069/bash-script-template
78
## AUTHOR : ralish (https://github.com/ralish/)
89
## CONTRIBUTOR : Silverbullet069 (https://github.com/Silverbullet069/)
910
## LICENSE : MIT License
@@ -21,7 +22,7 @@ function parse_params() {
2122
# shellcheck disable=SC2016,SC2312
2223
local script_file="${BASH_SOURCE[0]}"
2324
local in_case_block=false
24-
local -A options=() # associative array
25+
local -A options=() # associative array
2526
declare -g help_options=() # indexed array
2627

2728
while IFS= read -r line; do
@@ -38,12 +39,12 @@ function parse_params() {
3839
if [[ $line =~ ^[[:space:]]*-([a-z])[[:space:]]\|[[:space:]]--([a-z-]+)\)$ ]]; then
3940
option_name="${BASH_REMATCH[2]//-/_}"
4041
option_help="-${BASH_REMATCH[1]}, --${BASH_REMATCH[2]}"
41-
options["${option_name}"]= # empty
42+
options["${option_name}"]= # empty
4243

4344
elif [[ $line =~ ^[[:space:]]*--([a-z-]+)\)$ ]]; then
4445
option_name="${BASH_REMATCH[1]//-/_}"
4546
option_help=" --${BASH_REMATCH[1]}"
46-
options["${option_name}"]= # empty
47+
options["${option_name}"]= # empty
4748

4849
elif [[ $line =~ ^[[:space:]]*###[[:space:]]*(.*)$ ]]; then
4950
help_options+=("$(printf " %-28s %s\n" "${option_help}" "${BASH_REMATCH[1]}")")
@@ -54,7 +55,7 @@ function parse_params() {
5455
fi
5556
fi
5657
fi
57-
done < "$script_file"
58+
done <"$script_file"
5859

5960
# Check if options array is empty
6061
# shellcheck disable=SC2015
@@ -71,50 +72,50 @@ function parse_params() {
7172
local param="${1}"
7273
shift
7374
case "${param}" in
74-
# Add your options here
75-
# ...
76-
77-
# Built-in options
78-
# NOTE: ### comment will be displayed as short description for options in --help output
79-
-l | --log-level)
80-
### Specify log level. Add DEBUG=1 to turn on Bash debug mode.
81-
### Valid values: DBG, INF, WRN, ERR. Default: INF
82-
83-
_option_log_level="${1}"
84-
shift
85-
if [[ -z "${LOG_LEVELS[${_option_log_level}]}" ]]; then
86-
script_exit "Invalid log level: ${_option_log_level}. Choose 1 of the following: ${LOG_LEVELS[*]}" 2
87-
fi
88-
;;
89-
-n | --no-colour)
90-
### Disables colour output
91-
92-
_option_no_colour=1
93-
;;
94-
-q | --quiet)
95-
### Run silently unless an error is encountered
96-
97-
_option_quiet=1
98-
;;
99-
-t | --timestamp)
100-
### Enables timestamp output
101-
102-
_option_timestamp=1
103-
;;
104-
-h | --help)
105-
### Displays this help and exit
106-
107-
script_usage
75+
# Add your options here
76+
# ...
77+
78+
# Built-in options
79+
# NOTE: ### comment will be displayed as short description for options in --help output
80+
-l | --log-level)
81+
### Specify log level. Add DEBUG=1 to turn on Bash debug mode.
82+
### Valid values: DBG, INF, WRN, ERR. Default: INF
83+
84+
_option_log_level="${1}"
85+
shift
86+
if [[ -z "${LOG_LEVELS[${_option_log_level}]}" ]]; then
87+
script_exit "Invalid log level: ${_option_log_level}. Choose 1 of the following: ${LOG_LEVELS[*]}" 2
88+
fi
89+
;;
90+
-n | --no-colour)
91+
### Disables colour output
92+
93+
_option_no_colour=1
94+
;;
95+
-q | --quiet)
96+
### Run silently unless an error is encountered
97+
98+
_option_quiet=1
99+
;;
100+
-t | --timestamp)
101+
### Enables timestamp output
102+
103+
_option_timestamp=1
104+
;;
105+
-h | --help)
106+
### Displays this help and exit
107+
108+
script_usage
109+
exit 0
110+
;;
111+
*)
112+
# internal function calling
113+
if declare -F "${param}" &>/dev/null && [[ -n "${DEBUG-}" ]]; then
114+
"${param}" "$@"
108115
exit 0
109-
;;
110-
*)
111-
# internal function calling
112-
if declare -F "${param}" &> /dev/null && [[ -n "${DEBUG-}" ]]; then
113-
"${param}" "$@"
114-
exit 0
115-
fi
116-
script_exit "Invalid parameter was provided: ${param}" 1
117-
;;
116+
fi
117+
script_exit "Invalid parameter was provided: ${param}" 1
118+
;;
118119
esac
119120
done
120121

@@ -123,7 +124,7 @@ function parse_params() {
123124
[[ ${#options[@]} -eq 0 ]] && script_exit "No options found in parse_params() function." 1 || true
124125

125126
# Make the options read-only
126-
for option in "${options[@]}"; do
127+
for option in "${!options[@]}"; do
127128
readonly "_option_${option}"
128129
done
129130
}
@@ -133,7 +134,7 @@ function parse_params() {
133134
# OUTS: None
134135
# RETS: None
135136
function script_usage() {
136-
cat << EOF
137+
cat <<EOF
137138
138139
Usage: #~NAME~# [OPTIONS] ...
139140
@@ -176,7 +177,7 @@ fi
176177

177178
# Only enable these shell behaviours if we're not being sourced
178179
# Approach via: https://stackoverflow.com/a/28776166/8787985
179-
if ! (return 0 2> /dev/null); then
180+
if ! (return 0 2>/dev/null); then
180181
# A better class of script...
181182
set -o errexit # Exit on most errors (see the manual)
182183
set -o nounset # Disallow expansion of unset variables
@@ -198,7 +199,7 @@ source "$(dirname "${BASH_SOURCE[0]}")/source.sh"
198199

199200
# Invoke main with args if not sourced
200201
# Approach via: https://stackoverflow.com/a/28776166/8787985
201-
if ! (return 0 2> /dev/null); then
202+
if ! (return 0 2>/dev/null); then
202203
main "$@"
203204
fi
204205

0 commit comments

Comments
 (0)