Skip to content

Commit

Permalink
refactor: unify error message format and refactor related functions ℹ️
Browse files Browse the repository at this point in the history
- keep `usage` function simple
- use `-c`/`-h` option for optional argument of `die` function
  • Loading branch information
oldratlee committed Feb 22, 2024
1 parent 8f2487e commit 28e86aa
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 160 deletions.
42 changes: 27 additions & 15 deletions bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,30 @@ redPrint() {
}

die() {
redPrint "Error: $*" >&2
exit 1
}
local prompt_help=false exit_code=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-c)
exit_code=$2
shift 2
;;
*)
break
;;
esac
done

(($# > 0)) && redPrint "$PROG: $*"
$prompt_help && echo "Try '$PROG --help' for more information."

exit "$exit_code"
} >&2

# `realpath` command existed on Linux and macOS, return resolved physical path
# `realpath` command exists on Linux and macOS, return resolved physical path
# - realpath command on macOS do NOT support option `-e`;
# combined `[ -e $file ]` to check file existence first.
# - How can I get the behavior of GNU's readlink -f on a Mac?
Expand All @@ -45,14 +64,7 @@ realpath() {
}

usage() {
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

# NOTE: $'foo' is the escape sequence syntax of bash
(($# > 0)) && redPrint "$*"$'\n' >&"$out"

cat >&"$out" <<EOF
cat <<EOF
Usage: $PROG [OPTION]... [FILE]...
convert to Absolute Path.
Expand All @@ -65,7 +77,7 @@ Options:
-V, --version display version information and exit
EOF

exit "$exit_code"
exit
}

progVersion() {
Expand All @@ -92,7 +104,7 @@ while (($# > 0)); do
break
;;
-*)
usage 2 "$PROG: unrecognized option '$1'"
die -h "unrecognized option '$1'"
;;
*)
# if not option, treat all follow files as args
Expand All @@ -113,8 +125,8 @@ has_error=false

for f in "${files[@]}"; do
realpath "$f" || {
redPrint "error: $f does not exists!" >&2
has_error=true
redPrint "$PROG: $f: No such file or directory!" >&2
}
done

Expand Down
40 changes: 29 additions & 11 deletions bin/c
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,43 @@ readonly PROG_VERSION='2.x-dev'
# util functions
################################################################################

printErrorMsg() {
redPrint() {
# if stdout is a terminal, turn on color output.
# '-t' check: is a terminal?
# check isatty in bash https://stackoverflow.com/questions/10022323
if [ -t 1 ]; then
printf '\e[1;31m%s\e[0m\n\n' "Error: $*"
printf '\e[1;31m%s\e[0m\n' "$*"
else
printf '%s\n\n' "Error: $*"
printf '%s\n' "$*"
fi
}

usage() {
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))
die() {
local prompt_help=false exit_code=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-c)
exit_code=$2
shift 2
;;
*)
break
;;
esac
done

(($# > 0)) && redPrint "$PROG: $*"
$prompt_help && echo "Try '$PROG --help' for more information."

(($# > 0)) && printErrorMsg "$*" >&"$out"
exit "$exit_code"
} >&2

cat >&"$out" <<EOF
usage() {
cat <<EOF
Usage: $PROG [OPTION]... [command [command_args ...]]
Run command and put output to system clipper.
If no command is specified, read from stdin(pipe).
Expand All @@ -53,7 +71,7 @@ Options:
-V, --version display version information and exit
EOF

exit "$exit_code"
exit
}

progVersion() {
Expand Down Expand Up @@ -90,7 +108,7 @@ while (($# > 0)); do
break
;;
-*)
usage 2 "unrecognized option '$1'"
die -h "unrecognized option '$1'"
;;
*)
# if not option, treat all follow arguments as command
Expand Down
12 changes: 6 additions & 6 deletions bin/coat
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ readonly PROG_VERSION='2.x-dev'
# parse options
################################################################################

progVersion() {
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
printf 'cat executable: %s\n' "$(command -v cat)"
exit
}

usage() {
cat <<EOF
Usage: $PROG [OPTION]... [FILE]...
Expand All @@ -40,6 +34,12 @@ EOF
exit
}

progVersion() {
printf '%s version: %s\n' "$PROG" "$PROG_VERSION"
printf 'cat executable: %s\n' "$(command -v cat)"
exit
}

args=("$@")
# check arguments in reverse, so last option wins.
for ((idx = $# - 1; idx >= 0; --idx)); do
Expand Down
42 changes: 27 additions & 15 deletions bin/cp-into-docker-run
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,34 @@ redPrint() {
}

die() {
redPrint "Error: $*" >&2
exit 1
}
local prompt_help=false exit_code=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-c)
exit_code=$2
shift 2
;;
*)
break
;;
esac
done

(($# > 0)) && redPrint "$PROG: $*"
$prompt_help && echo "Try '$PROG --help' for more information."

exit "$exit_code"
} >&2

isAbsolutePath() {
[[ "$1" =~ ^/ ]]
}

# `realpath` command existed on Linux and macOS, return resolved physical path
# `realpath` command exists on Linux and macOS, return resolved physical path
# - realpath command on macOS do NOT support option `-e`;
# combined `[ -e $file ]` to check file existence first.
# - How can I get the behavior of GNU's readlink -f on a Mac?
Expand All @@ -46,14 +65,7 @@ realpath() {
}

usage() {
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))

# NOTE: $'foo' is the escape sequence syntax of bash
(($# > 0)) && redPrint "$*"$'\n' >&"$out"

cat >&"$out" <<EOF
cat <<EOF
Usage: $PROG [OPTION]... command [command-args]...
Copy the command into docker container
Expand Down Expand Up @@ -82,7 +94,7 @@ miscellaneous:
-V, --version display version information and exit
EOF

exit "$exit_code"
exit
}

progVersion() {
Expand Down Expand Up @@ -140,7 +152,7 @@ while (($# > 0)); do
break
;;
-*)
usage 2 "$PROG: unrecognized option '$1'"
die -h "unrecognized option '$1'"
;;
*)
# if not option, treat all follow arguments as command
Expand All @@ -153,7 +165,7 @@ done
readonly container_name docker_user docker_workdir docker_tmpdir docker_command_cp_path verbose args

[ -n "$container_name" ] ||
usage 1 "No destination docker container name, specified by option -c/--container!"
die -h "requires destination docker container name, specified by option -c/--container!"

if [ -n "$docker_workdir" ]; then
isAbsolutePath "$docker_workdir" ||
Expand Down
48 changes: 30 additions & 18 deletions bin/find-in-jars
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,32 @@ clearResponsiveMessage() {
}

die() {
clearResponsiveMessage
redPrint "Error: $*" >&2
exit 1
}
local prompt_help=false exit_code=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-c)
exit_code=$2
shift 2
;;
*)
break
;;
esac
done

usage() {
local -r exit_code=${1:-0}
(($# > 0)) && shift
local -r out=$(((exit_code != 0) + 1))
clearResponsiveMessage
(($# > 0)) && redPrint "$PROG: $*"
$prompt_help && echo "Try '$PROG --help' for more information."

# NOTE: $'foo' is the escape sequence syntax of bash
(($# > 0)) && redPrint "$*"$'\n' >&"$out"
exit "$exit_code"
} >&2

cat >&"$out" <<EOF
usage() {
cat <<EOF
Usage: $PROG [OPTION]... PATTERN
Find files in the jar files under specified directory,
Expand Down Expand Up @@ -124,7 +136,7 @@ Miscellaneous:
-V, --version display version information and exit
EOF

exit "$exit_code"
exit
}

progVersion() {
Expand Down Expand Up @@ -211,7 +223,7 @@ while (($# > 0)); do
break
;;
-*)
usage 2 "Error: unrecognized option '$1'"
die -h "unrecognized option '$1'"
;;
*)
args=(${args[@]:+"${args[@]}"} "$1")
Expand All @@ -227,13 +239,13 @@ dirs=${dirs:-.}
# shellcheck disable=SC2178
readonly extensions=${extensions:-jar}

((${#args[@]} == 0)) && usage 1 "Missing file pattern!"
((${#args[@]} > 1)) && usage 1 "More than 1 file pattern: ${args[*]}"
((${#args[@]} == 0)) && die -h "requires file pattern!"
((${#args[@]} > 1)) && die -h "more than 1 file pattern: ${args[*]}"
readonly pattern=${args[0]}

tmp_dirs=()
for d in "${dirs[@]}"; do
[ -e "$d" ] || die "file $d(specified by option -d) does not exist!"
[ -e "$d" ] || die "file $d(specified by option -d): No such file or directory!"
[ -d "$d" ] || die "file $d(specified by option -d) exists but is not a directory!"
[ -r "$d" ] || die "directory $d(specified by option -d) exists but is not readable!"

Expand Down Expand Up @@ -284,7 +296,7 @@ __prepareCommandToListZipEntries() {
command_to_list_zip_entries=(jar tf)
is_use_zip_cmd_to_list_zip_entries=false
else
die "NOT found command to list zip entries: zipinfo, unzip or jar!"
die "command to list zip entries NOT found : zipinfo, unzip or jar!"
fi

readonly command_to_list_zip_entries is_use_zip_cmd_to_list_zip_entries
Expand Down Expand Up @@ -326,7 +338,7 @@ searchJarFiles() {
local jar_files total_jar_count

jar_files=$(find "${dirs[@]}" "${find_iname_options[@]}" -type f)
[ -n "$jar_files" ] || die "No ${extensions[*]} file found!"
[ -n "$jar_files" ] || die "${extensions[*]} file NOT found!"

total_jar_count=$(printf '%s\n' "$jar_files" | wc -l)
# remove white space, because the `wc -l` output on mac contains white space!
Expand Down
Loading

0 comments on commit 28e86aa

Please sign in to comment.