From 85be4fb1bb04f39434604e7e60602b7cfcbcb2d9 Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Fri, 8 Sep 2023 00:47:05 +0800 Subject: [PATCH] fix https://github.com/koalaman/shellcheck/wiki/SC2251 --- commands/is-headless | 6 +++++- commands/is-interactive | 7 +++++-- commands/is-missing | 27 ++++++++++++++++++++++++++- commands/is-nonempty-string | 6 +++++- commands/is-present | 28 ++++++++++++++++++++++++++-- commands/is-value | 6 +++++- docs/bash/errors.md | 4 ++++ 7 files changed, 76 insertions(+), 8 deletions(-) diff --git a/commands/is-headless b/commands/is-headless index afa8a62ca..91e31fa18 100755 --- a/commands/is-headless +++ b/commands/is-headless @@ -3,7 +3,11 @@ function is_headless() ( source "$DOROTHY/sources/bash.bash" - ! is-headful + if is-headful; then + return 1 + else + return 0 + fi ) # fire if invoked standalone diff --git a/commands/is-interactive b/commands/is-interactive index e92a2aae3..2bb3d92cc 100755 --- a/commands/is-interactive +++ b/commands/is-interactive @@ -35,8 +35,11 @@ function is_interactive() ( # ===================================== # Action - is-tty - ! is-ci + if is-tty && ! is-ci; then + return 0 + else + return 1 + fi ) # fire if invoked standalone diff --git a/commands/is-missing b/commands/is-missing index 3973c3fb3..e5ee28822 100755 --- a/commands/is-missing +++ b/commands/is-missing @@ -3,7 +3,32 @@ function is_missing() ( source "$DOROTHY/sources/bash.bash" - ! is-present "$1" + # help + function help { + cat <<-EOF >/dev/stderr + ABOUT: + Check if a path is missing (not a file/directory/symlink). + Opposite of [is-present]. + + USAGE: + is-missing + EOF + if test "$#" -ne 0; then + echo-error "$@" + fi + return 22 # EINVAL 22 Invalid argument + } + + # process + if test "$#" -eq 0; then + help 'No provided.' + elif test "$*" = '--help'; then + help + elif test -e "$1" -o -L "$1"; then # just -e is faulty, as -e fails on symlinks + return 1 + else + return 0 + fi ) # fire if invoked standalone diff --git a/commands/is-nonempty-string b/commands/is-nonempty-string index 4860fa0fe..03a991c8a 100755 --- a/commands/is-nonempty-string +++ b/commands/is-nonempty-string @@ -3,7 +3,11 @@ function is_nonempty_string() ( source "$DOROTHY/sources/bash.bash" - ! is-empty-string "$1" + if is-empty-string "$1"; then + return 1 + else + return 0 + fi ) # fire if invoked standalone diff --git a/commands/is-present b/commands/is-present index d85cbc2b1..4028ea36d 100755 --- a/commands/is-present +++ b/commands/is-present @@ -3,8 +3,32 @@ function is_present() ( source "$DOROTHY/sources/bash.bash" - # just -e is faulty, as -e fails on symlinks - test -e "$1" -o -L "$1" + # help + function help { + cat <<-EOF >/dev/stderr + ABOUT: + Check if a path is present (is a file/directory/symlink). + Opposite of [is-missing]. + + USAGE: + is-present + EOF + if test "$#" -ne 0; then + echo-error "$@" + fi + return 22 # EINVAL 22 Invalid argument + } + + # process + if test "$#" -eq 0; then + help 'No provided.' + elif test "$*" = '--help'; then + help + elif test -e "$1" -o -L "$1"; then # just -e is faulty, as -e fails on symlinks + return 0 + else + return 1 + fi ) # fire if invoked standalone diff --git a/commands/is-value b/commands/is-value index 842273596..4d17042d5 100755 --- a/commands/is-value +++ b/commands/is-value @@ -3,7 +3,11 @@ function is_value() ( source "$DOROTHY/sources/bash.bash" - ! is-empty-value "${1-}" + if is-empty-value "${1-}"; then + return 1 + else + return 0 + fi ) # fire if invoked standalone diff --git a/docs/bash/errors.md b/docs/bash/errors.md index 2259a057d..32d0c415f 100755 --- a/docs/bash/errors.md +++ b/docs/bash/errors.md @@ -500,3 +500,7 @@ For more information on this, refer to: - https://gist.github.com/balupton/21ded5cefc26dc20833e6ed606209e1b - https://github.com/bevry/dorothy/blob/master/sources/bash.bash + +### a final gotcha + +https://github.com/koalaman/shellcheck/wiki/SC2251