Skip to content

Instantly share code, notes, and snippets.

@thiago-rezende
Last active June 17, 2023 19:54
Show Gist options
  • Save thiago-rezende/af0c6d139a33cdb7add1e677046352d4 to your computer and use it in GitHub Desktop.
Save thiago-rezende/af0c6d139a33cdb7add1e677046352d4 to your computer and use it in GitHub Desktop.
Shell CLI
#!/usr/bin/env bash
# ██░ ██ ▒█████ ██▀███ █ ██ ██████
# ▓██░ ██▒▒██▒ ██▒▓██ ▒ ██▒ ██ ▓██▒▒██ ▒
# ▒██▀▀██░▒██░ ██▒▓██ ░▄█ ▒▓██ ▒██░░ ▓██▄
# ░▓█ ░██ ▒██ ██░▒██▀▀█▄ ▓▓█ ░██░ ▒ ██▒
# ░▓█▒░██▓░ ████▓▒░░██▓ ▒██▒▒▒█████▓ ▒██████▒▒
# ▒ ░░▒░▒░ ▒░▒░▒░ ░ ▒▓ ░▒▓░░▒▓▒ ▒ ▒ ▒ ▒▓▒ ▒ ░
# ▒ ░▒░ ░ ░ ▒ ▒░ ░▒ ░ ▒░░░▒░ ░ ░ ░ ░▒ ░ ░
# ░ ░░ ░░ ░ ░ ▒ ░░ ░ ░░░ ░ ░ ░ ░ ░
# ░ ░ ░ ░ ░ ░ ░ ░
#
# Filename: cli.sh
# GitHub: https://github.com/thiago-rezende
# Maintainer: Thiago Rezende <thiago.manoel.rezende@gmail.com>
# script file name
script_name=`basename "$0"`
# logs directory
logs_directory=${SCRIPT_LOGS_DIRECTORY:-logs}
# verbosity
if [ -z "${SCRIPT_QUIET_LOGS:-}" ]; then
exec 3>&1
else
exec 3>/dev/null
fi
# ANSI colors
declare -r \
ansi_black='\033[30m' \
ansi_black_bold='\033[0;30;1m' \
ansi_red='\033[31m' \
ansi_red_bold='\033[0;31;1m' \
ansi_green='\033[32m' \
ansi_green_bold='\033[0;32;1m' \
ansi_yellow='\033[33m' \
ansi_yellow_bold='\033[0;33;1m' \
ansi_blue='\033[34m' \
ansi_blue_bold='\033[0;34;1m' \
ansi_magenta='\033[35m' \
ansi_magenta_bold='\033[0;35;1m' \
ansi_cyan='\033[36m' \
ansi_cyan_bold='\033[0;36;1m' \
ansi_white='\033[37m' \
ansi_white_bold='\033[0;37;1m' \
ansi_reset='\033[0m'
declare -r ansi_grey="$ansi_black_bold"
# usage message
usage() {
echo >&3 -e "[$ansi_green_bold $script_name $ansi_reset] <$ansi_white_bold usage $ansi_reset>"
echo >&3 -e "|> $ansi_cyan_bold $script_name $ansi_white_bold failure $ansi_reset | execute the '$ansi_magenta_bold failure $ansi_reset' procedure"
echo >&3 -e "|> $ansi_cyan_bold $script_name $ansi_white_bold help $ansi_reset | show this help message"
exit 0
}
# invalid argument message
invalid_argument() {
echo >&3 -e "[$ansi_green_bold $script_name $ansi_reset] <$ansi_red_bold error $ansi_reset> invalid argument $(if [[ $1 ]]; then echo -e \'$ansi_magenta_bold $1 $ansi_reset\'; fi)"
echo >&3 -e "|> run '$ansi_cyan_bold $script_name $ansi_white_bold help $ansi_reset' to check the available arguments"
exit 1
}
# failure procedure
failure() {
local context=$1
local log_file=$2
echo >&3 -e "[$ansi_green_bold $script_name $ansi_reset] <$ansi_red_bold error $ansi_reset> failed on '$ansi_magenta_bold $context $ansi_reset' context"
echo >&3 -e "|> [$ansi_white_bold log $ansi_reset] check '$ansi_yellow_bold $log_file $ansi_reset' for more information"
exit 1
}
# logs directory
logs_directory() {
if [ -d $logs_directory ]; then
return
fi
echo >&3 -e "[ $ansi_green_bold $script_name $ansi_reset ] <$ansi_cyan_bold logs $ansi_reset> settnig up the '$ansi_cyan_bold logs $ansi_reset' environment"
echo >&3 -e "|> [$ansi_white_bold mkdir $ansi_reset] creating the '$ansi_yellow_bold $logs_directory $ansi_reset' directory"
mkdir >&/tmp/${script_name%.*}__logs__mkdir.log -p $logs_directory
if [ $? -ne 0 ]; then
failure "logs" "/tmp/${script_name%.*}__logs__mkdir.log"
fi
}
# this procedure should fail
should_fail() {
echo >&3 -e "[$ansi_green_bold $script_name $ansi_reset ] <$ansi_white_bold should_fail $ansi_reset> this procedure should call '$ansi_magenta_bold failure $ansi_reset'"
echo >&3 -e "|> [$ansi_cyan_bold testing $ansi_reset] procedure will call '$ansi_magenta_bold failure $ansi_reset' now"
rm >&$logs_directory/sholud_fail__testing.log unknown_file
if [ $? -ne 0 ]; then
failure "should_fail" "$logs_directory/should_fail__testing.log"
fi
}
# argument handler
case $1 in
failure) logs_directory; should_fail;;
help) usage;;
*) invalid_argument $1;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment