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

Finally decided to port personal scripts into a standalone library.

--- Release Notes ------------------------

- added support for python, zsh, and zx scripts
- added support for "interactive" scripts which drop the user to a REPL
- added support for passing arguments to commands
- added support for python/node virtualenv management through scwrypts
- added contributing and usage docs
- updated zsh plugin to write commands to history
- licensed under GPLv3

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

zsh/scwrypts )
  - configure
  - environment/copy
  - environment/delete
  - environment/edit
  - environment/synchronize
  - logs/clear
  - logs/view

zsh )
  - aws/ecr/login
  - aws/efs/mount
  - aws/efs/unmount
  - aws/route53/backup
  - aws/s3/media-sync/pull
  - aws/s3/media-sync/push

python )
  - redis/interactive
This commit is contained in:
2022-04-28 16:09:23 -06:00
commit bffd64051c
63 changed files with 2131 additions and 0 deletions

31
zsh/scwrypts/README.md Normal file
View File

@@ -0,0 +1,31 @@
# Meta Scwrypts
The fastest way to configure scwrypts is through scwrypts!
The ZSH scripts in this library are used to manage Scwrypts artifacts.
## Configure
**Great for first-time setup!**
It is simple to edit the local dot-config and restart your terminal.
It is much faster to hit `CTRL+W` and select `config/edit` through a fuzzy search.
This will immediately open your custom configuration file and reload any necessary resources on save.
## Environment
If you use Scwrypts, **you should use these commands all the time**.
This is your gateway to managing scwrypts sandboxed environments.
Command | Description
------------- | ---------------------------------------------------------------------------------------
`edit` | edit an existing environment; synchronizes environments if new variables are added
`copy` | copy an existing environment to a new one
`delete` | permanently delete an environment by name
`synchronize` | uses [template](../../.template.env) to add missing and remove extemporaneous variables
## Logs
Quickly view or clear Scwrypts logs.
## Virtualenv
In addition to the custom environment sandbox, scwrypts will load the appropriate virtual environment for the current script.
Update / create the environment with `update-all`.
Drop and recreate the environment with `refresh`.

9
zsh/scwrypts/common.zsh Normal file
View File

@@ -0,0 +1,9 @@
source ${0:a:h}/../common.zsh
__CHECK_DEPENDENCIES \
;
__CHECK_ENV_VARS \
;
#####################################################################

34
zsh/scwrypts/configure vendored Executable file
View File

@@ -0,0 +1,34 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
[ ! -f $SCWRYPTS_CONFIG_PATH/config ] && {
__STATUS 'first-time setup detected; creating local configuration override...'
cp $SCWRYPTS_ROOT/.config $SCWRYPTS_CONFIG_PATH/config \
&& __SUCCESS 'created!' \
|| __FAIL 1 "unable to create config at '$SCWRYPTS_CONFIG_PATH/config'"
__STATUS 'attempting to build virtual environments'
$SCWRYPTS_ROOT/zsh/scwrypts/virutalenv/update-all \
&& __SUCCESS 'finished updating virtualenvs' \
|| __WARNING 'unable to create one or more virtualenv (see above)' \
;
__REMINDER
__REMINDER 'use "zsh/scwrypts/virtualenv/update-all" to update environments'
__REMINDER '(equivalent to "npm install" or "pip install -r requirements.txt")'
__REMINDER
}
__STATUS 'opening local config for editing'
__EDIT $SCWRYPTS_CONFIG_PATH/config
__STATUS 'finished editing!'
which __SCWRYPTS >/dev/null 2>&1 && {
__STATUS 'reloading configuration for current session'
source $SCWRYPTS_ROOT/zsh/common.zsh \
|| __FAIL 2 'unable to reload configuration :c'
}
__SUCCESS 'saved new configuration'

View File

@@ -0,0 +1,9 @@
source ${0:a:h}/../common.zsh
__CHECK_DEPENDENCIES \
;
__CHECK_ENV_VARS \
;
#####################################################################

24
zsh/scwrypts/environment/copy Executable file
View File

@@ -0,0 +1,24 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
__PROMPT 'choose an environment to copy'
TEMPLATE_ENV_NAME=$(__SELECT_ENV)
[ ! $TEMPLATE_ENV_NAME ] && __ABORT
__STATUS "selected '$TEMPLATE_ENV_NAME'"
__PROMPT 'enter new environment name'
ENV_NAME=$(__FZF_HEAD 'new environment')
[ ! $ENV_NAME ] && __ABORT
TEMPLATE_ENV_FILE=$(__GET_ENV_FILE $TEMPLATE_ENV_NAME)
ENV_FILE=$(__GET_ENV_FILE $ENV_NAME)
[ -f "$ENV_FILE" ] && __FAIL 2 "'$ENV_NAME' already exists"
__STATUS "creating environment"
cp "$TEMPLATE_ENV_FILE" "$ENV_FILE" \
&& __SUCCESS "created '$ENV_NAME'" \
|| __FAIL 3 "unable to create '$ENV_NAME'"

24
zsh/scwrypts/environment/delete Executable file
View File

@@ -0,0 +1,24 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
__PROMPT 'choose an environment to delete'
ENV_NAME=$(__SELECT_ENV)
[ ! $ENV_NAME ] && __ABORT
ENV_FILE=$(__GET_ENV_FILE $ENV_NAME)
__STATUS "preparing to remove '$ENV_NAME'"
__WARNING
__WARNING "the '$ENV_NAME' environment will be removed"
__WARNING 'configured options and stored credentials will be lost forever'
__WARNING
__yN 'continue?' || __ABORT
__STATUS "removing environment"
rm "$ENV_FILE" \
&& __SUCCESS "removed '$ENV_NAME'" \
|| __FAIL 3 "unable to remove '$ENV_FILE'; is it protected?"

49
zsh/scwrypts/environment/edit Executable file
View File

@@ -0,0 +1,49 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
[ $SCWRYPTS_ENV ] \
&& ENV_NAME=$SCWRYPTS_ENV \
|| ENV_NAME=$(__SELECT_OR_CREATE_ENV)
[ ! $ENV_NAME ] && __ABORT
ENV_FILE=$(__GET_ENV_FILE $ENV_NAME)
[ ! -f $ENV_FILE ] && {
__STATUS "Creating '$ENV_NAME'..." \
&& cp $__ENV_TEMPLATE $ENV_FILE \
&& __SUCCESS 'created!' \
|| { __ERROR "failed to create '$ENV_FILE'"; exit 1; }
}
__STATUS "opening '$ENV_NAME' for editing..."
__EDIT $ENV_FILE
sed -i "s/^[A-Z]/export &/; s/^[^#=]\\+$/&=/" $ENV_FILE
LC_COLLATE=C sort -uo $ENV_FILE $ENV_FILE
__STATUS "finished editing; looking for new environment variables"
while read line
do
ENV_VAR=$(echo "$line" | sed 's/=.*$//; s/^export //')
grep -q "$ENV_VAR" $__ENV_TEMPLATE || {
((NEW_VAR+=1))
echo "export $ENV_VAR=" >> $__ENV_TEMPLATE
__STATUS "detected new variable '$ENV_VAR'"
}
done < $ENV_FILE
[ $NEW_VAR ] && {
LC_COLLATE=C sort -uo $__ENV_TEMPLATE $__ENV_TEMPLATE
NOPROMPT=1 ${0:a:h}/synchronize
git add $__ENV_TEMPLATE \
&& __SUCCESS "auto-staged the $(basename $__ENV_TEMPLATE) changes" \
|| {
__WARNING "unable to stage $(basename $__ENV_TEMPLATE) changes"
__REMINDER "don't forget to commit changes to $(basename $__ENV_TEMPLATE)"
}
true
} || {
__STATUS 'no new environment variables'
}
__SUCCESS "environment '$ENV_NAME' successfully modified"

View File

@@ -0,0 +1,6 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
__CHECK_ENV_VARS $@ || NOPROMPT=1 $SCWRYPTS_ROOT/zsh/scwrypts/environment/synchronize

View File

@@ -0,0 +1,46 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
[ ! $NOPROMPT ] && {
__yN 'change the template before sync?' && __EDIT $__ENV_TEMPLATE
sed -i "s/^[A-Z]/export &/; s/^[^#=]\\+$/&=/; s/=.*$/=/" $__ENV_TEMPLATE
LC_COLLATE=C sort -uo $__ENV_TEMPLATE $__ENV_TEMPLATE
git add $__ENV_TEMPLATE >/dev/null 2>&1
}
ENVIRONMENTS=$(__GET_ENV_FILES)
__STATUS 'inserting new environment variables...'
while read line
do
for ENV_FILE in $(echo $ENVIRONMENTS)
do
grep -q "^$line" $ENV_FILE || {
echo $line >> $ENV_FILE && __STATUS "added '$line' to '$ENV_FILE'"
}
done
done < <(sed -n '/^./p' $__ENV_TEMPLATE)
__STATUS 'removing old environment variables...'
for ENV_FILE in $(echo $ENVIRONMENTS)
do
while read line
do
ENV_VAR=$(echo "$line" | sed 's/=.*/=/')
grep -q "$ENV_VAR" $__ENV_TEMPLATE || {
sed -i "\\%$ENV_VAR%d" $ENV_FILE
__WARNING "removed unwanted '$ENV_VAR' from '$ENV_FILE'"
}
done < $ENV_FILE
done
for ENV_FILE in $(echo $ENVIRONMENTS)
do
sed -i "s/^[A-Z]/export &/; s/^[^#=]\\+$/&=/" $ENV_FILE
LC_COLLATE=C sort -uo $ENV_FILE $ENV_FILE
done
LC_COLLATE=C sort -uo $__ENV_TEMPLATE $__ENV_TEMPLATE
__SUCCESS 'finished sync!'

20
zsh/scwrypts/logs/clear Executable file
View File

@@ -0,0 +1,20 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
cd $SCWRYPTS_ROOT
__STATUS "Found $(ls $SCWRYPTS_LOG_PATH | wc -l) log files"
__WARNING
__WARNING 'this will permanently clear all local cloud script logs found in'
__WARNING "'$SCWRYPTS_LOG_PATH'"
__WARNING
__yN 'continue?' || __ABORT
__STATUS 'removing logfiles'
rm -rf $SCWRYPTS_LOG_PATH/* \
&& __SUCCESS 'done' \
|| { __ERROR 'failed :c'; exit 2; }\
;

View File

@@ -0,0 +1,9 @@
source ${0:a:h}/../common.zsh
__CHECK_DEPENDENCIES \
;
__CHECK_ENV_VARS \
;
#####################################################################

13
zsh/scwrypts/logs/view Executable file
View File

@@ -0,0 +1,13 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
cd $SCWRYPTS_ROOT
__PROMPT 'select a script log'
LOG_FILE=$(ls $SCWRYPTS_LOG_PATH | __FZF 'logfile')
[ ! $LOG_FILE ] && { __ERROR 'user abort'; exit 1; }
__STATUS 'opening logfile'
__LESS "$SCWRYPTS_LOG_PATH/$LOG_FILE"
__SUCCESS 'done'

View File

@@ -0,0 +1,128 @@
source ${0:a:h}/../common.zsh
__CHECK_DEPENDENCIES \
virtualenv \
nodeenv \
;
__CHECK_ENV_VARS \
;
#####################################################################
__AVAILABLE_VIRTUALENVS=(python node)
#####################################################################
__REFRESH_VIRTUALENV() {
local TYPE="$1"
[ ! $TYPE ] && {
__ERROR 'no virtualenv type specified'
return 1
}
__STATUS "refreshing $TYPE virtual environment"
__DELETE_VIRTUALENV $TYPE \
&& __UPDATE_VIRTUALENV $TYPE \
&& __SUCCESS 'successfully refreshed virtual environment' \
|| { ERROR 'something went wrong during refresh (see above)'; return 1; } \
;
}
__UPDATE_VIRTUALENV() {
local TYPE="$1"
[ ! $TYPE ] && {
__ERROR 'no virtualenv type specified'
return 1
}
local VIRTUALENV_PATH=$(__GET_VIRTUALENV_PATH $TYPE)
[ ! -d $VIRTUALENV_PATH ] && __CREATE_VIRTUALENV_$TYPE $VIRTUALENV_PATH
__STATUS "updating $TYPE virtual environment"
source $VIRTUALENV_PATH/bin/activate || {
__ERROR 'failed to activate virtualenv; did create fail?'
return 1
}
cd $VIRTUALENV_PATH/../
local UPDATE_CODE=0
case $TYPE in
python ) pip install -r requirements.txt; UPDATE_CODE=$? ;;
node ) npm install ;
esac
UPDATE_CODE=$?
[[ $UPDATE_CODE -eq 0 ]] \
&& __SUCCESS "$TYPE virtual environment up-to-date" \
|| __ERROR "failed to update $TYPE virtual environment (see errors above)" \
;
deactivate_node >/dev/null 2>&1
deactivate >/dev/null 2>&1
return $UPDATE_CODE
}
__DELETE_VIRTUALENV() {
local TYPE="$1"
local VIRTUALENV_PATH="$(__GET_VIRTUALENV_PATH $TYPE)"
__STATUS "dropping $TYPE virtual environment artifacts"
[ ! -d $VIRTUALENV_PATH ] && {
__SUCCESS "no $TYPE environment detected"
return 0
}
rm -rf $VIRTUALENV_PATH \
&& __SUCCESS "succesfully cleaned up $TYPE virtual environment" \
|| { __ERROR "unabled to remove '$VIRTUALENV_PATH'"; return 1; }
}
__GET_VIRTUALENV_PATH() {
local TYPE="$1"
case $TYPE in
python ) echo "$SCWRYPTS_ROOT/py/.env" ;;
node ) echo "$SCWRYPTS_ROOT/zx/.env" ;;
esac
}
#####################################################################
__CREATE_VIRTUALENV_python() {
local VIRTUALENV_PATH="$1"
__STATUS 'creating python virtual environment'
local PY PYTHON
for PY in $(echo $__PREFERRED_PYTHON_VERSIONS)
do
which python$PY >/dev/null 2>&1 && {
PYTHON=$(which python$PY)
break
}
done
[ ! $PYTHON ] && {
__ERROR 'python>=3.9 not available; skipping python env'
return 1
}
__STATUS 'setting up virtualenv'
virtualenv $VIRTUALENV_PATH --python="$PYTHON" \
&& __SUCCESS 'python virtualenv created' \
|| {
__ERROR "unable to create '$VIRTUALENV_PATH' with '$PYTHON'"
return 2
}
}
__CREATE_VIRTUALENV_node() {
local VIRTUALENV_PATH="$1"
__STATUS 'setting up nodeenv'
nodeenv $VIRTUALENV_PATH --node=$__NODE_VERSION \
&& __SUCCESS 'node virtualenv created' \
|| {
__ERROR "unable to create '$VIRTUALENV_PATH' with '$__NODE_VERSION'"
return 2
}
}

17
zsh/scwrypts/virtualenv/refresh Executable file
View File

@@ -0,0 +1,17 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
ENV_TYPE=$(echo $__AVAILABLE_VIRTUALENVS | sed 's/ \+/\n/' | __FZF 'select an environment to refresh')
[ ! $ENV_TYPE ] && __ABORT
__REMINDER
__REMINDER "this will permanently remove all artifacts for the scwrypts $ENV_TYPE environment"
__REMINDER "(safe unless you have put something important in $(__GET_VIRTUALENV_PATH $ENV_TYPE))"
__REMINDER
__Yn "drop and recreate $ENV_TYPE virtual environment?" || __ABORT
__REFRESH_VIRTUALENV $ENV_TYPE

View File

@@ -0,0 +1,17 @@
#!/bin/zsh
source ${0:a:h}/common.zsh
__CHECK_IMPORTS
#####################################################################
__STATUS 'beginning update for all environments'
FAILED_COUNT=0
for ENV_TYPE in $(echo $__AVAILABLE_VIRTUALENVS)
do
__UPDATE_VIRTUALENV $ENV_TYPE || ((FAILED_COUNT+=1))
done
[[ $FAILED_COUNT -eq 0 ]] \
&& __SUCCESS 'all environments up-to-date' \
|| __FAIL $FAILED_COUNT 'failed to update one or more environments'