diff --git a/scwrypts b/scwrypts index 978407b..8be6ae1 100755 --- a/scwrypts +++ b/scwrypts @@ -12,7 +12,7 @@ __RUN() { while [[ $# -gt 0 ]] do case $1 in - -e|--env ) + -e | --env ) [ $ENV_NAME ] && __WARNING 'overwriting session environment' ENV_NAME="$2" __STATUS "using CLI environment '$ENV_NAME'" @@ -196,8 +196,7 @@ __VALIDATE_UPSTREAM_TIMELINE() { __GET_LOGFILE() { local SCRIPT="$1" - [ $CI ] \ - || [ $SUBSCWRYPT ] \ + [ $SUBSCWRYPT ] \ || [[ $SCRIPT =~ scwrypts/logs ]] \ || [[ $SCRIPT =~ interactive ]] \ && return 0 diff --git a/zsh/aws/efs/mount b/zsh/aws/efs/mount index f5945a6..29b8d7f 100755 --- a/zsh/aws/efs/mount +++ b/zsh/aws/efs/mount @@ -19,6 +19,7 @@ _EFS_CONNECT() { [ ! $FS_ID ] && __ABORT local MOUNT_POINT="$AWS__EFS__LOCAL_MOUNT_POINT/$FS_ID" + [ -d "$MOUNT_POINT" ] && sudo rmdir "$MOUNT_POINT" >/dev/null 2>&1 [ -d "$MOUNT_POINT" ] && { __STATUS "$FS_ID is already mounted" exit 0 diff --git a/zsh/utils/README.md b/zsh/utils/README.md index 4c73d64..84f71cf 100644 --- a/zsh/utils/README.md +++ b/zsh/utils/README.md @@ -55,17 +55,3 @@ _REQUIRED_ENV+=( source ./path/to/utils.plugin.zsh echo "missing $ENV_ERROR_COUNT required environment variables" ``` - -io.zsh -os.zsh - -## Basic Utilities - -One of my biggest pet-peeves with scripting is when every line of a *(insert-language-here)* program is escaped to shell. -This kind of program, which doesn't use language features, should be a shell script. -While there are definitely unavoidable limitations to shell scripting, we can minimize a variety of problems with a modern shell and shared utilities library. - -Loaded by `common.zsh`, the [`utils/` library](./utils) provides: -- common function wrappers to unify flags and context -- lazy dependency and environment variable validation -- consistent (and pretty) user input / output diff --git a/zsh/utils/colors.zsh b/zsh/utils/colors.zsh new file mode 100644 index 0000000..eb1ce2e --- /dev/null +++ b/zsh/utils/colors.zsh @@ -0,0 +1,25 @@ +__BLACK='\033[0;30m' +__DARK_GRAY='\033[1;30m' + +__RED='\033[0;31m' +__LIGHT_RED='\033[1;31m' + +__GREEN='\033[0;32m' +__LIGHT_GREEN='\033[1;32m' + +__ORANGE='\033[0;33m' +__YELLOW='\033[1;33m' + +__BLUE='\033[1;34m' +__DARK_BLUE='\033[0;34m' + +__PURPLE='\033[1;35m' +__DARK_PURPLE='\033[0;35m' + +__CYAN='\033[1;36m' +__DARK_CYAN='\033[0;36m' + +__WHITE='\033[1;37m' +__LIGHT_GRAY='\033[0;37m' + +__COLOR_RESET='\033[0m' diff --git a/zsh/utils/io.zsh b/zsh/utils/io.zsh index a14e69f..c89c704 100644 --- a/zsh/utils/io.zsh +++ b/zsh/utils/io.zsh @@ -1,14 +1,28 @@ -__ERROR() { echo "\\033[1;31mERROR ✖ : $@\\033[0m" >&2; } -__SUCCESS() { echo "\\033[1;32mSUCCESS ✔ : $@\\033[0m" >&2; } -__WARNING() { echo "\\033[1;33mWARNING  : $@\\033[0m" >&2; } -__STATUS() { echo "\\033[1;34mSTATUS : $@\\033[0m" >&2; } -__REMINDER() { echo "\\033[1;35mREMINDER  : $@\\033[0m" >&2; } +__PRINT() { + local COLOR="$1" + local MESSAGE="$2" + + local LINE_END + [ $3 ] && LINE_END='' || LINE_END='\n' + + printf "${COLOR}${MESSAGE}${__COLOR_RESET}${LINE_END}" +} + +__ERROR() { __PRINT $__RED "ERROR ✖ : $@" >&2; } +__SUCCESS() { __PRINT $__GREEN "SUCCESS ✔ : $@" >&2; } +__WARNING() { __PRINT $__ORANGE "WARNING  : $@" >&2; } +__STATUS() { __PRINT $__BLUE "STATUS : $@" >&2; } +__REMINDER() { __PRINT $__PURPLE "REMINDER  : $@" >&2; } +__INFO() { __PRINT $__WHITE "INFO  : $@" >&2; } __PROMPT() { - echo "\\033[1;36mPROMPT  : $@\\033[0m" >&2 - printf "\\033[1;36mUSER  : \\033[0m" >&2 + __PRINT $__CYAN "PROMPT  : $@" >&2 + __PRINT $__CYAN "USER  : " --no-end >&2 } +__FAIL() { __ERROR "${@:2}"; exit $1; } +__ABORT() { __FAIL 69 'user abort'; } + __Yn() { __PROMPT "$@ [Yn]" [ $CI ] && { echo y; return 0; } @@ -25,10 +39,6 @@ __yN() { [[ $yN =~ [yY] ]] && return 0 || return 1 } -__FAIL() { __ERROR "${@:2}"; exit $1; } - -__ABORT() { __FAIL 69 'user abort'; } - ##################################################################### __GETSUDO() { diff --git a/zsh/utils/utils.module.zsh b/zsh/utils/utils.module.zsh index eabec20..80f94f5 100644 --- a/zsh/utils/utils.module.zsh +++ b/zsh/utils/utils.module.zsh @@ -5,6 +5,7 @@ _REQUIRED_ENV+=() # (extensible) list of required environment variables ##################################################################### +source ${0:a:h}/colors.zsh source ${0:a:h}/io.zsh source ${0:a:h}/os.zsh source ${0:a:h}/credits.zsh