From 29904ac3c40d92ddd92b68f8057e0596755ff5ae Mon Sep 17 00:00:00 2001 From: yage Date: Tue, 24 May 2022 10:26:05 -0600 Subject: [PATCH] [ ZSH / DEPENDENCIES ] Changed environment dependency checker to be more automatic --- .gitattributes | 1 + scwrypts | 8 ------- scwrypts.plugin.zsh | 12 +++++++--- zsh/aws/common.zsh | 21 ++++++++--------- zsh/aws/ecr/common.zsh | 12 ++++------ zsh/aws/ecr/login | 3 ++- zsh/aws/efs/common.zsh | 12 ++++------ zsh/aws/efs/mount | 3 ++- zsh/aws/efs/unmount | 3 ++- zsh/aws/rds/common.zsh | 6 +++++ zsh/aws/route53/backup | 3 ++- zsh/aws/route53/common.zsh | 12 ++++------ zsh/aws/s3/common.zsh | 9 ++------ zsh/aws/s3/media-sync/common.zsh | 16 +++++-------- zsh/aws/s3/media-sync/pull | 3 ++- zsh/aws/s3/media-sync/push | 3 ++- zsh/common.zsh | 20 +++------------- zsh/hello-world | 3 ++- zsh/scwrypts/common.zsh | 9 ++------ zsh/scwrypts/configure | 3 ++- zsh/scwrypts/environment/common.zsh | 9 ++------ zsh/scwrypts/environment/copy | 3 ++- zsh/scwrypts/environment/delete | 3 ++- zsh/scwrypts/environment/edit | 3 ++- zsh/scwrypts/environment/stage-variables | 3 ++- zsh/scwrypts/environment/synchronize | 3 ++- zsh/scwrypts/logs/clear | 3 ++- zsh/scwrypts/logs/common.zsh | 9 ++------ zsh/scwrypts/logs/view | 3 ++- zsh/scwrypts/virtualenv/common.zsh | 14 ++++-------- zsh/scwrypts/virtualenv/refresh | 3 ++- zsh/scwrypts/virtualenv/update-all | 3 ++- zsh/utils/credits.zsh | 3 +++ zsh/utils/dependencies.zsh | 12 ++++------ zsh/utils/environment.zsh | 15 ++++-------- zsh/utils/utils.zsh | 29 ++++++++++++++++++++++++ 36 files changed, 133 insertions(+), 147 deletions(-) create mode 100644 .gitattributes create mode 100644 zsh/aws/rds/common.zsh create mode 100644 zsh/utils/utils.zsh diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..febccb6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.zsh diff diff --git a/scwrypts b/scwrypts index 1e22f85..4ad5a80 100755 --- a/scwrypts +++ b/scwrypts @@ -1,14 +1,6 @@ #!/bin/zsh SCWRYPTS_ROOT="${0:a:h}" - source "$SCWRYPTS_ROOT/zsh/common.zsh" - -__CHECK_DEPENDENCIES \ - fzf \ - ; - -__CHECK_IMPORTS - ##################################################################### __RUN() { diff --git a/scwrypts.plugin.zsh b/scwrypts.plugin.zsh index 78d50e3..298db43 100644 --- a/scwrypts.plugin.zsh +++ b/scwrypts.plugin.zsh @@ -1,6 +1,8 @@ source ${0:a:h}/zsh/common.zsh ##################################################################### -[ ! $SCWRYPTS_SHORTCUT ] && return 0 +[ ! $SCWRYPTS_SHORTCUT ] && { + export SCWRYPTS_SHORTCUT='' # CTRL + SPACE +} __SCWRYPTS() { @@ -14,11 +16,14 @@ __SCWRYPTS() { RBUFFER+=" $SCRIPT" zle accept-line } + zle -N scwrypts __SCWRYPTS bindkey $SCWRYPTS_SHORTCUT scwrypts ##################################################################### -[ ! $SCWRYPTS_ENV_SHORTCUT ] && return 0 +[ ! $SCWRYPTS_ENV_SHORTCUT ] && { + export SCWRYPTS_ENV_SHORTCUT='' # CTRL + / +} __SCWRYPTS_ENV() { local RESET='reset' @@ -31,9 +36,10 @@ __SCWRYPTS_ENV() { [ $SELECTED ] && { [[ $SELECTED =~ ^$RESET$ ]] \ && RBUFFER='unset SCWRYPTS_ENV' \ - || RBUFFER="export SCWRYPTS_ENV=$SELECTED'" + || RBUFFER="export SCWRYPTS_ENV=$SELECTED" } zle accept-line } + zle -N scwrypts-setenv __SCWRYPTS_ENV bindkey $SCWRYPTS_ENV_SHORTCUT scwrypts-setenv diff --git a/zsh/aws/common.zsh b/zsh/aws/common.zsh index 3b35d0b..d8be006 100644 --- a/zsh/aws/common.zsh +++ b/zsh/aws/common.zsh @@ -1,16 +1,13 @@ +_DEPENDENCIES+=( + aws + jq +) +_REQUIRED_ENV+=( + _AWS_ACCOUNT + _AWS_PROFILE + _AWS_REGION +) source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - aws \ - jq \ - ; - -__CHECK_ENV_VARS \ - _AWS_ACCOUNT \ - _AWS_PROFILE \ - _AWS_REGION \ - ; - ##################################################################### _AWS() { aws --profile $_AWS_PROFILE --region $_AWS_REGION --output json $@; } diff --git a/zsh/aws/ecr/common.zsh b/zsh/aws/ecr/common.zsh index 1036412..e3d45e5 100644 --- a/zsh/aws/ecr/common.zsh +++ b/zsh/aws/ecr/common.zsh @@ -1,10 +1,6 @@ +_DEPENDENCIES+=( + docker +) +_REQUIRED_ENV+=() source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - docker \ - ; - -__CHECK_ENV_VARS \ - ; - ##################################################################### diff --git a/zsh/aws/ecr/login b/zsh/aws/ecr/login index 1c54551..330c50e 100755 --- a/zsh/aws/ecr/login +++ b/zsh/aws/ecr/login @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __STATUS "performing AWS ECR docker login" diff --git a/zsh/aws/efs/common.zsh b/zsh/aws/efs/common.zsh index 2862f08..b47f7e1 100644 --- a/zsh/aws/efs/common.zsh +++ b/zsh/aws/efs/common.zsh @@ -1,10 +1,6 @@ +_DEPENDENCIES+=() +_REQUIRED_ENV+=( + AWS__EFS__LOCAL_MOUNT_POINT +) source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - ; - -__CHECK_ENV_VARS \ - AWS__EFS__LOCAL_MOUNT_POINT \ - ; - ##################################################################### diff --git a/zsh/aws/efs/mount b/zsh/aws/efs/mount index d2bfb06..f5945a6 100755 --- a/zsh/aws/efs/mount +++ b/zsh/aws/efs/mount @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### _EFS_CONNECT() { diff --git a/zsh/aws/efs/unmount b/zsh/aws/efs/unmount index 757a1a8..fe46c42 100755 --- a/zsh/aws/efs/unmount +++ b/zsh/aws/efs/unmount @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### _EFS_DISCONNECT() { diff --git a/zsh/aws/rds/common.zsh b/zsh/aws/rds/common.zsh new file mode 100644 index 0000000..98e14ea --- /dev/null +++ b/zsh/aws/rds/common.zsh @@ -0,0 +1,6 @@ +_DEPENDENCIES+=( + psql +) +_REQUIRED_ENV+=() +source ${0:a:h}/../common.zsh +##################################################################### diff --git a/zsh/aws/route53/backup b/zsh/aws/route53/backup index d7087f7..1275a61 100755 --- a/zsh/aws/route53/backup +++ b/zsh/aws/route53/backup @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### _ROUTE53_BACKUP() { diff --git a/zsh/aws/route53/common.zsh b/zsh/aws/route53/common.zsh index e274a44..7464e76 100644 --- a/zsh/aws/route53/common.zsh +++ b/zsh/aws/route53/common.zsh @@ -1,10 +1,6 @@ +_DEPENDENCIES+=( + cli53 +) +_REQUIRED_ENV+=() source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - cli53 \ - ; - -__CHECK_ENV_VARS \ - ; - ##################################################################### diff --git a/zsh/aws/s3/common.zsh b/zsh/aws/s3/common.zsh index 179ab8d..1191a72 100644 --- a/zsh/aws/s3/common.zsh +++ b/zsh/aws/s3/common.zsh @@ -1,9 +1,4 @@ +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - ; - -__CHECK_ENV_VARS \ - ; - ##################################################################### diff --git a/zsh/aws/s3/media-sync/common.zsh b/zsh/aws/s3/media-sync/common.zsh index 220432f..37922d4 100644 --- a/zsh/aws/s3/media-sync/common.zsh +++ b/zsh/aws/s3/media-sync/common.zsh @@ -1,17 +1,13 @@ +_DEPENDENCIES+=() +_REQUIRED_ENV+=( + AWS__S3__MEDIA_TARGETS + AWS__S3__MEDIA_BUCKET +) source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - ; - -__CHECK_ENV_VARS \ - AWS__S3__MEDIA_TARGETS \ - AWS__S3__MEDIA_BUCKET \ - ; +##################################################################### AWS__S3__MEDIA_TARGETS=($(echo $AWS__S3__MEDIA_TARGETS | sed 's/,/\n/g')) -##################################################################### - __SYNC_MEDIA() { local ACTION="$1" local REMOTE_TARGET="s3://$AWS__S3__MEDIA_BUCKET/$2" diff --git a/zsh/aws/s3/media-sync/pull b/zsh/aws/s3/media-sync/pull index 4dba6f6..27874aa 100755 --- a/zsh/aws/s3/media-sync/pull +++ b/zsh/aws/s3/media-sync/pull @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __PULL_ALL_MEDIA() { diff --git a/zsh/aws/s3/media-sync/push b/zsh/aws/s3/media-sync/push index 062458f..95884c3 100755 --- a/zsh/aws/s3/media-sync/push +++ b/zsh/aws/s3/media-sync/push @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __PUSH_ALL_MEDIA() { diff --git a/zsh/common.zsh b/zsh/common.zsh index 4b21956..f1c2b85 100644 --- a/zsh/common.zsh +++ b/zsh/common.zsh @@ -1,3 +1,5 @@ +##################################################################### + [ ! $SCWRYPTS_ROOT ] && SCWRYPTS_ROOT="$(dirname ${0:a:h})" source $SCWRYPTS_ROOT/.config @@ -12,23 +14,7 @@ __NODE_VERSION=18.0.0 ##################################################################### -__DEPENDENCY_ERROR=0 -__ENVIRONMENT_ERROR=0 - -source ${0:a:h}/utils/io.zsh -source ${0:a:h}/utils/os.zsh -source ${0:a:h}/utils/credits.zsh -source ${0:a:h}/utils/dependencies.zsh -source ${0:a:h}/utils/environment.zsh - -##################################################################### - -__CHECK_IMPORTS() { - [[ $__DEPENDENCY_ERROR -eq 0 ]] \ - && [[ $__ENVIRONMENT_ERROR -eq 0 ]] \ - || __FAIL 1 'import error (see output above)' \ - ; -} +source ${0:a:h}/utils/utils.zsh ##################################################################### diff --git a/zsh/hello-world b/zsh/hello-world index 1f25471..e70ad43 100755 --- a/zsh/hello-world +++ b/zsh/hello-world @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __SUCCESS 'hello world!' diff --git a/zsh/scwrypts/common.zsh b/zsh/scwrypts/common.zsh index 179ab8d..1191a72 100644 --- a/zsh/scwrypts/common.zsh +++ b/zsh/scwrypts/common.zsh @@ -1,9 +1,4 @@ +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - ; - -__CHECK_ENV_VARS \ - ; - ##################################################################### diff --git a/zsh/scwrypts/configure b/zsh/scwrypts/configure index f30caac..fcd68f1 100755 --- a/zsh/scwrypts/configure +++ b/zsh/scwrypts/configure @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### [ ! -f $SCWRYPTS_CONFIG_PATH/config ] && { diff --git a/zsh/scwrypts/environment/common.zsh b/zsh/scwrypts/environment/common.zsh index 179ab8d..1191a72 100644 --- a/zsh/scwrypts/environment/common.zsh +++ b/zsh/scwrypts/environment/common.zsh @@ -1,9 +1,4 @@ +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - ; - -__CHECK_ENV_VARS \ - ; - ##################################################################### diff --git a/zsh/scwrypts/environment/copy b/zsh/scwrypts/environment/copy index 316a090..7587f5e 100755 --- a/zsh/scwrypts/environment/copy +++ b/zsh/scwrypts/environment/copy @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __PROMPT 'choose an environment to copy' diff --git a/zsh/scwrypts/environment/delete b/zsh/scwrypts/environment/delete index b80c5e9..7c6d61c 100755 --- a/zsh/scwrypts/environment/delete +++ b/zsh/scwrypts/environment/delete @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __PROMPT 'choose an environment to delete' diff --git a/zsh/scwrypts/environment/edit b/zsh/scwrypts/environment/edit index ebe65f6..3b2d2af 100755 --- a/zsh/scwrypts/environment/edit +++ b/zsh/scwrypts/environment/edit @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### [ $SCWRYPTS_ENV ] \ diff --git a/zsh/scwrypts/environment/stage-variables b/zsh/scwrypts/environment/stage-variables index aa658c1..455eba0 100755 --- a/zsh/scwrypts/environment/stage-variables +++ b/zsh/scwrypts/environment/stage-variables @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __CHECK_ENV_VARS $@ || NOPROMPT=1 $SCWRYPTS_ROOT/zsh/scwrypts/environment/synchronize diff --git a/zsh/scwrypts/environment/synchronize b/zsh/scwrypts/environment/synchronize index 0e23c97..e0538d5 100755 --- a/zsh/scwrypts/environment/synchronize +++ b/zsh/scwrypts/environment/synchronize @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### [ ! $NOPROMPT ] && { diff --git a/zsh/scwrypts/logs/clear b/zsh/scwrypts/logs/clear index 57e01b5..037210e 100755 --- a/zsh/scwrypts/logs/clear +++ b/zsh/scwrypts/logs/clear @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### cd $SCWRYPTS_ROOT diff --git a/zsh/scwrypts/logs/common.zsh b/zsh/scwrypts/logs/common.zsh index 179ab8d..1191a72 100644 --- a/zsh/scwrypts/logs/common.zsh +++ b/zsh/scwrypts/logs/common.zsh @@ -1,9 +1,4 @@ +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - ; - -__CHECK_ENV_VARS \ - ; - ##################################################################### diff --git a/zsh/scwrypts/logs/view b/zsh/scwrypts/logs/view index fcd721e..e251a31 100755 --- a/zsh/scwrypts/logs/view +++ b/zsh/scwrypts/logs/view @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### cd $SCWRYPTS_ROOT diff --git a/zsh/scwrypts/virtualenv/common.zsh b/zsh/scwrypts/virtualenv/common.zsh index bc9d208..cbb459e 100644 --- a/zsh/scwrypts/virtualenv/common.zsh +++ b/zsh/scwrypts/virtualenv/common.zsh @@ -1,13 +1,9 @@ +_DEPENDENCIES+=( + virtualenv + nodeenv +) +_REQUIRED_ENV+=() source ${0:a:h}/../common.zsh - -__CHECK_DEPENDENCIES \ - virtualenv \ - nodeenv \ - ; - -__CHECK_ENV_VARS \ - ; - ##################################################################### __AVAILABLE_VIRTUALENVS=(python node) diff --git a/zsh/scwrypts/virtualenv/refresh b/zsh/scwrypts/virtualenv/refresh index 1ad69ca..236f0bf 100755 --- a/zsh/scwrypts/virtualenv/refresh +++ b/zsh/scwrypts/virtualenv/refresh @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### ENV_TYPE=$(echo $__AVAILABLE_VIRTUALENVS | sed 's/ \+/\n/' | __FZF 'select an environment to refresh') diff --git a/zsh/scwrypts/virtualenv/update-all b/zsh/scwrypts/virtualenv/update-all index 4c5ddfb..dd7dd5b 100755 --- a/zsh/scwrypts/virtualenv/update-all +++ b/zsh/scwrypts/virtualenv/update-all @@ -1,6 +1,7 @@ #!/bin/zsh +_DEPENDENCIES+=() +_REQUIRED_ENV+=() source ${0:a:h}/common.zsh -__CHECK_IMPORTS ##################################################################### __STATUS 'beginning update for all environments' diff --git a/zsh/utils/credits.zsh b/zsh/utils/credits.zsh index e2b75c5..8e1a379 100644 --- a/zsh/utils/credits.zsh +++ b/zsh/utils/credits.zsh @@ -1,4 +1,7 @@ __CREDITS() { + # only applicable within scwrypts ("credits" pulled from README files) + [ ! $SCWRYPTS_ROOT ] && return 0 + local COMMAND="$1" cd $SCWRYPTS_ROOT cat ./**/README.md \ diff --git a/zsh/utils/dependencies.zsh b/zsh/utils/dependencies.zsh index 4dbc422..f738b10 100644 --- a/zsh/utils/dependencies.zsh +++ b/zsh/utils/dependencies.zsh @@ -1,19 +1,15 @@ __CHECK_DEPENDENCIES() { - local DEPENDENCY - for DEPENDENCY in $* - do - __CHECK_DEPENDENCY $DEPENDENCY || ((__DEPENDENCY_ERROR+=1)) - done + local DEP ERROR=0 - __CHECK_COREUTILS || ((__DEPENDENCY_ERROR+=$?)) + for DEP in $*; do __CHECK_DEPENDENCY $DEP || ((ERROR+=1)); done + __CHECK_COREUTILS || ((ERROR+=$?)) - return $__DEPENDENCY_ERROR + return $ERROR } __CHECK_DEPENDENCY() { local DEPENDENCY="$1" [ ! $DEPENDENCY ] && return 1 - command -v $DEPENDENCY >/dev/null 2>&1 || { __ERROR "'$1' required but not installed. $(__CREDITS $1)" return 1 diff --git a/zsh/utils/environment.zsh b/zsh/utils/environment.zsh index 1c18d98..73f67a5 100644 --- a/zsh/utils/environment.zsh +++ b/zsh/utils/environment.zsh @@ -1,14 +1,7 @@ -__CHECK_ENV_VARS() { - local OPTIONAL=0 - [[ $1 =~ ^--optional$ ]] && { shift 1; OPTIONAL=1; } - - local VAR_NAME - for VAR_NAME in $* - do - __CHECK_ENV_VAR $VAR_NAME $OPTIONAL || ((__ENVIRONMENT_ERROR+=1)) - done - - return $__ENVIRONMENT_ERROR +__CHECK_REQUIRED_ENV() { + local VAR ERROR=0 + for VAR in $*; do __CHECK_ENV_VAR $VAR_NAME || ((ERROR+=1)); done + return $ERROR } __CHECK_ENV_VAR() { diff --git a/zsh/utils/utils.zsh b/zsh/utils/utils.zsh new file mode 100644 index 0000000..4518b93 --- /dev/null +++ b/zsh/utils/utils.zsh @@ -0,0 +1,29 @@ +##################################################################### + +_DEPENDENCIES+=(fzf) # (extensible) list of PATH dependencies +_REQUIRED_ENV+=() # (extensible) list of required environment variables + +##################################################################### + +source ${0:a:h}/io.zsh +source ${0:a:h}/os.zsh +source ${0:a:h}/credits.zsh + +##################################################################### + +IMPORT_ERROR=0 + +source ${0:a:h}/dependencies.zsh +_DEPENDENCIES=($(echo $_DEPENDENCIES | sort -u)) +__CHECK_DEPENDENCIES $_DEPENDENCIES || ((IMPORT_ERROR+=$?)) + +source ${0:a:h}/environment.zsh +_REQUIRED_ENV=($(echo $__CHECK_REQUIRED_ENV | sort -u)) +__CHECK_REQUIRED_ENV $_REQUIRED_ENV || ((IMPORT_ERROR+=$?)) + +[[ $IMPORT_ERROR -eq 0 ]] || { + __ERROR "encountered $IMPORT_ERROR import error(s)" + return 1 +} + +#####################################################################