Skip to content

Commit

Permalink
ci: linting
Browse files Browse the repository at this point in the history
- `while true` to `while :`, and `true` to `:` where appropriate
- brew: compact a conditional
- (choose|config-helper|confirm|echo-lines|is-empty-value|setup-(git|linux|mac|system)): compact an or conditional
- is-suffix: update for `__substr` shim in `bash.bash` for bash v3 support
- `is-dir`: move to deprecated, as there is now `is-directory`
  • Loading branch information
balupton committed Oct 29, 2024
1 parent 75a30d7 commit ef8a387
Show file tree
Hide file tree
Showing 20 changed files with 39 additions and 37 deletions.
8 changes: 6 additions & 2 deletions commands.beta/is-suffix
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ function is_suffix() (
haystack="${2:?"USAGE: is-suffix <needle> <HAYSTACK>"}"

# oldschool way:
# echo-regexp -q "$needle\$" -- "$haystack"
# echo-regexp -q "$needle\$" -- "$haystack" <-- doesn't work, as $needle is not escaped
# [[ $haystack =~ ^.*$needle$ ]] <-- doesn't work, as $needle is not escaped

# better way:
[[ $needle == "${haystack:${#needle}*-1}" ]]
local suffix
local -i length=${#needle}
suffix="$(__substr "$haystack" $((length * -1)))"
[[ $needle == "$suffix" ]]
return
)

Expand Down
1 change: 1 addition & 0 deletions commands.beta/is-dir → commands.deprecated/is-dir
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

function is_dir() (
source "$DOROTHY/sources/bash.bash"
dorothy-warnings add --code='is-dir' --bold=' has been deprecated in favor of ' --code='is-directory'

local dir="$1"
[[ -d $dir ]]
Expand Down
2 changes: 1 addition & 1 deletion commands/ask
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ function ask_() (
fi

ASKED='yes' # not local
while true; do
while :; do
# reset
__read_status=0
READ_RESULT=''
Expand Down
2 changes: 1 addition & 1 deletion commands/brew
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function brew_() (
fi
return 22 # EINVAL 22 Invalid argument
}
if [[ -z $bin ]] || [[ $* == '--help' ]]; then
if [[ -z $bin || $* == '--help' ]]; then
help >/dev/stderr
fi

Expand Down
7 changes: 3 additions & 4 deletions commands/choose
Original file line number Diff line number Diff line change
Expand Up @@ -1402,8 +1402,7 @@ function choose_() (
if [[ $option_required == 'yes' ]]; then
# if required, and trying to do proceed without items, then go back to choose and send a bell
if
[[ ($selected_count -eq 0 && ($new == 'confirm' || $new == 'confirmed')) ||
($fallbacks_count -eq 0 && $new == 'cancel') ]]
[[ ($selected_count -eq 0 && $new =~ ^(confirm|confirmed)$) || ($fallbacks_count -eq 0 && $new == 'cancel') ]]
then
menu_skip_render='yes'
menu_mode='choose'
Expand Down Expand Up @@ -1902,7 +1901,7 @@ function choose_() (
# action
show_cursor_on_exit
__print_string "$style__alternative_screen_buffer" >"$terminal_device_file"
while true; do
while :; do
# (re-)render the menu?
if [[ $menu_skip_render == 'no' ]]; then
render_menu
Expand Down Expand Up @@ -2037,7 +2036,7 @@ function choose_() (
else
action_all
fi
elif [[ $key == 'insert' || $key == '+' ]]; then
elif [[ $key =~ ^(insert|\+)$ ]]; then
action_select_and_next
elif [[ $key == '-' ]]; then
action_select_and_prior
Expand Down
2 changes: 1 addition & 1 deletion commands/config-edit
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ function config_edit() (
else
expected=''
fi
while true; do
while :; do
lines="$("$option_searcher" "$option_needle" || :)"
eval_capture --statusvar=status -- "$option_comparer" "$lines" "$expected"
if [[ $status -eq 0 ]]; then
Expand Down
4 changes: 2 additions & 2 deletions commands/config-helper
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ function config_helper() (
esac

# adjust quote based on file
if [[ $option_quote == 'generic' || $option_quote == 'yes' ]]; then
if [[ $option_quote =~ ^(generic|yes)$ ]]; then
if [[ $option_file == *'.bash' || $option_file == *'.sh' ]]; then
option_quote='bash'
else
Expand Down Expand Up @@ -370,7 +370,7 @@ function config_helper() (
value="$(echo-escape-bash -- "$value")"
elif [[ $option_quote == 'command' ]]; then
value="$(echo-escape-command -- "$value")"
elif [[ $option_quote == 'generic' || $option_quote == 'yes' ]]; then
elif [[ $option_quote =~ ^(generic|yes)$ ]]; then
value="$(echo-quote -- "$value")"
fi
addition="$field=$value"
Expand Down
13 changes: 7 additions & 6 deletions commands/confirm
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function confirm_() (
done

# mode
if [[ $option_mode == 'positive' || $option_mode == 'negative' ]]; then
if [[ $option_mode =~ ^(positive|negative)$ ]]; then
if [[ -z $option_timeout ]]; then
option_timeout='60' # one minute
fi
Expand Down Expand Up @@ -350,7 +350,7 @@ function confirm_() (
question_confirm_and_input_lines=''
if [[ $terminal_tty_support == 'no' ]]; then
for key in "${keys[@]}"; do
if [[ $key == 'enter' || $key == 'line-buffer' ]]; then
if [[ $key =~ ^(enter|line-buffer)$ ]]; then
question_confirm_and_input_lines+=$'\n'
fi
done
Expand Down Expand Up @@ -405,19 +405,20 @@ function confirm_() (
fi
;;
'left' | 'right' | 'up' | 'down')
# Yes the differences in combination is intentional... try it!
if [[ -z $selected_index || $selectable_count -eq 1 ]]; then
if [[ $key == 'right' || $key == 'up' ]]; then
if [[ $key =~ ^(right|up)$ ]]; then
selected_index="$selectable_last"
elif [[ $key == 'left' || $key == 'down' ]]; then
elif [[ $key =~ ^(left|down)$ ]]; then
selected_index=0
fi
elif [[ $key == 'left' || $key == 'up' ]]; then
elif [[ $key =~ ^(left|up)$ ]]; then
if [[ $selected_index -eq 0 ]]; then
selected_index="$selectable_last"
else
selected_index="$((selected_index - 1))"
fi
elif [[ $key == 'right' || $key == 'down' ]]; then
elif [[ $key =~ ^(right|down)$ ]]; then
if [[ $selected_index -eq $selectable_last ]]; then
selected_index=0
else
Expand Down
2 changes: 1 addition & 1 deletion commands/echo-lines
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ function echo_lines() (
function on_input {
items+=("$option_prefix$(echo-escape-command -- "$1")$option_suffix")
}
elif [[ $option_quote == 'generic' || $option_quote == 'yes' ]]; then
elif [[ $option_quote =~ ^(generic|yes)$ ]]; then
function on_input {
items+=("$option_prefix$(echo-quote -- "$1")$option_suffix")
}
Expand Down
4 changes: 2 additions & 2 deletions commands/fs-temp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function fs_temp() (
for directory in "${option_directories[@]}"; do
if [[ -z $directory ]]; then
# generate a non-existent directory name
while true; do
while :; do
directory="$option_prefix$(get-random-number)$option_suffix"
if is-missing -- "$root/$directory"; then
break
Expand All @@ -144,7 +144,7 @@ function fs_temp() (
for file in "${option_files[@]}"; do
if [[ -z $file ]]; then
# generate a non-existent filename
while true; do
while :; do
file="$option_prefix$(get-random-number)$option_suffix$option_extension"
if is-missing -- "$root/$file"; then
break
Expand Down
5 changes: 1 addition & 4 deletions commands/is-empty-value
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ function is_empty_value() (
local value
for value in "${option_inputs[@]}"; do
# check for empty values, or check for an empty string
if [[ -z $value ||
$value == 'null' || $value == 'NULL' ||
$value == 'void' || $value == 'VOID' ||
$value == 'undefined' || $value == 'UNDEFINED' ]] || is-empty-string -- "$value"; then
if [[ -z $value || $value =~ ^(null|NULL|void|VOID|undefined|UNDEFINED)$ ]] || is-empty-string -- "$value"; then
: # all good, conntinue
else
return 1 # not good
Expand Down
2 changes: 1 addition & 1 deletion commands/mount-helper
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,7 @@ function mount_helper() (

# it exists, check if mounted
local local_check_status=''
while true; do
while :; do
# update check
eval_capture -- do_check
local_check_status="$SHARED_CHECK_STATUS"
Expand Down
2 changes: 1 addition & 1 deletion commands/setup-git
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function setup_git() (
fi

# handle deprecations
if [[ $GPG_SIGNING_KEY == 'op' || $GPG_SIGNING_KEY == '1password' ]]; then
if [[ $GPG_SIGNING_KEY =~ ^(op|1password)$ ]]; then
GPG_SIGNING_AGENT='op'
dorothy-config 'git.bash' -- \
--field='GPG_SIGNING_KEY' --replace= \
Expand Down
2 changes: 1 addition & 1 deletion commands/setup-linux
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ function setup_linux() (
-- "do_${item}_clean"
fi
done
elif [[ $action == 'install' || $action == 'update' ]]; then
elif [[ $action =~ ^(install|update)$ ]]; then
# log
echo-style --h1="$title"

Expand Down
2 changes: 1 addition & 1 deletion commands/setup-mac
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function setup_mac() (
# action
if [[ $action == 'clean' ]]; then
setup-mac-brew clean
elif [[ $action == 'install' || $action == 'update' ]]; then
elif [[ $action =~ ^(install|update)$ ]]; then
# log
echo-style --h1="$title"

Expand Down
2 changes: 1 addition & 1 deletion commands/setup-system
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function setup_system() (
echo-style --h1="$title"

# action
if [[ $action == 'install' || $action == 'update' ]]; then
if [[ $action =~ ^(install|update)$ ]]; then
# pre-requisites
is-internet-working

Expand Down
2 changes: 1 addition & 1 deletion commands/setup-util-xcode
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function setup_util_xcode() (

# check if xcode exists
local xcode xcodebuild xcodesdk
while true; do
while :; do
# prerequisites
setup-util-apple-rosetta --quiet="$option_quiet"
setup-util-apple-cli-tools --quiet="$option_quiet"
Expand Down
2 changes: 1 addition & 1 deletion commands/symlink-helper
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function symlink_helper() (

# ensure validity of existing path
local original
while true; do
while :; do
# ensure both paths are not relative paths, otherwise weird things will happen
option_existing="$(fs-absolute -- "$option_existing")"
option_symlink="$(fs-absolute -- "$option_symlink")"
Expand Down
2 changes: 1 addition & 1 deletion commands/waiter
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function waiter_() (

# wait
local -i minutes delta waited=0
while true; do
while :; do
if [[ -n $option_exists && -e $option_exists ]]; then
option_status=0
break
Expand Down
10 changes: 5 additions & 5 deletions sources/bash.bash
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function __sudo_mkdirp {
return "$status"
}

# bash < 4.2 doesn't support negative lengths, bash >= 4.2 supports negative start indexes however it requires a preceeding space if done directly: ${var: -1}
# bash < 4.2 doesn't support negative lengths, bash >= 4.2 supports negative start indexes however it requires a preceeding space or wrapped parenthesis if done directly: ${var: -1} or ${var:(-1)}
# the bash >= 4.2 behaviour returns empty string if negative start index is out of bounds, rather than the entire string, which is unintuitive: v=12345; s=-6; echo "${v:s}"
# function __substr_native {
# local string="$1" start="${2:-0}" length="${3-}"
Expand Down Expand Up @@ -344,7 +344,7 @@ if [[ -z ${BASH_VERSION_CURRENT-} ]]; then
if [[ $BASH_VERSION_MAJOR -eq 5 ]]; then
IS_BASH_VERSION_OUTDATED='no'
function __require_upgraded_bash {
true
:
}
else
IS_BASH_VERSION_OUTDATED='yes'
Expand Down Expand Up @@ -377,7 +377,7 @@ shopt -s huponexit
# bash v4.2: lastpipe If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current shell environment.
if shopt -s lastpipe 2>/dev/null; then
function __require_lastpipe {
true
:
}
else
function __require_lastpipe {
Expand Down Expand Up @@ -644,7 +644,7 @@ shopt -s nullglob
# bash v4: globstar: If set, the pattern ‘**’ used in a filename expansion context will match all files and zero or more directories and subdirectories. If the pattern is followed by a ‘/’, only directories and subdirectories match.
if shopt -s globstar 2>/dev/null; then
function __require_globstar {
true
:
}
else
function __require_globstar {
Expand All @@ -656,7 +656,7 @@ fi
# bash v5: extglob: If set, the extended pattern matching features described above (see Pattern Matching) are enabled.
if shopt -s extglob 2>/dev/null; then
function __require_extglob {
true
:
}
else
function __require_extglob {
Expand Down

0 comments on commit ef8a387

Please sign in to comment.