scwrypts/zsh/lib/utils/io.zsh
yage a739d3b5a2 v4.0.0
=====================================================================

Big day! V4 is finally live. This INCLUDES some BREAKING CHANGES to ZSH
TYPE scwrypts! Please refer to the readme for upgrade details
                     (more specifically docs/upgrade/v3-to-v4.md)

Upgrade is SUPER EASY, so please take the time to do so.

--- New Features ----------------------------------------------------

- zsh type scwrypts have an upgraded runstring to improve context setup
  and simplicity to the scwrypt-writer

- scwrypts now publishes the package (scwrypts) to PyPi; this provides a
  simple way to invoke scwrypts from python-based environments as well
  as the entire scwrypts python library suite

  pip install scwrypts

- scwrypts now publishes the package (scwrypts) to npm; this provides a
  simple way to invoke scwrypts from nodesjs environments

  npm install scwrypts

--- Bug Fixes -------------------------------------------------------

- scwrypts runner prompts which use the zshbuiltin "read" now
  appropriately read input from tty, pipe, files, and user input

- virtualenv refresh now loads and prepares the scwrypts virtual
  environments correctly

--- Changes ---------------------------------------------------------

- created the (-v, --log-level) scwrypts arguments as improvements of
  and replacements to the --verbose and --no-log flags
     - (-n) is now an alias for (--log-level 0)
     - (--no-log) is the same as (-n) for compatibility, but will be removed in 4.2

- zsh/lib/utils/io print functions now *interact with log-level* various
  log levels will now only display the appropriate console prints for
  the specified log level

- zsh/lib/utils/io:INFO has been renamed to DEBUG to align with
  log-level output; please use DEBUG for debug messages and REMINDER for
  important user messages

- created zsh/lib/utils/io:FZF_USER_INPUT as a *drop-in replacement* for
  the confusing FZF_HEAD and FZF_TAIL commands. Update by literally
  changing any instances of FZF_HEAD or FZF_TAIL with FZF_USER_INPUT
     - FZF_HEAD and FZF_TAIL will be removed in 4.2

- zsh/lib/utils/io:READ (and other zshbuiltin/read-based prompts) now
  accept a --force-user-input flag in case important checks should
  require an admin's approval. This flag will ensure that piped input
  and the `scwrypts -y` flag are ignored for the single prompt.

- zsh/lib/utils/color has been updated to use color names which match
  the ANSI color names

- zsh/hello-world has been reduced to a minimal example; this is to
  emphasize ease-of-use with v4

- zsh/sanity-check is a scwrypts/run testing helper and detailed
  starting reference (helpful since hello-world is now minimal)

- various refactor, updates, and improvements to the scwrypts runner

- migrated all zsh scwrypts and plugins to use v4 runner syntax
     - zsh
     - plugins/kubectl
     - plugins/ci

- refactored py/lib into py/lib/scwrypts (PyPi)
2024-02-20 23:51:32 -07:00

231 lines
5.6 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#####################################################################
### basic colorized print messages ##################################
#####################################################################
source "${0:a:h}/io.print.zsh"
[ ! $ERRORS ] && ERRORS=0
ERROR() { # command encountered an error
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="ERROR ✖" COLOR=$__RED PRINT "$@"
((ERRORS+=1))
return $ERRORS
}
SUCCESS() { # command completed successfully
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="SUCCESS ✔" COLOR=$__GREEN PRINT "$@"
}
REMINDER() { # include sysadmin reminder or other important notice to users
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="REMINDER " COLOR=$__BRIGHT_MAGENTA PRINT "$@"
}
STATUS() { # general status updates (prefer this to generic 'echo')
[[ $SCWRYPTS_LOG_LEVEL -ge 2 ]] \
&& PREFIX="STATUS " COLOR=$__BLUE PRINT "$@"
}
WARNING() { # warning-level messages; not errors
[[ $SCWRYPTS_LOG_LEVEL -ge 3 ]] \
&& PREFIX="WARNING " COLOR=$__YELLOW PRINT "$@"
}
DEBUG() { # helpful during development or (sparingly) to help others' development
[[ $SCWRYPTS_LOG_LEVEL -ge 4 ]] \
&& PREFIX="DEBUG " COLOR=$__WHITE PRINT "$@"
}
PROMPT() { # you probably want to use yN or INPUT from below
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="PROMPT " COLOR=$__CYAN PRINT "$@" \
&& PREFIX="USER ⌨" COLOR=$__BRIGHT_CYAN PRINT '' --no-line-end \
;
}
FAIL() { SCWRYPTS_LOG_LEVEL=1 ERROR "${@:2}"; exit $1; }
ABORT() { FAIL 69 'user abort'; }
#####################################################################
### check for reported errors and format USAGE contents #############
#####################################################################
CHECK_ERRORS() {
local FAIL_OUT=true
local DISPLAY_USAGE=true
[ ! $ERRORS ] && ERRORS=0
while [[ $# -gt 0 ]]
do
case $1 in
--fail ) FAIL_OUT=true ;;
--no-fail ) FAIL_OUT=false ;;
--usage ) DISPLAY_USAGE=true ;;
--no-usage ) DISPLAY_USAGE=false ;;
esac
shift 1
done
[[ $ERRORS -eq 0 ]] && return 0
[[ $DISPLAY_USAGE =~ true ]] && USAGE
[[ $FAIL_OUT =~ true ]] && exit $ERRORS || return $ERRORS
}
source "${0:a:h}/io.usage.zsh"
#####################################################################
### facilitate user prompt and input ################################
#####################################################################
# yes/no prompts && = yes (exit code 0)
# || = no (exit code 1)
Yn() { [[ ! $(READ_YN $@ '[Yn]') =~ [nN] ]]; } # default 'yes'
yN() { [[ $(READ_YN $@ '[yN]') =~ [yY] ]]; } # default 'no'
INPUT() { # read a single line of user input
PROMPT "${@:2}"
READ $1
local VALUE=$(eval echo '$'$1)
[ $VALUE ]
}
source "${0:a:h}/io.fzf.zsh" # allow user to select from a list of inputs
EDIT() { # edit a file in user's preferred editor
[ $CI ] && {
WARNING 'currently in CI, skipping EDIT'
return 0
}
STATUS "opening '$1' for editing"
$EDITOR $@ </dev/tty >/dev/tty
SUCCESS "finished editing '$1'!"
}
#####################################################################
### basic commands with tricky states or default requirements #######
#####################################################################
LESS() { less -R $@ </dev/tty >/dev/tty; }
YQ() {
yq --version | grep -q mikefarah || {
yq $@
return $?
}
yq eval '... comments=""' | yq $@
}
#####################################################################
### other i/o utilities #############################################
#####################################################################
CAPTURE() {
[ ! $USAGE ] && USAGE="
usage: stdout-varname stderr-varname [...cmd and args...]
captures stdout and stderr on separate variables for a command
"
{
IFS=$'\n' read -r -d '' $2;
IFS=$'\n' read -r -d '' $1;
} < <((printf '\0%s\0' "$(${@:3})" 1>&2) 2>&1)
}
GETSUDO() {
echo "\\033[1;36mPROMPT  : checking sudo password...\\033[0m" >&2
sudo echo hi >/dev/null 2>&1 </dev/tty \
&& SUCCESS '...authenticated!' \
|| { ERROR 'failed :c'; return 1; }
}
READ() {
[ $CI ] && [ -t 0 ] \
&& FAIL 42 'currently in CI, but attempting interactive read; aborting'
local FORCE_USER_INPUT=false
local ARGS=()
while [[ $# -gt 0 ]]
do
case $1 in
--force-user-input ) FORCE_USER_INPUT=true ;;
-k )
ARGS+=($1)
;;
* ) ARGS+=($1) ;;
esac
shift 1
done
while read -k -t 0; do :; done; # flush user stdin
case $FORCE_USER_INPUT in
true )
read ${PREARGS[@]} ${ARGS[@]} $@ </dev/tty
;;
false )
[ -t 0 ] || ARGS=(-u 0 ${ARGS[@]})
read ${ARGS[@]} $@
;;
esac
}
READ_YN() { # yes/no read is suprisingly tricky
local FORCE_USER_INPUT=false
local USERPROMPT=()
local READ_ARGS=()
while [[ $# -gt 0 ]]
do
case $1 in
--force-user-input )
# overrides 'scwrypts -y' and stdin pipe but not CI
FORCE_USER_INPUT=true
READ_ARGS+=($1)
;;
* ) USERPROMPT+=($1) ;;
esac
shift 1
done
##########################################
local SKIP_USER_INPUT=false
[ $CI ] \
&& SKIP_USER_INPUT=true
[ $__SCWRYPTS_YES ] && [[ $__SCWRYPTS_YES -eq 1 ]] && [[ $FORCE_USER_INPUT =~ false ]] \
&& SKIP_USER_INPUT=true
##########################################
local yn
PROMPT "${USERPROMPT[@]}"
case $SKIP_USER_INPUT in
true ) yn=y ;;
false )
[[ $FORCE_USER_INPUT =~ true ]] && [[ $SCWRYPTS_LOG_LEVEL -lt 1 ]] \
&& echo -n "${USERPROMPT[@]} : " >&2
READ ${READ_ARGS[@]} -s -k yn
[[ $FORCE_USER_INPUT =~ true ]] && [[ $SCWRYPTS_LOG_LEVEL -lt 1 ]] \
&& echo $yn >&2
;;
esac
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] && echo $yn >&2
echo $yn
}