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 `-s`/`-h` option for optional argument of `die` function
- use bash builtin `type -P` instead of `which` command 🐚
- rename var, use `COLOR_INDEX` instead of `COUNT`
  • Loading branch information
oldratlee committed Mar 21, 2024
1 parent 52f6a07 commit 0b4180c
Show file tree
Hide file tree
Showing 16 changed files with 297 additions and 196 deletions.
4 changes: 2 additions & 2 deletions bin/a2l
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ readonly args
################################################################################

readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
COLOR_INDEX=0
rotateColorPrint() {
local content=$*
# - if stdout is a terminal, turn on color output.
Expand All @@ -82,7 +82,7 @@ rotateColorPrint() {
if [[ ! -t 1 || $content =~ ^[[:space:]]*$ ]]; then
printf '%s\n' "$content"
else
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}
printf '\e[1;%sm%s\e[0m\n' "$color" "$content"
fi
}
Expand Down
53 changes: 33 additions & 20 deletions bin/ap
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,43 @@ readonly PROG_VERSION='2.x-dev'
# util functions
################################################################################

redPrint() {
errorMsgPrint() {
local errorMsg="$PROG: $*"
# 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' "$*"
printf '\e[1;31m%s\e[0m\n' "$errorMsg"
else
printf '%s\n' "$*"
printf '%s\n' "$errorMsg"
fi
}
} >&2

die() {
redPrint "Error: $*" >&2
exit 1
}

# `realpath` command existed on Linux and macOS, return resolved physical path
local prompt_help=false exit_status=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-s)
exit_status=$2
shift 2
;;
*)
break
;;
esac
done

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

exit "$exit_status"
} >&2

# `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 +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]... [FILE]...
convert to Absolute Path.
Expand All @@ -65,7 +78,7 @@ Options:
-V, --version display version information and exit
EOF

exit "$exit_code"
exit
}

progVersion() {
Expand All @@ -92,7 +105,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 +126,8 @@ has_error=false

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

Expand Down
42 changes: 30 additions & 12 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))

(($# > 0)) && printErrorMsg "$*" >&"$out"
die() {
local prompt_help=false exit_status=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-s)
exit_status=$2
shift 2
;;
*)
break
;;
esac
done

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

exit "$exit_status"
} >&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
18 changes: 9 additions & 9 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 @@ -34,12 +28,18 @@ Support options:
--version output version information and exit
All other options and arguments are delegated to command cat,
more info see the help/man of command cat(e.g. cat --help).
cat executable: $(command -v cat)
cat executable: $(type -P cat)
EOF

exit
}

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

args=("$@")
# check arguments in reverse, so last option wins.
for ((idx = $# - 1; idx >= 0; --idx)); do
Expand All @@ -58,15 +58,15 @@ unset args idx
[ -t 1 ] || exec cat "$@"

readonly -a ROTATE_COLORS=(33 35 36 31 32 37 34)
COUNT=0
COLOR_INDEX=0
# CAUTION: print content WITHOUT new line
rotateColorPrint() {
local content=$*
# skip color for white space
if [[ $content =~ ^[[:space:]]*$ ]]; then
printf %s "$content"
else
local color=${ROTATE_COLORS[COUNT++ % ${#ROTATE_COLORS[@]}]}
local color=${ROTATE_COLORS[COLOR_INDEX++ % ${#ROTATE_COLORS[@]}]}
printf '\e[1;%sm%s\e[0m' "$color" "$content"
fi
}
Expand Down
44 changes: 28 additions & 16 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_staus=2
while (($# > 0)); do
case "$1" in
-h)
prompt_help=true
shift
;;
-s)
exit_staus=$2
shift 2
;;
*)
break
;;
esac
done

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

exit "$exit_staus"
} >&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 All @@ -171,7 +183,7 @@ fi
# check docker command existence
########################################

command -v docker &>/dev/null || die 'docker command not found!'
type -P docker &>/dev/null || die 'docker command not found!'

########################################
# prepare vars for docker operation
Expand Down
Loading

0 comments on commit 0b4180c

Please sign in to comment.