v3.0.0 "The Great Overhaul"

=====================================================================

Notice the major version change which comes with breaking changes to
2.x! Reconstructs "library" functions for both python and zsh scwrypts,
with changes to virtualenv naming conventions (you'll need to refresh
all virtualenv with the appropriate scwrypt).

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

- changed a naming convention across zsh scripts, particularly
  removing underscores where there is no need to avoid naming clash
  (e.g. 'zsh/lib/utils/io.zsh' renames '__STATUS' to 'STATUS')

- moved clients reliant on py.lib.http to the py.lib.http module

- python scripts now rely on py.lib.scwrypts.execute

- updated package.json in zx scripts to include `type = module`

- 'scwrypts --list' commandline argument now includes additional
  relevant data for each scwrypt

- environment variables no longer add themselves to be staged in the
  '.env.template'

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

- new 'use' syntax for disjoint import within zsh scripts; took me
  a very long time to convince myself this would be necessary

- introduced scwrypt "groups" to allow portable module creation;
  (i.e. ability add your own scripts from another repo!)

- py.lib.scwrypts.io provides a combined IO stream for quick, hybrid
  use of input/output files and stdin/stdout

- py.lib.fzf provides a wrapper to provide similar functionality to
  zsh/utils/io.zsh including fzf_(head|tail)

- improved efficiency of various scwrypts; notably reducing runtime
  of scwrypts/environment sync

- improved scwrypts CLI by adding new options for exact scwrypt
  matching, better filtering, and prettier/more-detailed interfaces

--- New Scripts --------------------------

- py/twilio )
    basic SMS integration with twilio
     - send-sms

- py/directus )
    interactive directus GET query
     - get-items

- py/discord )
    post message to discord channel or webhook
     - post-message
This commit is contained in:
2023-02-21 18:44:27 -07:00
parent 7617c938b1
commit 76a746a53e
196 changed files with 3472 additions and 2053 deletions

57
zsh/lib/utils/README.md Normal file
View File

@ -0,0 +1,57 @@
# ZSH Utilities
A shell-scripting utilities module made for ZSH.
This module is definitely a major component of Scwrypts, but is also standalone and can be sourced by any ZSH script to utilize (almost) all of the features.
## Usage
Import `utils.module.zsh` to activate all of the features.
Doing so will *also* check for path dependencies and required environment variables (see [Dependencies](#dependencies) and [Environment](#environment) below).
```shell
#!/bin/zsh
source ./path/to/utils.plugin.zsh
SUCCESS 'ZSH utilities online!'
```
Checkout [io](./io.zsh) and [os](./os.zsh) for available simple functions.
### Dependencies
Ensures dependent programs are available for execution.
Specify a simple name to check the current `PATH`, or give a fully-qualified path for arbitrary dependency inclusion.
Include a dependency by adding to the `DEPENDENCIES` array.
*Always using `+=` makes your dependencies extensible to other scripts :)*
If any dependencies are missing, `source utils.module.zsh` will return an error code and count the number of missing dependencies in the variable `DEP_ERROR_COUNT`.
```shell
#!/bin/zsh
DEPENDENCIES+=(
path-executable-1
path-executable-2
/path/to/arbitrary/program
)
source ./path/to/utils.plugin.zsh
echo "missing $DEP_ERROR required dependencies"
```
### Environment
Similar to [Dependencies](#dependencies), `environment.zsh` ensures a list of environment variables are *set to non-empty values*.
Include an environment variable by adding to the `REQUIRED_ENV` array.
*Something something use `+=` here too ;)*
If any environment variables are missing, `source utils.module.zsh` will return an error code and count the number of missing variables in `ENV_ERROR_COUNT`.
Missing environment variables will be added to the environment template (*exclusive to Scwrypts*).
```shell
#!/bin/zsh
REQUIRED_ENV+=(
AWS_PROFILE
AWS_REGION
)
source ./path/to/utils.plugin.zsh
echo "missing $ENV_ERROR_COUNT required environment variables"
```

44
zsh/lib/utils/colors.zsh Normal file
View File

@ -0,0 +1,44 @@
__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'
__GET_RANDOM_COLOR() {
local COLORS=(
$__RED
$__LIGHT_RED
$__GREEN
$__LIGHT_GREEN
$__ORANGE
$__YELLOW
$__BLUE
$__DARK_BLUE
$__PURPLE
$__DARK_PURPLE
$__CYAN
$__DARK_CYAN
$__WHITE
)
print "$__COLOR_RESET${COLORS[$(shuf -i 1-${#COLORS[@]} -n 1)]}"
}

11
zsh/lib/utils/credits.zsh Normal file
View File

@ -0,0 +1,11 @@
__CREDITS() {
# scwrypts exclusive ("credits" pulled from README files)
[ ! $SCWRYPTS_ROOT ] && return 0
local COMMAND="$1"
[[ $COMMAND =~ - ]] && COMMAND=$(echo $COMMAND | sed 's/-/--/g')
cd $SCWRYPTS_ROOT
cat ./**/README.md \
| grep 'Generic Badge' \
| sed -n "s/.*Generic Badge.*-$COMMAND-.*(/(/p"
}

View File

@ -0,0 +1,43 @@
__CHECK_DEPENDENCIES() {
local DEP ERROR=0
DEPENDENCIES=($(echo $DEPENDENCIES | sed 's/ \+/\n/g' | sort -u))
for DEP in ${DEPENDENCIES[@]}; do __CHECK_DEPENDENCY $DEP || ((ERROR+=1)); done
__CHECK_COREUTILS || ((ERROR+=$?))
return $ERROR
}
__CHECK_DEPENDENCY() {
local DEPENDENCY="$1"
[ ! $DEPENDENCY ] && return 1
command -v $DEPENDENCY >/dev/null 2>&1 || {
ERROR "'$1' required but not available on PATH $(__CREDITS $1)"
return 1
}
}
__CHECK_COREUTILS() {
local COREUTILS=(awk find grep sed readlink)
local MISSING_DEPENDENCY_COUNT=0
local NON_GNU_DEPENDENCY_COUNT=0
local UTIL
for UTIL in $(echo $COREUTILS)
do
__CHECK_DEPENDENCY $UTIL || { ((MISSING_DEPENDENCY_COUNT+=1)); continue; }
$UTIL --version 2>&1 | grep -q 'GNU' || {
WARNING "non-GNU version of $UTIL detected"
((NON_GNU_DEPENDENCY_COUNT+=1))
}
done
[[ $NON_GNU_DEPENDENCY_COUNT -gt 0 ]] && {
WARNING 'scripts rely on GNU coreutils; functionality may be limited'
IS_MACOS && REMINDER 'GNU coreutils can be installed and linked through Homebrew'
}
return $MISSING_DEPENDENCY_COUNT
}

View File

@ -0,0 +1,38 @@
__CHECK_REQUIRED_ENV() {
local VAR ERROR=0
REQUIRED_ENV=($(echo $REQUIRED_ENV | sed 's/\s\+/\n/g' | sort -u))
for VAR in ${REQUIRED_ENV[@]}; do __CHECK_ENV_VAR $VAR || ((ERROR+=1)); done
return $ERROR
}
__CHECK_ENV_VAR() {
local NAME="$1"
[ ! $NAME ] && return 1
local OVERRIDE_VALUE=$(eval echo '$'$NAME'__override')
[ $OVERRIDE_VALUE ] && export $NAME=$OVERRIDE_VALUE && return 0
local OPTIONAL="$2"
local DEFAULT_VALUE="$3"
local VALUE=$(eval echo '$'$NAME)
[ $VALUE ] && return 0
local SELECTION_VALUES=$(eval echo '$'$NAME'__select' | sed 's/,/\n/g; s/ /\n/g')
[[ $ERROR -eq 0 ]] && [[ ${#SELECTION_VALUES[@]} -gt 0 ]] && {
local SELECTION=$(echo $SELECTION_VALUES | FZF "select a value for '$NAME'")
[ $SELECTION ] && {
export $NAME=$SELECTION
return 0
}
}
[ $VALUE ] && return 0
[ $OPTIONAL ] && {
[ $DEFAULT_VALUE ] && $NAME="$DEFAULT_VALUE"
return 0
} || {
ERROR "'$NAME' required"
return 1
}
}

152
zsh/lib/utils/io.zsh Normal file
View File

@ -0,0 +1,152 @@
PRINT() {
local MESSAGE
local LAST_LINE_END='\n'
local STDERR=1
local STDOUT=0
local LTRIM=1
while [[ $# -gt 0 ]]
do
case $1 in
-n | --no-trim-tabs ) LTRIM=0 ;;
-x | --no-line-end ) LAST_LINE_END='' ;;
-o | --use-stdout ) STDOUT=1; STDERR=0 ;;
* ) MESSAGE+="$(echo $1) " ;;
esac
shift 1
done
local STYLED_MESSAGE="${COLOR}$({
printf "${COLOR}"
while IFS='' read line
do
[[ $PREFIX =~ ^[[:space:]]\+$ ]] && printf '\n'
printf "${PREFIX} : $(echo "$line" | sed 's/^ \+//; s/ \+$//')"
PREFIX=$(echo $PREFIX | sed 's/./ /g')
done <<< $MESSAGE
})${__COLOR_RESET}${LAST_LINE_END}"
[[ $STDERR -eq 1 ]] && printf $STYLED_MESSAGE >&2
[[ $STDOUT -eq 1 ]] && printf $STYLED_MESSAGE
return 0
}
[ ! $ERRORS ] && ERRORS=0
ERROR() { PREFIX="ERROR ✖" COLOR=$__RED PRINT "$@"; ((ERRORS+=1)); }
SUCCESS() { PREFIX="SUCCESS ✔" COLOR=$__GREEN PRINT "$@"; }
WARNING() { PREFIX="WARNING " COLOR=$__ORANGE PRINT "$@"; }
STATUS() { PREFIX="STATUS " COLOR=$__BLUE PRINT "$@"; }
REMINDER() { PREFIX="REMINDER " COLOR=$__PURPLE PRINT "$@"; }
INFO() { PREFIX="INFO " COLOR=$__WHITE PRINT "$@"; }
PROMPT() {
PREFIX="PROMPT " COLOR=$__CYAN PRINT "$@"
PREFIX="USER " COLOR=$__CYAN PRINT '' --no-line-end
}
FAIL() { ERROR "${@:2}"; exit $1; }
ABORT() { FAIL 69 'user abort'; }
CHECK_ERRORS() {
local FAIL_OUT=1
while [[ $# -gt 0 ]]
do
case $1 in
-n | --no-fail ) FAIL_OUT=0 ;;
esac
shift 1
done
[ ! $ERRORS ] && ERRORS=0
[[ $ERRORS -ne 0 ]] && USAGE
[[ $ERRORS -eq 0 ]] || {
[[ $FAIL_OUT -eq 1 ]] \
&& exit $ERRORS \
|| return $ERRORS
}
}
USAGE() {
[ ! $USAGE ] && return 0
USAGE=$(echo $USAGE | sed "s/^\t\+//; s/\s\+$//")
local USAGE_LINE=$(\
echo $USAGE \
| grep -i '^ *usage *:' \
| sed "s;^[^:]*:;& scwrypts $SCWRYPT_NAME --;" \
| sed 's/ \{2,\}/ /g; s/scwrypts -- scwrypts/scwrypts/' \
)
local THE_REST=$(echo $USAGE | grep -vi '^ *usage *:' | sed 'N;/^\n$/D;P;D;')
{ echo; printf "$__DARK_BLUE $USAGE_LINE$__COLOR_RESET\n"; echo $THE_REST; echo } >&2
}
INPUT() {
PROMPT "${@:2}"
READ $1
local VALUE=$(eval echo '$'$1)
[ $VALUE ]
}
Yn() {
PROMPT "$@ [Yn]"
[ $CI ] && { echo y; return 0; }
local Yn; READ -k Yn; echo
[[ $Yn =~ [nN] ]] && return 1 || return 0
}
yN() {
PROMPT "$@ [yN]"
[ $CI ] && { echo y; return 0; }
local yN; READ -k yN; echo
[[ $yN =~ [yY] ]] && return 0 || return 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; }
}
LESS() { less -R $@ </dev/tty >/dev/tty; }
FZF() {
[ $CI ] && {
ERROR 'currently in CI, but FZF requires user input'
exit 1
}
local SELECTION=$(fzf -i --height=30% --layout=reverse --prompt "$1 : " ${@:2})
PROMPT "$1"
echo $SELECTION >&2
echo $SELECTION
}
FZF_HEAD() { FZF $@ --print-query | sed '/^$/d' | head -n1; } # prefer user input over selected
FZF_TAIL() { FZF $@ --print-query | sed '/^$/d' | tail -n1; } # prefer selected over user input
READ() {
[ $CI ] && {
INFO 'currently in CI, skipping READ'
return 0
}
read $@ </dev/tty
}
EDIT() {
[ $CI ] && {
INFO 'currently in CI, skipping EDIT'
return 0
}
STATUS "opening '$1' for editing"
$EDITOR $@ </dev/tty >/dev/tty
SUCCESS "finished editing '$1'!"
}

12
zsh/lib/utils/os.zsh Normal file
View File

@ -0,0 +1,12 @@
IS_MACOS() { uname -s | grep -q 'Darwin'; }
OPEN() {
local OPEN=''
{
command -v xdg-open && OPEN=xdg-open
command -v open && OPEN=open
} >/dev/null 2>&1
[ ! $OPEN ] && { ERROR 'unable to detect default open command (e.g. xdg-open)'; return 1 }
$OPEN $@
}

View File

@ -0,0 +1,76 @@
#####################################################################
DEPENDENCIES+=(fzf) # (extensible) list of PATH dependencies
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
#####################################################################
source ${0:a:h}/dependencies.zsh
source ${0:a:h}/environment.zsh
#####################################################################
CHECK_ENVIRONMENT() {
local ENVIRONMENT_STATUS=0
__CHECK_DEPENDENCIES $DEPENDENCIES
local MISSING_DEPENDENCIES=$?
__CHECK_REQUIRED_ENV $REQUIRED_ENV
local MISSING_ENVIRONMENT_VARIABLES=$?
##########################################
local ERROR_MESSAGE=""
[[ $MISSING_DEPENDENCIES -ne 0 ]] && {
((ENVIRONMENT_STATUS+=1))
ERROR_MESSAGE+="\n$MISSING_DEPENDENCIES missing "
[[ $MISSING_DEPENDENCIES -eq 1 ]] \
&& ERROR_MESSAGE+='dependency' \
|| ERROR_MESSAGE+='dependencies' \
;
}
[[ $MISSING_ENVIRONMENT_VARIABLES -ne 0 ]] && {
((ENVIRONMENT_STATUS+=2))
ERROR_MESSAGE+="\n$MISSING_ENVIRONMENT_VARIABLES missing environment variable"
[[ $MISSING_ENVIRONMENT_VARIABLES -gt 1 ]] && ERROR_MESSAGE+=s
}
[ $IMPORT_ERRORS ] && [[ $IMPORT_ERRORS -ne 0 ]] && {
((ENVIRONMENT_STATUS+=4))
ERROR_MESSAGE+="\n$IMPORT_ERRORS import error"
[[ $IMPORT_ERRORS -gt 1 ]] && ERROR_MESSAGE+=s
}
##########################################
[[ ENVIRONMENT_STATUS -eq 0 ]] || {
ERROR_MESSAGE=$(echo $ERROR_MESSAGE | sed '1d; s/^/ /')
ERROR "environment errors found (see above)\n$ERROR_MESSAGE"
}
[[ $MISSING_ENVIRONMENT_VARIABLES -ne 0 ]] && {
REMINDER "
to quickly update missing environment variables, run:
'scwrypts zsh/scwrypts/environment/edit'
"
}
[[ $ENVIRONMENT_STATUS -eq 0 ]] || {
[[ $NO_EXIT -eq 1 ]] && return $ENVIRONMENT_STATUS
exit $ENVIRONMENT_STATUS
}
}
CHECK_ENVIRONMENT