Compare commits

..

21 Commits

Author SHA1 Message Date
d4ef1c70e0 plugins/ci migration from v3 to v4 2024-02-12 23:45:37 -07:00
c9e107d2fd plugins/kubectl migration from v3 to v4 2024-02-12 23:45:05 -07:00
b6b4f2e5b8 FZF_(HEAD|TAIL) refactor to FZF_USER_INPUT 2024-02-12 23:44:43 -07:00
432593a0f3 update ZLE plugin so it no more make errors 2024-02-12 23:43:28 -07:00
6629caf459 FINALLY fix the weird cases for zsh/read builtin (particularly around reading one character from tty/pipe/file); also gave a --force-user-input flag in case you want to require user input on a yn prompt 2024-02-12 23:42:55 -07:00
8bcc99b898 improved i/o handling on the run executable means this is no longer relevant 2024-02-12 23:41:42 -07:00
05694ed022 bring some much-needed tender love and care to the scwrypts runner 2024-02-12 23:39:56 -07:00
67bd712590 v3-to-v4 upgrade docs 2024-02-12 23:39:00 -07:00
a90482de8c swap INFO for DEBUG 2024-02-07 15:16:51 -07:00
261bbee1a4 introduce --verbosity flag rather than mixed logging settings; correct color misnaming to ANSI convention; added sanity-check; simplified hello-world; created FZF_USER_INPUT to replace the confusing FZF_HEAD and FZF_TAIL 2024-02-07 15:14:36 -07:00
fcf492c661 basic runner format; write a MAIN function 2024-02-06 14:06:44 -07:00
a200c1eb22 v3.9.1
=====================================================================

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

- Moved personal-environment specific scwrypts to wrynegade/dotwryn
2023-12-23 10:23:38 -07:00
f3e70c61cb v3.9.0
=====================================================================

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

- scwrypts runner has new arguments
  -q/--quiet   allows quiet-mode operation while still logging to logfiles
  -v/--verbose forces verbose mode
    --version  longform required (-v is now for "verbose" mode)

- scwrypts runner now auto-detects certain CLI usage, running in quiet,
  logged mode if pattern match successfully identifies a single scwrypt
  (or when using --name); use --verbose to override this behavior

- 'k exec' no longer requires double '--' if a '--' comes after
  - old : k exec -it my-pod-0 -- -- /bin/sh
  + new : k exec -it my-pod-0 -- /bin/sh
  + still works : k -- exec -it my-pod-0 -- /bin/sh

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

- fixed various plugins/kubectl auto-completion settings; arguments
  after '--' or profile number (e.g. 'k 1 get deployments') will now
  appropriately autocomplete in the indicated profile

- helm template functions now work on related .tpl files as well
  (renders from chart root)

- fixed some goofy UTF-8 icons in zsh/lib/utils/io

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

- (experimental) scwrypts zsh plugin for interactive command selection
  (like CTRL+SPACE), but allows you to build command arguments,
  providing help dialogue for the selected command

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

- zsh/misc/tally ) helps keep tally-counts of things; helpful when
                   running long scripts "what iteration am I on"
2023-12-11 18:19:37 -07:00
72e831da33 v3.8.0
=====================================================================

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

- kubectl driver updates; getting better, but still need to fix
  autocomplete in certain circumstances

- added -y|--yes flags to scwrypts to auto-accept user-prompts (use with
  caution)

- figured out the whole mikefarah/yq vs kislyuk/yq thing; use YQ for
  compatiblity

--- Bug fixes ----------------------------

- helm template generation now loads values in a more appropriate order
  which prevents overwrite by the wrong values file
2023-11-22 15:54:16 -07:00
a03885e8db v3.7.8
=====================================================================

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

- --list-envs now shows all available environments
2023-11-13 16:19:05 -07:00
6cc10e3f4f v3.7.7
=====================================================================

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

- GitHub Actions now autodetects groups within the "$GITHUB_WORKSPACE"
2023-11-13 13:16:04 -07:00
4a1208942d v3.7.6
=====================================================================

Documentation update
2023-11-13 12:30:49 -07:00
91780024f0 v3.7.5
=====================================================================

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

- adding variables of the format `^SCWRYPTS_GROUP_LOADERS__[a-z_]\+=`
  will let those files be explicitly sourced during run (this should
  allow custom group usage in CI)
2023-11-13 12:27:15 -07:00
3ca4fe0c65 v3.7.4
=====================================================================

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

- fixed typo in Vundle.vim clone
2023-11-11 15:27:34 -07:00
e6dfff255c v3.7.3
=====================================================================

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

- Helm template generation looks for default values in file://
  dependencies in addition to the standard values locations

- vundle installer now *actually installs Vundle.vim* if it is missing
2023-11-11 15:13:30 -07:00
15942bb08d v3.7.2
=====================================================================

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

- Various github-actions fixes
2023-11-10 17:41:01 -07:00
88 changed files with 1803 additions and 1257 deletions

View File

@ -1,16 +0,0 @@
---
name: Random Test
on: push # yamllint disable-line rule:truthy
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: run a scwrypts thing
uses: wrynegade/scwrypts@main
with:
scwrypt: --name hello-world --group scwrypts --type py
args: --message "hello from github actions ci <3"
- name: cat config stuff
shell: bash
run: ls ~/.config/scwrypts; cat ~/.config/scwrypts/*

View File

@ -6,6 +6,9 @@ In modern developer / dev-ops workflows, scripts require a complex configuration
Without a better solution, the developer is cursed to copy lines-upon-lines of variables into terminals, create random text artifacts, or maybe even commit secure credentials into source.
Scwrypts leverages ZSH to give hot-key access to run scripts in such environments.
## Major Version Upgrade Notice
Please refer to [Version 3 to Version 4 Upgrade Path](./docs/upgrade/v3-to-v4.md) when upgrading from scwrypts v3 to scwrypts v4!
## Dependencies
Due to the wide variety of resources used by scripting libraries, the user is expected to manually resolve dependencies.
@ -48,6 +51,8 @@ There are a few notable changes to this runtime:
- User yes/no prompts will **always be YES**
- Other user input will default to an empty string
- Logs will not be captured
- Setting the environment variable `SCWRYPTS_GROUP_LOADER__[a-z_]\+` will source the file indicated in the variable (this allows custom groups without needing to modify the `config.zsh` directly)
- In GitHub actions, `*.scwrypts.zsh` groups are detected automatically from the `$GITHUB_WORKSPACE`; set `SCWRYPTS_GITHUB_NO_AUTOLOAD=true` to disable
## Contributing

View File

@ -27,7 +27,7 @@ runs:
- uses: actions/checkout@v4
with:
repository: wrynegade/scwrypts
path: $HOME/scwrypts
path: ./wrynegade/scwrypts
ref: ${{ inputs.version }}
- name: check dependencies
@ -43,7 +43,7 @@ runs:
sudo apt-get update
sudo apt-get install --yes zsh fzf ripgrep
for D in $($HOME/scwrypts/scwrypts -n --name check-all-dependencies --group ci --type zsh)
for D in $($GITHUB_WORKSPACE/wrynegade/scwrypts/scwrypts -n --name check-all-dependencies --group ci --type zsh)
do
echo "--- installing $D ---"
( sudo apt-get install --yes $D; exit 0; )
@ -51,7 +51,7 @@ runs:
} > $HOME/.scwrypts.apt-get.log 2>&1
echo "updating virtual dependencies"
$HOME/scwrypts/scwrypts -n \
$GITHUB_WORKSPACE/wrynegade/scwrypts/scwrypts -n \
--name scwrypts/virtualenv/update-all \
--group scwrypts \
--type zsh \
@ -65,4 +65,4 @@ runs:
env:
CI: true
SCWRYPTS_ENV: ${{ inputs.scwrypts-env }}
run: $HOME/scwrypts/scwrypts ${{inputs.scwrypt}} -- ${{inputs.args}} || exit 1
run: $GITHUB_WORKSPACE/wrynegade/scwrypts/scwrypts ${{inputs.scwrypt}} -- ${{inputs.args}} || exit 1

254
docs/upgrade/v3-to-v4.md Normal file
View File

@ -0,0 +1,254 @@
# Scwrypts Upgrade v3 to v4 Notes
Scwrypts v4 brings a big update to the *runstring for `zsh`-type scwrypts*.
I've found some of the boilerplate required by each individual script to be confusing and terse, and the goal here is to make it easier and safer to write and run scwrypts in this critical format.
Jump to [Technical Bits](#technical-bits) if you just want to get started with migration steps.
The actual migration _should take less than a minute per script_.
This document is deliberately verbose for future reference when I don't remember how any of this works.
## Ideology and History
Originally (scwrypts v2 and below) wanted to preserve the direct-use of individual scwrypts.
In those versions, executable files could be executed directly (outside of the `scwrypts` function) and still operate with minimal, unwanted consequences.
This resulted in a rigid structure which made code-sharing difficult at small scales and untenable in many non-trivial cases.
Scwrypts v3, begrudgingly introduced a pseudo-import syntax with `use`.
This sought to combat the issues of code-sharing and open up the structure of executable scwrypts to the scwrypts-writer.
Beyond just clarity, this allowed external libraries to be written and cross-referenced.
Although "importing" is an odd (anti?)feature to shell scripting, the way libraries could be written and reused was too helpful and I succumbed to write the `import.driver.zsh` module.
Throughout v3, I tried to maintain the "executability" of individual scwrypts.
It's ugly though.
Every individual scwrypt relies on `import.driver.zsh` and the context set up by the `scwrypts` executable.
While you _could_ run the executable file directly, it would misbehave at-best and fail pretty reliably.
So... here's v4!
Scwrypts v4 accepts the reality that, although `zsh` scwrypts are zsh, they do not stand alone without the proper context setup provided by `scwrypts`.
To improve usability, I've abstracted much of the boilerplate so you never have to see it.
I've injected safety mechanisms like `--help` arguments and utility mechanisms like flag separation (`-abc` is really `-a -b -c`) into all v4 zsh scwrypts.
You don't have to worry about checking the context, v4 does that for you!
You don't have to worry about execution, v4 does that for you!
So!
Are you coupling your zsh scripts to `scwrypts` when you write them? Yes.
Is that a bad thing? I don't think so.
Shell-scripting is such a critical coupler to real-life systems.
High-risk-high-impact to SLAs means we cannot allow context mistakes by sysadmins and users.
Reusability between local machine, cloud runtime, and CI pipelines is a must.
And if you have a need to reign all that in to single, isolated executable files...
...then good luck <3
## Technical Bits
Big idea: let's get rid of v3 boilerplate and make things easy.
### Your executable must be in a MAIN function
A main function in shell scripts?
Weird!
Don't worry, it's easy.
Take your original scwrypt, and slap the executable stuff into a function called `MAIN` (yes, it must be _exactly_, all-caps `MAIN`):
```diff
#!/bin/zsh
#####################################################################
DEPENDENCIES+=(dep-function-a dep-function-b)
REQUIRED_ENV+=()
use do/awesome/stuff --group my-custom-library
CHECK_ENVIRONMENT
#####################################################################
- echo "do some stuff here"
- # ... etc ...
- SUCCESS "completed the stuff"
+ MAIN() {
+ echo "do some stuff here"
+ # ... etc ...
+ SUCCESS "completed the stuff
+ }
```
**Don't invoke the function!**
Scwrypts will now do that on your behalf.
I've already written many scwrypts which _almost_ used this syntax.
All I had to do in this case was delete the function invocation at the end:
```diff
# ... top boilerplate ...
MAIN() {
SUCCESS "look at me I'm so cool I already wrote this in a main function"
}
-
- #####################################################################
- MAIN $@
```
Again, **do not invoke the function**. Just name it `MAIN` and you're good-to-go!
### Great news!
Great news!
We have finished with *all of the necessary steps* to migrate to v4!
Easy!
While you're here, let's do a couple more things to cleanup your scwrypts (I promise they are also easy and will take less than a few seconds for each)!
### Remove the boilerplate
Were you confused by all that garbage at the top?
Awesome!
Just get rid of any of it you don't use.
While you _probably_ will still need whatever dependencies you already defined, feel free to get rid of empty config lists like `DEPENDENCIES+=()`.
For non-empty lists, the syntax remains the same (use the `+=` and make sure it's an array-type `()` just like before!)
Also you can ditch the `CHECK_ENVIRONMENT`.
While it won't hurt, v4 already does this, so just get rid of it.
Here's my recommended formatting:
```diff
#!/bin/zsh
- #####################################################################
DEPENDENCIES+=(dep-function-a dep-function-b)
- REQUIRED_ENV+=()
use do/awesome/stuff --group my-custom-library
-
- CHECK_ENVIRONMENT
#####################################################################
MAIN() {
echo "do some stuff here"
# ... etc ...
SUCCESS "completed the stuff
}
```
### Get rid of `--help` argument processing
Scwrypts v4 injects the `--help` argument into every zsh scwrypt.
So there's no need to process it manually anymore!
We can now eliminate the help case from any MAIN body or library function:
```diff
MAIN() {
while [[ $# -gt 0 ]]
do
case $1 in
# ... a bunch of cases ...
- -h | --help ) USAGE; return 0 ;;
# ... a bunch of cases ...
esac
shift 1
done
}
```
While you probably weren't doing this, you can also do the same for any logic which splits arguments input like `-abc` which should be read as `-a -b -c`.
If you know how to do this, you know how to get rid of it.
### Write some help docs
Okay this one might take a little bit of time if you haven't done it already (but this is the last recommended step! hang in there and make your stuff better!).
If you _have_ done it already, typically by writing a variable called "USAGE" in your code, maybe consider the _new and improved_ way to write your help strings.
Returning to our original `MAIN()` example, I'll add some options parsing so we should now look something like this:
```sh
#!/bin/zsh
DEPENDENCIES+=(dep-function-a dep-function-b)
use do/awesome/stuff --group my-custom-library
#####################################################################
MAIN() {
local A
local B=false
local ARGS=()
while [[ $# -gt 0 ]]
do
case $1 in
-a | --option-a ) A=$2; shift 1 ;;
-b | --allow-b ) B=true ;;
* ) ARGS+=($1) ;;
esac
shift 1
done
echo "A : $A\nB : $B\nARGS : $ARGS"
}
```
All we have to do is add some usage variables and we're done!
I want to call out a few specific ones:
- `USAGE__options` provides descriptions for CLI flags like `-a` `--some-flag` and `-a <some value>` (reminder, you *don't* need '-h, --help' anymore!)
- `USAGE__args` provides descriptions for non-flag CLI arguments, where order matters (e.g. `cat file-a file-b ... etc`)
- `USAGE__description` provides the human-readable description of what your function does
- `USAGE__usage` you probably don't need to adjust this one, but it will be everything after the `--` in the usage-line. Defaults to include `[...options...]`, but I suppose you might want to write `USAGE__usage+=' [...args...]` if you 1) have args and 2) are really specific about your help strings.
Just add another section to define these values before declaring `MAIN`:
```sh
#!/bin/zsh
DEPENDENCIES+=(dep-function-a dep-function-b)
use do/awesome/stuff --group my-custom-library
#####################################################################
USAGE__options='
-a, --option-a <string> sets the value of the A variable
-b, --allow-b enables the B option
'
# remember there's no specific formatting here, just write it nice
USAGE__args='
N-args All remaining args are inserted into the ARGS variable
'
USAGE__description="
This is my cool example function. It's really neato, but does
very little.
"
#####################################################################
MAIN() {
# ... etc ...
}
```
Now, when we run `scwrypts my sample -- --help`, we get:
```txt
usage: scwrypts my sample -- [...options...]
args:
N-args All remaining args are inserted into the ARGS variable
options:
-a, --option-a <string> sets the value of the A variable
-b, --allow-b enables the B option
-h, --help display this message and exit
This is my cool example function. It's really neato, but does
very little.
```
### All done
No more recommendations at this time.
Someday I'll have an auto-formatter and a language server to help with go-to-definition, but that's still for the future.
Thanks for your time and welcome to v4!

View File

@ -1,9 +1,5 @@
#!/bin/zsh
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
#####################################################################
MAIN() {
cd "$SCWRYPTS_ROOT__scwrypts/"
@ -35,6 +31,3 @@ MAIN() {
echo $DEPENDENCIES | sed 's/ /\n/g'
CHECK_ENVIRONMENT && SUCCESS "all dependencies satisfied"
}
#####################################################################
MAIN $@

View File

@ -2,30 +2,45 @@
command -v compdef >/dev/null 2>&1 || return 0
#####################################################################
_k() {
local C=$(k meta get context)
local NS=$(k meta get namespace)
for CLI in kubectl helm flux
do
eval "_${CLI[1]}() {
local SUBSESSION=0
echo \${words[2]} | grep -q '^[0-9]\\+$' && SUBSESSION=\${words[2]}
local KUBEWORDS=(kubectl)
[ $C ] && KUBEWORDS+=(--context $C)
[ $NS ] && KUBEWORDS+=(--namespace $NS)
local PASSTHROUGH_WORDS=($CLI)
[[ \$CURRENT -gt 2 ]] && echo \${words[2]} | grep -qv '^[0-9]\\+$' && {
local KUBECONTEXT=\$(k \$SUBSESSION meta get context)
local NAMESPACE=\$(k \$SUBSESSION meta get namespace)
words="$KUBEWORDS ${words[@]:1}"
_kubectl
[ \$KUBECONTEXT ] \
&& PASSTHROUGH_WORDS+=($([[ $CLI =~ ^helm$ ]] && echo '--kube-context' || echo '--context') \$KUBECONTEXT) \
;
[ \$NAMESPACE ] \
&& PASSTHROUGH_WORDS+=(--namespace \$NAMESPACE) \
;
}
compdef _k k
local DELIMIT_COUNT=0
local WORD
for WORD in \${words[@]:1}
do
case \$WORD in
[0-9]* ) continue ;;
-- )
echo \$words | grep -q 'exec' && ((DELIMIT_COUNT+=1))
[[ \$DELIMIT_COUNT -eq 0 ]] && ((DELIMIT_COUNT+=1)) && continue
;;
esac
PASSTHROUGH_WORDS+=(\"\$WORD\")
done
#####################################################################
_h() {
local C=$(k meta get context)
local NS=$(k meta get namespace)
echo \"\$words\" | grep -q '\\s\\+$' && PASSTHROUGH_WORDS+=(' ')
local KUBEWORDS=(kubectl)
[ $C ] && KUBEWORDS+=(--context $C)
[ $NS ] && KUBEWORDS+=(--namespace $NS)
words="$KUBEWORDS ${words[@]:1}"
_helm
words=\"\$PASSTHROUGH_WORDS\"
_$CLI
}
compdef _h h
"
compdef _${CLI[1]} ${CLI[1]}
done

View File

@ -3,6 +3,7 @@
unalias k h >/dev/null 2>&1
k() { _SCWRYPTS_KUBECTL_DRIVER kubectl $@; }
h() { _SCWRYPTS_KUBECTL_DRIVER helm $@; }
f() { _SCWRYPTS_KUBECTL_DRIVER flux $@; }
_SCWRYPTS_KUBECTL_DRIVER() {
@ -57,7 +58,7 @@ _SCWRYPTS_KUBECTL_DRIVER() {
"
local USAGE__description="
Provides 'k' (kubectl) and 'h' (helm) shorthands to the respective
Provides 'k' (kubectl), 'h' (helm), and 'f' (flux) shorthands to the respective
utility. These functions leverage redis and scwrypts environments to
allow quick selection of contexts and namespaces usable across all
active shell instances.
@ -100,18 +101,20 @@ _SCWRYPTS_KUBECTL_DRIVER() {
--subsession ) SUBSESSION=$2; shift 1 ;;
-n | --namespace )
echo "TODO: set namespace ('$2')" >&2
USER_ARGS+=(--namespace $2); shift 1
;;
-k | --context | --kube-context )
echo "TODO: set context ('$2')" >&2
[[ $CLI =~ ^helm$ ]] && USER_ARGS+=(--kube-context $2)
[[ $CLI =~ ^kubectl$ ]] && USER_ARGS+=(--context $2)
_SCWRYPTS_KUBECTL_DRIVER kubectl meta set namespace $2
shift 1
;;
-- ) shift 1; break ;;
-k | --context | --kube-context )
_SCWRYPTS_KUBECTL_DRIVER kubectl meta set context $2
shift 1
;;
-- )
echo $USER_ARGS | grep -q 'exec' && USER_ARGS+=(--)
shift 1
break
;;
* )
[ ! $CUSTOM_COMMAND ] && {
@ -149,6 +152,7 @@ _SCWRYPTS_KUBECTL_DRIVER() {
[ $CONTEXT ] && [[ $CLI =~ ^helm$ ]] && CLI_ARGS+=(--kube-context $CONTEXT)
[ $CONTEXT ] && [[ $CLI =~ ^kubectl$ ]] && CLI_ARGS+=(--context $CONTEXT)
[ $CONTEXT ] && [[ $CLI =~ ^flux$ ]] && CLI_ARGS+=(--context $CONTEXT)
[[ $STRICT -eq 1 ]] && {
[ $CONTEXT ] || ERROR "missing kubectl 'context'"

View File

@ -30,8 +30,15 @@ SCWRYPTS_KUBECTL_CUSTOM_COMMAND_PARSE__meta() {
USAGE__args="set (namespace|context)"
USAGE__description="interactively set a namespace or context for '$SCWRYPTS_ENV'"
case $2 in
namespace | context ) USER_ARGS+=($1 $2) ;;
namespace | context ) USER_ARGS+=($1 $2 $3); [ $3 ] && shift 1 ;;
-h | --help ) HELP=1 ;;
'' )
: \
&& SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta set context \
&& SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta set namespace \
;
return $?
;;
* ) ERROR "cannot set '$2'" ;;
esac
@ -94,7 +101,7 @@ SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta() {
;;
set )
scwrypts -n --name set-$2 --type zsh --group kubectl -- --subsession $SUBSESSION >/dev/null \
scwrypts -n --name set-$2 --type zsh --group kubectl -- $3 --subsession $SUBSESSION >/dev/null \
&& SUCCESS "$2 set"
;;

View File

@ -1,16 +1,7 @@
#!/bin/zsh
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
use kubectl --group kubectl
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
KUBECTL__GET_CONTEXT
}
#####################################################################
MAIN $@

View File

@ -1,16 +1,7 @@
#!/bin/zsh
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
use kubectl --group kubectl
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
KUBECTL__GET_NAMESPACE
}
#####################################################################
MAIN $@

View File

@ -21,7 +21,6 @@ KUBECTL() {
kubectl ${KUBECTL_ARGS[@]} $@
}
#####################################################################
KUBECTL__GET_CONTEXT() { REDIS get --prefix "current:context"; }
@ -87,3 +86,73 @@ KUBECTL__LIST_NAMESPACES() {
echo default
KUBECTL get namespaces -o name | sed 's/^namespace\///' | sort
}
#####################################################################
KUBECTL__SERVE() {
[ $CONTEXT ] || local CONTEXT=$(KUBECTL__GET_CONTEXT)
[ $CONTEXT ] || ERROR 'must configure a context in which to serve'
[ $NAMESPACE ] || local NAMESPACE=$(KUBECTL__GET_NAMESPACE)
[ $NAMESPACE ] || ERROR 'must configure a namespace in which to serve'
CHECK_ERRORS --no-fail --no-usage || return 1
[ $SERVICE ] && SERVICE=$(KUBECTL__LIST_SERVICES | jq -c "select (.service == \"$SERVICE\")" || echo $SERVICE)
[ $SERVICE ] || local SERVICE=$(KUBECTL__SELECT_SERVICE)
[ $SERVICE ] || ERROR 'must provide or select a service'
KUBECTL__LIST_SERVICES | grep -q "^$SERVICE$"\
|| ERROR "no service '$SERVICE' in '$CONFIG/$NAMESPACE'"
CHECK_ERRORS --no-fail --no-usage || return 1
##########################################
SERVICE_PASSWORD="$(KUBECTL__GET_SERVICE_PASSWORD)"
KUBECTL__SERVICE_PARSE
INFO "attempting to serve ${NAMESPACE}/${SERVICE_NAME}:${SERVICE_PORT}"
[ $SERVICE_PASSWORD ] && INFO "password : $SERVICE_PASSWORD"
KUBECTL port-forward service/$SERVICE_NAME $SERVICE_PORT
}
KUBECTL__SELECT_SERVICE() {
[ $NAMESPACE ] || local NAMESPACE=$(KUBECTL__GET_NAMESPACE)
[ $NAMESPACE ] || return 1
local SERVICES=$(KUBECTL__LIST_SERVICES)
local SELECTED=$({
echo "namespace service port"
echo $SERVICES \
| jq -r '.service + " " + .port' \
| sed "s/^/$NAMESPACE /" \
;
} \
| column -t \
| FZF 'select a service' --header-lines=1 \
| awk '{print $2;}' \
)
echo $SERVICES | jq -c "select (.service == \"$SELECTED\")"
}
KUBECTL__LIST_SERVICES() {
KUBECTL get service --no-headers\
| awk '{print "{\"service\":\""$1"\",\"ip\":\""$3"\",\"port\":\""$5"\"}"}' \
| jq -c 'select (.ip != "None")' \
;
}
KUBECTL__GET_SERVICE_PASSWORD() {
[ $PASSWORD_SECRET ] && [ $PASSWORD_KEY ] || return 0
KUBECTL get secret $PASSWORD_SECRET -o jsonpath="{.data.$PASSWORD_KEY}" \
| base64 --decode
}
KUBECTL__SERVICE_PARSE() {
SERVICE_NAME=$(echo $SERVICE | jq -r .service)
SERVICE_PORT=$(echo $SERVICE | jq -r .port | sed 's|/.*$||')
}

View File

@ -1,11 +1,7 @@
#!/bin/zsh
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
use redis --group kubectl
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
echo $(REDIS --get-static-definition)
}

49
plugins/kubectl/serve Executable file
View File

@ -0,0 +1,49 @@
#!/bin/zsh
use kubectl --group kubectl
#####################################################################
MAIN() {
local USAGE="
usage: [service] [...options...]
args:
service (optional) name of the service to forward locally
options:
--context override context
--namespace override namespace
--subsession REDIS subsession (default 0)
to show a required password on screen, use both:
--password-secret Secret resource
--password-key key within Secret's 'data'
-h, --help show this dialogue and exit
"
local CONTEXT NAMESPACE SERVICE
local SUBSESSION=0
while [[ $# -gt 0 ]]
do
case $1 in
--context ) CONTEXT=$2; shift 1 ;;
--namespace ) NAMESPACE=$2; shift 1 ;;
--subsession ) SUBSESSION=$2; shift 1 ;;
--password-secret ) PASSWORD_SECRET=$2; shift 1 ;;
--password-key ) PASSWORD_KEY=$2; shift 1 ;;
-h | --help ) USAGE; return 0 ;;
* )
[ $SERVICE ] && ERROR "unexpected argument '$2'"
SERVICE=$1
;;
esac
shift 1
done
CHECK_ERRORS
KUBECTL__SERVE
}

View File

@ -1,11 +1,5 @@
#!/bin/zsh
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
use kubectl --group kubectl
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
@ -17,6 +11,8 @@ MAIN() {
options:
--subsession REDIS subsession (default 0)
-h, --help show this dialogue and exit
"
local CONTEXT
local SUBSESSION=0
@ -26,6 +22,8 @@ MAIN() {
case $1 in
--subsession ) SUBSESSION=$2; shift 1 ;;
-h | --help ) USAGE; return 0 ;;
* )
[ $CONTEXT ] && ERROR "unexpected argument '$2'"
CONTEXT=$1
@ -41,6 +39,3 @@ MAIN() {
KUBECTL__SET_CONTEXT $CONTEXT
}
#####################################################################
MAIN $@

View File

@ -1,11 +1,5 @@
#!/bin/zsh
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
use kubectl --group kubectl
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
@ -17,6 +11,8 @@ MAIN() {
options:
--subsession REDIS subsession (default 0)
-h, --help show this dialogue and exit
"
local NAMESPACE
local SUBSESSION=0
@ -26,6 +22,8 @@ MAIN() {
case $1 in
--subsession ) SUBSESSION=$2; shift 1 ;;
-h | --help ) USAGE; return 0 ;;
* )
[ $NAMESPACE ] && ERROR "unexpected argument '$2'"
NAMESPACE=$1
@ -41,6 +39,3 @@ MAIN() {
KUBECTL__SET_NAMESPACE $NAMESPACE
}
#####################################################################
MAIN $@

380
run
View File

@ -1,151 +1,189 @@
#!/bin/zsh
export EXECUTION_DIR=$(pwd)
source "${0:a:h}/zsh/lib/import.driver.zsh" || exit 42
#####################################################################
__RUN() {
() {
cd "$SCWRYPTS_ROOT__scwrypts"
GIT_SCWRYPTS() { git -C "$SCWRYPTS_ROOT__scwrypts" $@; }
local ERRORS=0
local USAGE='
usage: scwrypts [OPTIONS ...] SCRIPT -- [SCRIPT OPTIONS ...]
usage: scwrypts [...options...] [...patterns...] -- [...script options...]
OPTIONS
-g, --group <group-name> only use scripts from the indicated group
-t, --type <type-name> only use scripts of the indicated type
options:
selection
-m, --name <scwrypt-name> only run the script if there is an exact match
(requires type and group)
-g, --group <group-name> only use scripts from the indicated group
-t, --type <type-name> only use scripts of the indicated type
runtime
-y, --yes auto-accept all [yn] prompts through current scwrypt
-e, --env <env-name> set environment; overwrites SCWRYPTS_ENV
-n, --no-log skip logging and run in quiet mode
-n shorthand for "--verbosity 0"
-v, --verbosity [0-4] set scwrypts log level to one of the following:
0 : only command output and critical failures; skips logfile
1 : add success / failure messages
2 : (default) include status update messages
3 : include warning messages
4 : include debug messages
--update update scwrypts library to latest version
-v, --version print out scwrypts version and exit
-l, --list print out command list and exit
alternate commands
-h, --help display this message and exit
-l, --list print out command list and exit
--list-envs print out environment list and exit
--update update scwrypts library to latest version
--version print out scwrypts version and exit
patterns:
- a list of glob patterns to loose-match a scwrypt by name
script options:
- everything after "--" is forwarded to the scwrypt you run
("-- --help" will provide more information)
'
cd "$SCWRYPTS_ROOT"
#####################################################################
### cli argument parsing and global configuration ###################
#####################################################################
local ENV_NAME="$SCWRYPTS_ENV"
local SEARCH_PATTERNS=()
local VARSPLIT SEARCH_GROUP SEARCH_TYPE SEARCH_NAME
local ERROR=0
[ ! $SCWRYPTS_LOG_LEVEL ] && {
local SCWRYPTS_LOG_LEVEL
[ $CI ] && SCWRYPTS_LOG_LEVEL=3 || SCWRYPTS_LOG_LEVEL=2
}
while [[ $# -gt 0 ]]
do
case $1 in
-t | --type )
[ ! $2 ] && ERROR "missing value for argument $1" && break
SEARCH_TYPE=$2
shift 2
;;
-g | --group )
[ ! $2 ] && ERROR "missing value for argument $1" && break
SEARCH_GROUP=$2
shift 2
;;
-m | --name )
[ ! $2 ] && ERROR "missing value for argument $1" && break
SEARCH_NAME=$2
shift 2
;;
-[a-z][a-z]* )
VARSPLIT=$(echo "$1 " | sed 's/^\(-.\)\(.*\) /\1 -\2/')
set -- $(echo " $VARSPLIT ") ${@:2}
;;
### alternate commands ###################
-h | --help )
USAGE
return 0
;;
-n | --no-log )
[ ! $SUBSCWRYPT ] && SUBSCWRYPT=0
shift 1
;;
-e | --env )
[ ! $2 ] && ERROR "missing value for argument $1" && break
[ ! $SUBSCWRYPTS ] \
&& [ $ENV_NAME ] \
&& WARNING 'overwriting session environment' \
;
ENV_NAME="$2"
STATUS "using CLI environment '$ENV_NAME'"
shift 2
;;
-l | --list )
SCWRYPTS__GET_AVAILABLE_SCWRYPTS
return 0
;;
-v | --version )
echo scwrypts $(cd "$SCWRYPTS__ROOT__scwrypts"; git describe --tags)
--list-envs )
SCWRYPTS__GET_ENV_NAMES
return 0
;;
--version )
echo scwrypts $(GIT_SCWRYPTS describe --tags)
return 0
;;
--update )
cd "$SCWRYPTS__ROOT__scwrypts"
git fetch --quiet origin main
git fetch --quiet origin main --tags
GIT_SCWRYPTS fetch --quiet origin main
GIT_SCWRYPTS fetch --quiet origin main --tags
local SYNC_STATUS=$?
git diff --exit-code origin/main -- . >&2
GIT_SCWRYPTS diff --exit-code origin/main -- . >/dev/null 2>&1
local DIFF_STATUS=$?
[[ $SYNC_STATUS -eq 0 ]] && [[ $DIFF_STATUS -eq 0 ]] && {
SUCCESS 'already up-to-date with origin/main'
} || {
git rebase --autostash origin/main \
GIT_SCWRYPTS rebase --autostash origin/main \
&& SUCCESS 'up-to-date with origin/main' \
&& GIT_SCWRYPTS log -n1 \
|| {
git rebase --abort
GIT_SCWRYPTS rebase --abort
ERROR 'unable to update scwrypts; please try manual upgrade'
REMINDER "installation in '$(pwd)'"
}
}
return 0
;;
-- )
shift 1
break # pass arguments after '--' to the scwrypt
;;
--* )
ERROR "unrecognized argument '$1'"
### scwrypts filters #####################
-m | --name )
[ $2 ] || { ERROR "missing value for argument $1"; break; }
SEARCH_NAME=$2
shift 1
;;
* )
SEARCH_PATTERNS+=($1)
-g | --group )
[ $2 ] || { ERROR "missing value for argument $1"; break; }
SEARCH_GROUP=$2
shift 1
;;
-t | --type )
[ $2 ] || { ERROR "missing value for argument $1"; break; }
SEARCH_TYPE=$2
shift 1
;;
### runtime settings #####################
-y | --yes ) export __SCWRYPTS_YES=1 ;;
-n | --no-log )
SCWRYPTS_LOG_LEVEL=0
[[ $1 =~ ^--no-log$ ]] && WARNING 'the --no-log flag is deprecated and will be removed in scwrypts v4.2'
;;
-v | --verbosity )
[[ $2 =~ ^[0-4]$ ]] || ERROR "invalid setting for verbosity '$2'"
SCWRYPTS_LOG_LEVEL=$2
shift 1
;;
-e | --env )
[ $2 ] || { ERROR "missing value for argument $1"; break; }
[ $ENV_NAME ] && DEBUG 'overwriting session environment'
ENV_NAME="$2"
STATUS "using CLI environment '$ENV_NAME'"
shift 1
;;
##########################################
-- ) shift 1; break ;; # pass arguments after '--' to the scwrypt
--* ) ERROR "unrecognized argument '$1'" ;;
* ) SEARCH_PATTERNS+=($1) ;;
esac
shift 1
done
[ $SEARCH_NAME ] && {
[ ! $SEARCH_TYPE ] && ERROR '--name requires --type argument'
[ ! $SEARCH_GROUP ] && ERROR '--name requires --group argument'
[ $SEARCH_TYPE ] || ERROR '--name requires --type argument'
[ $SEARCH_GROUP ] || ERROR '--name requires --group argument'
}
CHECK_ERRORS
##########################################
#####################################################################
### scwrypts selection / filtering ##################################
#####################################################################
local SCWRYPTS_AVAILABLE
local POTENTIAL_ERROR="no such scwrypt exists:"
SCWRYPTS_AVAILABLE=$(SCWRYPTS__GET_AVAILABLE_SCWRYPTS)
[ $SEARCH_NAME ] && {
POTENTIAL_ERROR+="\n NAME : '$SEARCH_NAME'"
POTENTIAL_ERROR+="\n TYPE : '$SEARCH_TYPE'"
POTENTIAL_ERROR+="\n GROUP : '$SEARCH_GROUP'"
SCWRYPTS_AVAILABLE=$({
##########################################
[ $SEARCH_NAME ] && SCWRYPTS_AVAILABLE=$({
echo $SCWRYPTS_AVAILABLE | head -n1
echo $SCWRYPTS_AVAILABLE | sed -e 's/\x1b\[[0-9;]*m//g' | grep "^$SEARCH_NAME *$SEARCH_TYPE *$SEARCH_GROUP\$"
})
}
[ ! $SEARCH_NAME ] && {
}) || {
[ $SEARCH_TYPE ] && {
POTENTIAL_ERROR+="\n TYPE : '$SEARCH_TYPE'"
SCWRYPTS_AVAILABLE=$(\
{
echo $SCWRYPTS_AVAILABLE | head -n1
@ -158,7 +196,6 @@ __RUN() {
}
[ $SEARCH_GROUP ] && {
POTENTIAL_ERROR+="\n GROUP : '$SEARCH_GROUP'"
SCWRYPTS_AVAILABLE=$(
{
echo $SCWRYPTS_AVAILABLE | head -n1
@ -185,39 +222,49 @@ __RUN() {
}
}
[[ $(echo $SCWRYPTS_AVAILABLE | wc -l) -lt 2 ]] && ERROR "$POTENTIAL_ERROR"
CHECK_ERRORS
[[ $(echo $SCWRYPTS_AVAILABLE | wc -l) -lt 2 ]] && {
FAIL 1 "$(echo "
no such scwrypt exists
NAME : '$SEARCH_NAME'
TYPE : '$SEARCH_TYPE'
GROUP : '$SEARCH_GROUP'
PATTERNS : '$SEARCH_PATTERNS'
" | sed "1d; \$d; /''$/d")"
}
##########################################
local NAME="$SEARCH_NAME"
local TYPE="$SEARCH_TYPE"
local GROUP="$SEARCH_GROUP"
[[ $(echo $SCWRYPTS_AVAILABLE | wc -l) -eq 2 ]] \
&& SCWRYPT_SELECTION=$(echo $SCWRYPTS_AVAILABLE | tail -n1) \
|| SCWRYPT_SELECTION=$(echo $SCWRYPTS_AVAILABLE | FZF "select a script to run" --header-lines 1)
|| SCWRYPT_SELECTION=$(echo $SCWRYPTS_AVAILABLE | FZF "select a script to run" --header-lines 1) \
;
[ $SCWRYPT_SELECTION ] || exit 2
##########################################
local NAME TYPE GROUP
SCWRYPTS__SEPARATE_SCWRYPT_SELECTION $SCWRYPT_SELECTION
export SCWRYPT_NAME=$NAME
export SCWRYPT_TYPE=$TYPE
export SCWRYPT_GROUP=$GROUP
##########################################
#####################################################################
### environment variables and configuration validation ##############
#####################################################################
local ENV_REQUIRED=true \
&& [ ! $CI ] \
&& [[ ! $SCWRYPT_NAME =~ scwrypts/logs ]] \
&& [[ ! $SCWRYPT_NAME =~ scwrypts/environment ]] \
|| ENV_REQUIRED=false
local ENV_REQUIRED=$(__CHECK_ENV_REQUIRED && echo 1 || echo 0)
local REQUIRED_ENVIRONMENT_REGEX=$(eval echo '$SCWRYPTS_REQUIRED_ENVIRONMENT_REGEX__'$SCWRYPT_GROUP)
[ $REQUIRED_ENVIRONMENT_REGEX ] && {
[[ $ENV_NAME =~ $REQUIRED_ENVIRONMENT_REGEX ]] \
|| FAIL 5 "group '$SCWRYPT_GROUP' requires current environment to match '$REQUIRED_ENVIRONMENT_REGEX' (currently $ENV_NAME)"
}
[[ $ENV_REQUIRED -eq 1 ]] && {
[[ $ENV_REQUIRED =~ true ]] && {
[ ! $ENV_NAME ] && ENV_NAME=$(SCWRYPTS__SELECT_ENV)
for GROUP in ${SCWRYPTS_GROUPS[@]}
do
local ENV_FILE=$(SCWRYPTS__GET_ENV_FILE "$ENV_NAME" "$GROUP")
@ -232,104 +279,97 @@ __RUN() {
export ENV_NAME
}
##########################################
[ $REQUIRED_ENVIRONMENT_REGEX ] && {
[[ $ENV_NAME =~ $REQUIRED_ENVIRONMENT_REGEX ]] \
|| FAIL 5 "group '$SCWRYPT_GROUP' requires current environment to match '$REQUIRED_ENVIRONMENT_REGEX' (currently $ENV_NAME)"
|| FAIL 5 "group '$SCWRYPT_GROUP' requires current environment name to match '$REQUIRED_ENVIRONMENT_REGEX' (currently $ENV_NAME)"
}
##########################################
[ ! $SUBSCWRYPT ] \
&& [[ $ENV_NAME =~ prod ]] \
&& { __VALIDATE_UPSTREAM_TIMELINE || ABORT; }
##########################################
local RUN_STRING=$(SCWRYPTS__GET_RUNSTRING $SCWRYPT_NAME $SCWRYPT_TYPE $SCWRYPT_GROUP)
[ ! $RUN_STRING ] && exit 3
##########################################
local LOGFILE=$(__GET_LOGFILE)
local HEADER=$(
[ $SUBSCWRYPT ] && return 0
echo '====================================================================='
echo "script : $SCWRYPT_GROUP $SCWRYPT_TYPE $SCWRYPT_NAME"
echo "run at : $(date)"
echo "config : $ENV_NAME"
[ ! $LOGFILE ] && echo '\033[1;33m------------------------------------------\033[0m'
)
[ ! $LOGFILE ] && {
[ $HEADER ] && echo $HEADER
[ $SUBSCWRYPT ] && {
eval "$RUN_STRING $(printf "%q " "$@")"
exit $?
} || {
eval "$RUN_STRING $(printf "%q " "$@")" </dev/tty >/dev/tty 2>&1
exit $?
}
}
{
[ $HEADER ] && echo $HEADER
echo '\033[1;33m--- BEGIN OUTPUT -------------------------\033[0m'
(eval "$RUN_STRING $(printf "%q " "$@")")
EXIT_CODE=$?
echo '\033[1;33m--- END OUTPUT ---------------------------\033[0m'
[[ $EXIT_CODE -eq 0 ]] && EXIT_COLOR='32m' || EXIT_COLOR='31m'
echo "terminated with\\033[1;$EXIT_COLOR code $EXIT_CODE\\033[0m"
} 2>&1 | tee --append "$LOGFILE"
exit $(\
sed -n 's/^terminated with.*code \([0-9]*\).*$/\1/p' $LOGFILE \
| tail -n1
)
}
#####################################################################
__CHECK_ENV_REQUIRED() {
[ $CI ] && return 1
echo $SCWRYPT_NAME | grep -q 'scwrypts/logs/' && return 1
echo $SCWRYPT_NAME | grep -q 'scwrypts/environment/' && return 1
return 0
}
__VALIDATE_UPSTREAM_TIMELINE() {
[ ! $SUBSCWRYPT ] && [[ $ENV_NAME =~ prod ]] && {
STATUS "on '$ENV_NAME'; checking diff against origin/main"
git fetch --quiet origin main
GIT_SCWRYPTS fetch --quiet origin main
local SYNC_STATUS=$?
git diff --exit-code origin/main -- . >&2
GIT_SCWRYPTS diff --exit-code origin/main -- . >&2
local DIFF_STATUS=$?
[[ $SYNC_STATUS -eq 0 ]] && [[ $DIFF_STATUS -eq 0 ]] && {
SUCCESS 'up-to-date with origin/main'
} || {
WARNING
[[ $SYNC_STATUS -ne 0 ]] && WARNING 'unable to synchronize with origin/main'
[[ $DIFF_STATUS -ne 0 ]] && WARNING 'your branch differs from origin/main (diff listed above)'
WARNING
SCWRYPTS_LOG_LEVEL=3 WARNING "you are trying to run in ${__BRIGHT_RED}production${__YELLOW} but $([[ $SYNC_STATUS -ne 0 ]] && echo 'I am unable to verify your scwrypts version')$([[ $DIFF_STATUS -ne 0 ]] && echo 'your scwrypts is out-of-date (diff listed above)')"
yN 'continue?' || return 1
yN 'continue?' || {
REMINDER "you can use 'scwrypts --update' to quickly update scwrypts to latest"
ABORT
}
}
}
__GET_LOGFILE() {
[ $SUBSCWRYPT ] \
|| [[ $SCWRYPT_NAME =~ scwrypts/logs ]] \
|| [[ $SCWRYPT_NAME =~ interactive ]] \
&& return 0
##########################################
echo "$SCWRYPTS_LOG_PATH/$(echo $GROUP/$TYPE/$NAME | sed 's/^\.\///; s/\//\%/g').log"
local RUN_STRING=$(SCWRYPTS__GET_RUNSTRING $SCWRYPT_NAME $SCWRYPT_TYPE $SCWRYPT_GROUP)
[ "$RUN_STRING" ] || return 42
#####################################################################
### logging and pretty header/footer setup ##########################
#####################################################################
local LOGFILE \
&& [[ $SCWRYPTS_LOG_LEVEL -gt 0 ]] \
&& [ ! $SUBSCWRYPT ] \
&& [[ ! $SCWRYPT_NAME =~ scwrypts/logs ]] \
&& [[ ! $SCWRYPT_NAME =~ interactive ]] \
&& LOGFILE="$SCWRYPTS_LOG_PATH/$(echo $GROUP/$TYPE/$NAME | sed 's/^\.\///; s/\//\%/g').log" \
|| LOGFILE='/dev/null' \
;
local HEADER FOOTER
[[ $SCWRYPTS_LOG_LEVEL -ge 2 ]] && {
HEADER=$(
echo "
=====================================================================
script : $SCWRYPT_GROUP $SCWRYPT_TYPE $SCWRYPT_NAME
run at : $(date)
config : $ENV_NAME
verbosity : $SCWRYPTS_LOG_LEVEL
\\033[1;33m--- SCWRYPT BEGIN ---------------------------------------------------\\033[0m
" | sed 's/^\s\+//; 1d'
)
FOOTER="\\033[1;33m--- SCWRYPT END ---------------------------------------------------\\033[0m"
}
[ $SUBSCWRYPT ] && {
HEADER="\\033[0;33m--- ($SUBSCWRYPT) BEGIN $SCWRYPT_GROUP $SCWRYPT_TYPE $SCWRYPT_NAME ---"
FOOTER="\\033[0;33m--- ($SUBSCWRYPT) END $SCWRYPT_GROUP $SCWRYPT_TYPE $SCWRYPT_NAME ---"
}
#####################################################################
__RUN $@
### run the scwrypt #################################################
#####################################################################
set -o pipefail
{
[ $HEADER ] && echo $HEADER
[[ $LOGFILE =~ ^/dev/null$ ]] && {
eval "$RUN_STRING $(printf "%q " "$@")" </dev/tty >/dev/tty 2>&1
EXIT_CODE=$?
} || {
(eval "$RUN_STRING $(printf "%q " "$@")")
EXIT_CODE=$?
}
[ $FOOTER ] && echo $FOOTER
[[ $EXIT_CODE -eq 0 ]] && EXIT_COLOR='32m' || EXIT_COLOR='31m'
[[ $SCWRYPTS_LOG_LEVEL -ge 2 ]] && [ ! $SUBSCWRYPT ] \
&& echo "terminated with\\033[1;$EXIT_COLOR code $EXIT_CODE\\033[0m"
return $EXIT_CODE
} 2>&1 | tee --append "$LOGFILE"
} $@

View File

@ -6,7 +6,7 @@ SCWRYPTS__ZSH_PLUGIN() {
local NAME
local TYPE
local GROUP
zle clear-command-line
LBUFFER= RBUFFER=
[ ! $SCWRYPT_SELECTION ] && { zle accept-line; return 0; }
SCWRYPTS__SEPARATE_SCWRYPT_SELECTION $SCWRYPT_SELECTION
@ -21,6 +21,33 @@ SCWRYPTS__ZSH_PLUGIN() {
zle -N scwrypts SCWRYPTS__ZSH_PLUGIN
bindkey $SCWRYPTS_SHORTCUT scwrypts
#####################################################################
SCWRYPTS__ZSH_BUILDER_PLUGIN() {
local SCWRYPT_SELECTION=$(SCWRYPTS__GET_AVAILABLE_SCWRYPTS | FZF 'select a script' --header-lines 1)
local NAME
local TYPE
local GROUP
LBUFFER= RBUFFER=
[ ! $SCWRYPT_SELECTION ] && { zle accept-line; return 0; }
SCWRYPTS__SEPARATE_SCWRYPT_SELECTION $SCWRYPT_SELECTION
scwrypts --name $NAME --group $GROUP --type $TYPE -- --help >&2 || {
zle accept-line
return 0
}
echo
zle reset-prompt
which scwrypts >/dev/null 2>&1\
&& LBUFFER="scwrypts" || LBUFFER="$SCWRYPTS_ROOT/scwrypts"
LBUFFER+=" --name $NAME --group $GROUP --type $TYPE -- "
}
zle -N scwrypts-builder SCWRYPTS__ZSH_BUILDER_PLUGIN
bindkey $SCWRYPTS_BUILDER_SHORTCUT scwrypts-builder
#####################################################################
SCWRYPTS__ZSH_PLUGIN_ENV() {
local RESET='reset'

View File

@ -1,10 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use cloud/aws/ecr
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
ECR_LOGIN $@
}

View File

@ -3,11 +3,9 @@ DEPENDENCIES+=(jq)
REQUIRED_ENV+=(AWS__EFS__LOCAL_MOUNT_POINT)
use cloud/aws/cli
CHECK_ENVIRONMENT
#####################################################################
EFS_CONNECT() {
MAIN() {
GETSUDO || exit 1
[ ! -d $AWS__EFS__LOCAL_MOUNT_POINT ] && {
sudo mkdir $AWS__EFS__LOCAL_MOUNT_POINT \
@ -64,6 +62,3 @@ EFS_CONNECT() {
FAIL 2 "unable to mount '$FS_ID'"
}
}
#####################################################################
EFS_CONNECT $@

View File

@ -3,11 +3,9 @@ DEPENDENCIES+=(jq)
REQUIRED_ENV+=(AWS__EFS__LOCAL_MOUNT_POINT)
use cloud/aws/cli
CHECK_ENVIRONMENT
#####################################################################
EFS_DISCONNECT() {
MAIN() {
[ ! -d "$AWS__EFS__LOCAL_MOUNT_POINT" ] && {
STATUS 'no efs currently mounted'
exit 0
@ -32,6 +30,3 @@ EFS_DISCONNECT() {
&& SUCCESS "done" \
|| FAIL 2 "failed to unmount '$EFS'"
}
#####################################################################
EFS_DISCONNECT $@

View File

@ -1,10 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use cloud/aws/eks
CHECK_ENVIRONMENT
#####################################################################
EKS_CLUSTER_LOGIN $@
MAIN() {
EKS__CLUSTER_LOGIN $@
}

View File

@ -1,19 +1,11 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use cloud/aws/rds
use db/postgres
CHECK_ENVIRONMENT
#####################################################################
CREATE_BACKUP() {
MAIN() {
local DB_HOST DB_PORT DB_NAME DB_USER DB_PASS
RDS__GET_DATABASE_CREDENTIALS $@ || return 1
PG_DUMP
}
#####################################################################
CREATE_BACKUP $@

View File

@ -1,20 +1,11 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use cloud/aws/rds
use db/postgres
CHECK_ENVIRONMENT
#####################################################################
RDS_INTERACTIVE_LOGIN() {
MAIN() {
local DB_HOST DB_PORT DB_NAME DB_USER DB_PASS
RDS__GET_DATABASE_CREDENTIALS $@ || return 1
POSTGRES__LOGIN_INTERACTIVE
}
#####################################################################
RDS_INTERACTIVE_LOGIN $@

View File

@ -1,20 +1,11 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use cloud/aws/rds
use db/postgres
CHECK_ENVIRONMENT
#####################################################################
LOAD_BACKUP() {
MAIN() {
local DB_HOST DB_PORT DB_NAME DB_USER DB_PASS
RDS__GET_DATABASE_CREDENTIALS $@ || return 1
PG_RESTORE
}
#####################################################################
LOAD_BACKUP $@

View File

@ -1,11 +1,9 @@
#!/bin/zsh
DEPENDENCIES+=(cli53)
REQUIRED_ENV+=(AWS_PROFILE)
CHECK_ENVIRONMENT
#####################################################################
ROUTE53_BACKUP() {
MAIN() {
local BACKUP_PATH="$SCWRYPTS_OUTPUT_PATH/$ENV_NAME/aws-dns-backup/$(date '+%Y-%m-%d')"
mkdir -p $BACKUP_PATH >/dev/null 2>&1
@ -25,12 +23,11 @@ ROUTE53_BACKUP() {
for P in ${JOBS[@]}; do wait $P >/dev/null 2>&1; done
}
#####################################################################
ROUTE53_GET_DOMAINS() {
cli53 list --profile $AWS_PROFILE \
| awk '{print $2;}' \
| sed '1d; s/\.$//'\
;
}
#####################################################################
ROUTE53_BACKUP

View File

@ -1,10 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use cloud/media-sync
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
MEDIA_SYNC__PULL $@
}

View File

@ -1,10 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use cloud/media-sync
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
MEDIA_SYNC__PUSH $@
}

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use db/postgres
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
POSTGRES__LOGIN_INTERACTIVE $@
}

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use db/postgres
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
PG_DUMP $@
}

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use db/postgres
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
PG_RESTORE $@
}

View File

@ -1,13 +1,9 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use db/postgres
CHECK_ENVIRONMENT
#####################################################################
RUN_SQL_POSTGRES() {
MAIN() {
WARNING " \nthis function is in a beta state\n "
local _PASS _ARGS=()
POSTGRES__SET_LOGIN_ARGS $@
@ -43,9 +39,3 @@ RUN_SQL_POSTGRES() {
&& SUCCESS "finished running '$INPUT_FILE'" \
|| FAIL 3 "something went wrong running '$INPUT_FILE' (see above)"
}
#####################################################################
WARNING
WARNING 'this function is in a beta state'
WARNING
RUN_SQL_POSTGRES $@

View File

@ -1,11 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=(docker)
REQUIRED_ENV+=()
CHECK_ENVIRONMENT
#####################################################################
DOCKER_CLEAN() {
MAIN() {
WARNING 'this will prune all docker resources from the current machine'
WARNING 'pruned resources are PERMANENTLY DELETED'
yN 'continue?' || return 1
@ -14,6 +11,3 @@ DOCKER_CLEAN() {
SUCCESS "IMAGE : $(docker image prune -f 2>/dev/null | tail -n 1)"
SUCCESS "VOLUME : $(docker volume prune -f 2>/dev/null | tail -n 1)"
}
#####################################################################
DOCKER_CLEAN $@

View File

@ -1,8 +1,6 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
CHECK_ENVIRONMENT
#####################################################################
SUCCESS 'hello world!'
MAIN() {
SUCCESS 'Hello, World!'
}

View File

@ -1,17 +1,9 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use helm
use scwrypts
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
unset USAGE
HELM__TEMPLATE__GET $@
}
#####################################################################
MAIN $@

View File

@ -1,17 +1,9 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use helm
use scwrypts
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
unset USAGE
HELM__DEPENDENCY__UPDATE $@
}
#####################################################################
MAIN $@

View File

@ -1,19 +1,44 @@
#####################################################################
DEPENDENCIES+=(
kubectl
)
REQUIRED_ENV+=(
AWS_ACCOUNT
AWS_REGION
)
DEPENDENCIES+=(kubectl yq)
REQUIRED_ENV+=()
use cloud/aws/cli
#####################################################################
EKS_CLUSTER_LOGIN() {
EKS__KUBECTL() { EKS kubectl $@; }
EKS__FLUX() { EKS flux $@; }
#####################################################################
EKS() {
local USAGE="
usage: cli [...kubectl args...]
args:
cli a kubectl-style CLI (e.g. kubectl, helm, flux, etc)
Allows access to kubernetes CLI commands by configuring environment
to point to a specific cluster.
"
REQUIRED_ENV=(AWS_REGION AWS_ACCOUNT CLUSTER_NAME) DEPENDENCIES=(kubectl $1) CHECK_ENVIRONMENT || return 1
local CONTEXT="arn:aws:eks:${AWS_REGION}:${AWS_ACCOUNT}:cluster/${CLUSTER_NAME}"
local CONTEXT_ARGS=()
case $1 in
helm ) CONTEXT_ARGS+=(--kube-context $CONTEXT) ;;
* ) CONTEXT_ARGS+=(--context $CONTEXT) ;;
esac
$1 ${CONTEXT_ARGS[@]} ${@:2}
}
#####################################################################
EKS__CLUSTER_LOGIN() {
local USAGE="
usage: [...options...]
@ -25,6 +50,7 @@ EKS_CLUSTER_LOGIN() {
cluster in EKS. Also creates the kubeconfig entry if it does not
already exist.
"
REQUIRED_ENV=(AWS_ACCOUNT AWS_REGION) CHECK_ENVIRONMENT || return 1
local CLUSTER_NAME

View File

@ -0,0 +1,116 @@
#####################################################################
DEPENDENCIES+=(eksctl)
REQUIRED_ENV+=()
use cloud/aws/eks
#####################################################################
EKSCTL() {
REQUIRED_ENV=(AWS_PROFILE AWS_REGION) CHECK_ENVIRONMENT || return 1
AWS_PROFILE=$AWS_PROFILE AWS_REGION=$AWS_REGION \
eksctl $@
}
EKSCTL__CREATE_IAMSERVICEACCOUNT() {
local USAGE="
usage: serviceaccount-name namespace [...options...] -- [...'eksctl create iamserviceaccount' args...]
options:
--serviceaccount (required) target k8s:ServiceAccount
--namespace (required) target k8s:Namespace
--role-name (required) name of the IAM role to assign
--force don't check for existing serviceaccount and override any existing configuration
eksctl create iamserviceaccount args:
$(eksctl create iamserviceaccount --help 2>&1 | grep -v -- '--name' | grep -v -- '--namespace' | grep -v -- '--role-name' | sed 's/^/ /')
"
REQUIRED_ENV=(AWS_REGION AWS_ACCOUNT CLUSTER_NAME) CHECK_ENVIRONMENT || return 1
local SERVICEACCOUNT NAMESPACE ROLE_NAME
local FORCE=0
local EKSCTL_ARGS=()
while [[ $# -gt 0 ]]
do
case $1 in
--serviceaccount ) SERVICEACCOUNT=$2; shift 1 ;;
--namespace ) NAMESPACE=$2; shift 1 ;;
--role-name ) ROLE_NAME=$2; shift 1 ;;
--force ) FORCE=1 ;;
-- ) shift 1; break ;;
* ) ERROR "unknown argument '$1'" ;;
esac
shift 1
done
while [[ $# -gt 0 ]]; do EKSCTL_ARGS+=($1); shift 1; done
[ $SERVICEACCOUNT ] || ERROR "--serviceaccount is required"
[ $NAMESPACE ] || ERROR "--namespace is required"
[ $ROLE_NAME ] || ERROR "--role-name is required"
CHECK_ERRORS --no-fail || return 1
##########################################
[[ $FORCE -eq 0 ]] && {
_EKS__CHECK_IAMSERVICEACCOUNT_EXISTS
local EXISTS_STATUS=$?
case $EXISTS_STATUS in
0 )
SUCCESS "'$NAMESPACE/$SERVICEACCOUNT' already configured with '$ROLE_NAME'"
return 0
;;
1 ) ;; # role does not exist yet; continue with rollout
2 )
ERROR "'$NAMESPACE/$SERVICEACCOUNT' has been configured with a different role than '$ROLE_NAME'"
REMINDER "must use --force flag to overwrite"
return 2
;;
esac
}
STATUS "creating iamserviceaccount" \
&& EKSCTL create iamserviceaccount \
--cluster $CLUSTER_NAME \
--namespace $NAMESPACE \
--name $SERVICEACCOUNT \
--role-name $ROLE_NAME \
--override-existing-serviceaccounts \
--approve \
${EKSCTL_ARGS[@]} \
&& SUCCESS "successfully configured '$NAMESPACE/$SERVICEACCOUNT' with IAM role '$ROLE_NAME'" \
|| { ERROR "unable to configure '$NAMESPACE/$SERVICEACCOUNT' with IAM role '$ROLE_NAME' (check cloudformation dashboard for details)"; return 3; }
}
_EKS__CHECK_IAMSERVICEACCOUNT_EXISTS() {
STATUS "checking for existing role-arn"
local CURRENT_ROLE_ARN=$(
EKS__KUBECTL --namespace $NAMESPACE get serviceaccount $SERVICEACCOUNT -o yaml \
| YQ -r '.metadata.annotations["eks.amazonaws.com/role-arn"]' \
| grep -v '^null$' \
)
[ $CURRENT_ROLE_ARN ] || {
STATUS "serviceaccount does not exist or has no configured role"
return 1
}
[[ $CURRENT_ROLE_ARN =~ "$ROLE_NAME$" ]] || {
STATUS "serviceaccount current role does not match desired role:
CURRENT : $CURRENT_ROLE_ARN
DESIRED : arn:aws:iam::${AWS_ACCOUNT}:role/$ROLE_NAME
"
return 2
}
STATUS "serviceaccount current role matches desired role"
return 0
}

View File

@ -86,7 +86,7 @@ RDS__GET_DATABASE_CREDENTIALS() {
user-input ) _RDS_AUTH__userinput ;;
esac
[[ $PRINT_PASSWORD -eq 1 ]] && INFO "password : $DB_PASS"
[[ $PRINT_PASSWORD -eq 1 ]] && DEBUG "password : $DB_PASS"
return 0
}

Binary file not shown.

View File

@ -47,6 +47,26 @@ do
}
done
#####################################################################
for GROUP_LOADER in $(env | sed -n 's/^SCWRYPTS_GROUP_LOADER__[a-z_]\+=//p')
do
[ -f "$GROUP_LOADER" ] && source "$GROUP_LOADER"
done
: \
&& [ ! "$SCWRYPTS_AUTODETECT_GROUP_BASEDIR" ] \
&& [ $GITHUB_WORKSPACE ] \
&& [ ! $SCWRYPTS_GITHUB_NO_AUTOLOAD ] \
&& SCWRYPTS_AUTODETECT_GROUP_BASEDIR="$GITHUB_WORKSPACE"
[ "$SCWRYPTS_AUTODETECT_GROUP_BASEDIR" ] && [ -d "$SCWRYPTS_AUTODETECT_GROUP_BASEDIR" ] && {
for GROUP_LOADER in $(find "$SCWRYPTS_AUTODETECT_GROUP_BASEDIR" -type f -name \*scwrypts.zsh)
do
[ -f "$GROUP_LOADER" ] && source "$GROUP_LOADER"
done
}
#####################################################################
[ $NO_EXPORT_CONFIG ] || __SCWRYPT=1 # arbitrary; indicates currently inside a scwrypt
true

View File

@ -4,6 +4,7 @@ DEPENDENCIES+=(helm kubeval)
REQUIRED_ENV+=()
use helm/validate
use scwrypts
#####################################################################
@ -17,6 +18,8 @@ HELM__TEMPLATE__GET() {
--colorize use 'bat' to colorize output
--raw remove scwrypts-added fluff and only output helm template details
-h, --help show this help dialogue
Smart helm-template generator which auto-detects the chart
and sample values for testing and developing helm charts.
"
@ -35,8 +38,11 @@ HELM__TEMPLATE__GET() {
;;
--raw ) RAW=1 ;;
-h | --help ) USAGE; return 0 ;;
-- ) shift 1; break ;;
* ) HELM_ARGS+=($1) ;;
esac
shift 1
@ -60,6 +66,7 @@ HELM__TEMPLATE__GET() {
[ ! $TEMPLATE_OUTPUT ] && EXIT_CODE=1
[[ $RAW -eq 1 ]] && {
[ $USE_CHART_ROOT ] && [[ $USE_CHART_ROOT -eq 1 ]] || HELM_ARGS+=(--show-only $(echo $TEMPLATE_FILENAME | sed "s|^$CHART_ROOT/||"))
[[ $COLORIZE -eq 1 ]] \

View File

@ -27,13 +27,20 @@ HELM__VALIDATE() {
return 1
}
CHART_NAME=$(yq -r .name "$CHART_ROOT/Chart.yaml")
CHART_NAME=$(YQ -r .name "$CHART_ROOT/Chart.yaml")
[[ $TEMPLATE_FILENAME =~ values*.yaml$ ]] && {
[[ $TEMPLATE_FILENAME =~ values.*.yaml$ ]] && {
HELM_ARGS+=(--values $TEMPLATE_FILENAME)
USE_CHART_ROOT=1
}
[[ $TEMPLATE_FILENAME =~ tests/.*.yaml$ ]] && {
HELM_ARGS+=(--values $TEMPLATE_FILENAME)
USE_CHART_ROOT=1
}
[[ $TEMPLATE_FILENAME =~ .tpl$ ]] \
&& USE_CHART_ROOT=1
[[ $(dirname $TEMPLATE_FILENAME) =~ ^$CHART_ROOT$ ]] \
&& USE_CHART_ROOT=1
@ -55,11 +62,34 @@ _HELM__GET_CHART_ROOT() {
_HELM__GET_DEFAULT_VALUES_ARGS() {
for F in \
"$CHART_ROOT/values.yaml" \
"$CHART_ROOT/values.test.yaml" \
"$CHART_ROOT/tests/default.yaml" \
"$CHART_ROOT/values.test.yaml" \
"$CHART_ROOT/values.yaml" \
;
do
[ -f "$F" ] && HELM_ARGS+=(--values "$F")
[ -f "$F" ] && HELM_ARGS=(--values "$F" $HELM_ARGS)
done
for LOCAL_REPOSITORY in $(\
cat "$CHART_ROOT/Chart.yaml" \
| YQ -r '.dependencies[] | .repository' \
| grep '^file://' \
| sed 's|file://||' \
)
do
[[ $LOCAL_REPOSITORY =~ ^[/~] ]] \
&& LOCAL_REPOSITORY_ROOT="$LOCAL_REPOSITORY" \
|| LOCAL_REPOSITORY_ROOT="$CHART_ROOT/$LOCAL_REPOSITORY" \
;
for F in \
"$LOCAL_REPOSITORY_ROOT/tests/default.yaml" \
"$LOCAL_REPOSITORY_ROOT/values.test.yaml" \
"$LOCAL_REPOSITORY_ROOT/values.yaml" \
;
do
[ -f "$F" ] && HELM_ARGS=(--values "$F" $HELM_ARGS)
done
done
}

View File

@ -0,0 +1,106 @@
#####################################################################
DEPENDENCIES+=()
REQUIRED_ENV+=()
#####################################################################
TALLY_USE_REDIS=false # maybe someday
TALLY_PATH="$SCWRYPTS_DATA_PATH/tally"
#####################################################################
TALLY() {
local USAGE="
usage: [...options...]
options:
-c, --increment-count increment the tally by this much (default 1)
-n, --tally-name name of tally system (default 'default')
-g, --get only output the current value
-s, --set set the tally to a specific value
-r, --reset set the tally back to zero
--raw only output the tally value
-h, --help print this dialogue and exit
Simple tally mark system; keep track of a count.
"
local INCREMENT_COUNT=1
local TALLY_NAME=default
local RAW=false
local SET_VALUE=
while [[ $# -gt 0 ]]
do
case $1 in
-c | --increment-count ) INCREMENT_COUNT=$2; shift 1 ;;
-n | --tally-name ) TALLY_NAME=$2; shift 1 ;;
-g | --get ) INCREMENT_COUNT=0 ;;
-s | --set ) SET_VALUE=$2; shift 1 ;;
-r | --reset ) SET_VALUE=0 ;;
--raw ) RAW=true ;;
-h | --help ) USAGE; return 0 ;;
* ) ERROR "unknown argument '$1'" ;;
esac
shift 1
done
[ $TALLY_NAME ] && echo "$TALLY_NAME" | grep -qv '/' \
|| ERROR "invalid tally name '$TALLY_NAME'"
local TALLY_FILENAME="$TALLY_PATH/$TALLY_NAME.txt"
CHECK_ERRORS --no-fail || return 1
##########################################
local NEW_VALUE CURRENT_VALUE=0
[ $SET_VALUE ] && NEW_VALUE=$SET_VALUE || {
[ -f "$TALLY_FILENAME" ] && {
CURRENT_VALUE=$(cat "$TALLY_FILENAME" | tail -n1 | grep '^[0-9]\+')
}
[ $CURRENT_VALUE ] || {
ERROR "malformed tally file '$TALLY_FILENAME'; aborting"
return 1
}
NEW_VALUE=$(($CURRENT_VALUE + $INCREMENT_COUNT))
}
##########################################
local TALLY_DIR="$(dirname "$TALLY_FILENAME")"
[ -d "$TALLY_DIR" ] || mkdir -p "$TALLY_DIR"
[ -d "$TALLY_DIR" ] || {
ERROR "unable to write to '$TALLY_DIR'; aborting"
return 1
}
echo "# autogenerated tally file; avoid direct modification\n$NEW_VALUE" > "$TALLY_FILENAME" || {
ERROR "failed to write to '$TALLY_FILENAME': aborting"
return 1
}
##########################################
case $RAW in
true ) printf "$NEW_VALUE" ;;
false )
case $TALLY_NAME in
default ) SUCCESS "current tally : $NEW_VALUE" ;;
* ) SUCCESS "$TALLY_NAME : $NEW_VALUE" ;;
esac
esac
}

View File

@ -12,7 +12,7 @@ SCWRYPTS__SELECT_ENV() {
}
SCWRYPTS__SELECT_OR_CREATE_ENV() {
SCWRYPTS__GET_ENV_NAMES | FZF_TAIL 'select / create an environment'
SCWRYPTS__GET_ENV_NAMES | FZF_USER_INPUT 'select / create an environment'
}
SCWRYPTS__GET_ENV_FILES() {

View File

@ -8,15 +8,13 @@ use scwrypts/run
#####################################################################
SCWRYPTS__RUN() {
SCWRYPTS__RUN() { # context wrapper to run scwrypts within scwrypts
local EXIT_CODE=0
((SUBSCWRYPT+=1))
echo "--- START SUBSCWRYPT=$SUBSCWRYPT $@"
SUBSCWRYPT=$SUBSCWRYPT $SCWRYPTS_ROOT/run $@
EXIT_CODE=$?
((SUBSCWRYPT-=1))
return $EXIT_CODE
echo "--- END SUBSCWRYPT=$SUBSCWRYPT $@"
}

View File

@ -112,6 +112,58 @@ SCWRYPTS__GET_RUNSTRING() {
SCWRYPTS__GET_RUNSTRING__zsh() {
__CHECK_DEPENDENCY zsh || return 1
local SCWRYPT_FILENAME
[ $(eval echo '$SCWRYPTS_TYPE__'$SCWRYPT_GROUP) ] \
&& SCWRYPT_FILENAME="$GROUP_PATH/$SCWRYPT_NAME" \
|| SCWRYPT_FILENAME="$GROUP_PATH/$SCWRYPT_TYPE/$SCWRYPT_NAME" \
;
printf "
source '$SCWRYPT_FILENAME'
CHECK_ENVIRONMENT
ERRORS=0
export USAGE=\"
usage: -
args: -
options: -
-h, --help display this message and exit
description: -
\"
[ ! \$USAGE__usage ] && export USAGE__usage='[...options...]'
() {
local MAIN_ARGS=()
local VARSPLIT
while [[ \$# -gt 0 ]]
do
case \$1 in
-[a-z][a-z]* )
VARSPLIT=\$(echo \"\$1 \" | sed 's/^\\\\(-.\\\\)\\\\(.*\\\\) /\\\\1 -\\\\2/')
set -- throw-away \$(echo \" \$VARSPLIT \") \${@:2}
;;
-h | --help ) USAGE; exit 0 ;;
* ) MAIN_ARGS+=(\$1) ;;
esac
shift 1
done
MAIN \${MAIN_ARGS[@]}
} "
return 0
}
SCWRYPTS__GET_RUNSTRING__zsh_v3() {
WARNING "scwrypts zsh/v3 runstrings are now deprecated; please update to scwrypts v4 format"
__CHECK_DEPENDENCY zsh || return 1
[ $(eval echo '$SCWRYPTS_TYPE__'$SCWRYPT_GROUP) ] \
&& echo "source $GROUP_PATH/$SCWRYPT_NAME" \
|| echo "source $GROUP_PATH/$SCWRYPT_TYPE/$SCWRYPT_NAME" \

View File

@ -20,7 +20,7 @@ VUNDLE__PLUGIN_LIST=$(ls $VUNDLE__PLUGIN_DIR | grep -v 'Vundle.vim' | grep -v 'b
source $VUNDLE__BUILD_DEFINITIONS
for PLUGIN in $(echo $VUNDLE__PLUGIN_LIST)
do
typeset -f VUNDLE__BUILD__$PLUGIN >/dev/null 2>/dev/null || {
which VUNDLE__BUILD__$PLUGIN >/dev/null 2>/dev/null || {
echo -e "\nVUNDLE__BUILD__$PLUGIN() {\n # ... build steps from $HOME/.vim/$PLUGIN \n}" \
>> $VUNDLE__BUILD_DEFINITIONS
VUNDLE__BUILD__$PLUGIN() {}

View File

@ -2,22 +2,22 @@ __BLACK='\033[0;30m'
__DARK_GRAY='\033[1;30m'
__RED='\033[0;31m'
__LIGHT_RED='\033[1;31m'
__BRIGHT_RED='\033[1;31m'
__GREEN='\033[0;32m'
__LIGHT_GREEN='\033[1;32m'
__BRIGHT_GREEN='\033[1;32m'
__ORANGE='\033[0;33m'
__YELLOW='\033[1;33m'
__YELLOW='\033[0;33m'
__BRIGHT_YELLOW='\033[1;33m'
__BLUE='\033[1;34m'
__DARK_BLUE='\033[0;34m'
__BLUE='\033[0;34m'
__BRIGHT_BLUE='\033[1;34m'
__PURPLE='\033[1;35m'
__DARK_PURPLE='\033[0;35m'
__MAGENTA='\033[0;35m'
__BRIGHT_MAGENTA='\033[1;35m'
__CYAN='\033[1;36m'
__DARK_CYAN='\033[0;36m'
__CYAN='\033[0;36m'
__BRIGHT_CYAN='\033[1;36m'
__WHITE='\033[1;37m'
__LIGHT_GRAY='\033[0;37m'
@ -27,17 +27,17 @@ __COLOR_RESET='\033[0m'
__GET_RANDOM_COLOR() {
local COLORS=(
$__RED
$__LIGHT_RED
$__BRIGHT_RED
$__GREEN
$__LIGHT_GREEN
$__ORANGE
$__BRIGHT_GREEN
$__YELLOW
$__BRIGHT_YELLOW
$__BLUE
$__DARK_BLUE
$__PURPLE
$__DARK_PURPLE
$__BRIGHT_BLUE
$__MAGENTA
$__BRIGHT_MAGENTA
$__CYAN
$__DARK_CYAN
$__BRIGHT_CYAN
$__WHITE
)
print "$__COLOR_RESET${COLORS[$(shuf -i 1-${#COLORS[@]} -n 1)]}"

View File

@ -1,13 +1,14 @@
__CHECK_DEPENDENCIES() {
local DEP ERROR=0
local DEP ERRORS=0
local SCWRYPTS_LOG_LEVEL=1
[ ! $E ] && E=ERROR
DEPENDENCIES=($(echo $DEPENDENCIES | sed 's/ \+/\n/g' | sort -u))
for DEP in ${DEPENDENCIES[@]}; do __CHECK_DEPENDENCY $DEP || ((ERROR+=1)); done
__CHECK_COREUTILS || ((ERROR+=$?))
for DEP in ${DEPENDENCIES[@]}; do __CHECK_DEPENDENCY $DEP || ((ERRORS+=1)); done
__CHECK_COREUTILS || ((ERRORS+=$?))
return $ERROR
return $ERRORS
}
__CHECK_DEPENDENCY() {
@ -17,6 +18,13 @@ __CHECK_DEPENDENCY() {
$E "application '$1' "$([[ $OPTIONAL -eq 1 ]] && echo preferred || echo required)" but not available on PATH $(__CREDITS $1)"
return 1
}
[[ $DEPENDENCY =~ ^yq$ ]] && {
yq --version | grep -q mikefarah \
|| WARNING 'detected kislyuk/yq but mikefarah/yq is preferred (compatibility may vary)'
}
return 0
}
__CHECK_COREUTILS() {
@ -36,7 +44,7 @@ __CHECK_COREUTILS() {
done
[[ $NON_GNU_DEPENDENCY_COUNT -gt 0 ]] && {
WARNING 'scripts rely on GNU coreutils; functionality may be limited'
WARNING 'scripts rely on GNU coreutils; compatibility may vary'
IS_MACOS && REMINDER 'GNU coreutils can be installed and linked through Homebrew'
}

View File

@ -1,4 +1,5 @@
__CHECK_REQUIRED_ENV() {
local SCWRYPTS_LOG_LEVEL=1
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

57
zsh/lib/utils/io.fzf.zsh Normal file
View File

@ -0,0 +1,57 @@
FZF() {
[ $CI ] && FAIL 1 'currently in CI, but FZF requires user input'
local FZF_ARGS=()
FZF_ARGS+=(-i)
FZF_ARGS+=(--ansi)
FZF_ARGS+=(--bind=ctrl-c:cancel)
FZF_ARGS+=(--height=50%)
FZF_ARGS+=(--layout=reverse)
local SELECTION=$(fzf ${FZF_ARGS[@]} --prompt "$1 : " ${@:2})
PROMPT "$1"
[ $BE_QUIET ] || {
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] && echo $SELECTION >&2
}
echo $SELECTION
[ $SELECTION ]
}
FZF_USER_INPUT() { # allow user to type custom answers; reconfirm if ambiguous with select
local FZF_OUTPUT=$(BE_QUIET=1 FZF $@ --print-query | sed '/^$/d' | sort -u)
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] && echo $FZF_OUTPUT | head -n1 >&2
[ ! $FZF_OUTPUT ] && return 1
[[ $(echo "$FZF_OUTPUT" | wc -l) -eq 1 ]] \
&& { echo "$FZF_OUTPUT"; return 0; }
local FZF_OUTPUT=$(
echo "$FZF_OUTPUT" \
| sed "1s/\$/^$(printf "$__LIGHT_GRAY\\033[3m")<- what you typed$(printf $__COLOR_RESET)/" \
| sed "2s/\$/^$(printf "$__LIGHT_GRAY\\033[3m")<- what you selected$(printf $__COLOR_RESET)/" \
| column -ts '^' \
| BE_QUIET=1 FZF "$@ (clarify)" \
)
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] && echo $FZF_OUTPUT >&2
FZF_OUTPUT=$(echo $FZF_OUTPUT | sed 's/\s\+<- what you .*$//')
echo $FZF_OUTPUT
[ $FZF_OUTPUT ]
}
#####################################################################
### vvv DEPRECATED vvv ##############################################
#####################################################################
FZF_HEAD() { # prefer user input over selected
WARNING 'FZF_HEAD is deprecated and will be unavailable in v4.2; please switch to FZF_USER_INPUT (drop-in fix!)'
FZF $@ --print-query | sed '/^$/d' | head -n1;
}
FZF_TAIL() { # prefer selected over user input
WARNING 'FZF_TAIL is deprecated and will be unavailable in v4.2; please switch to FZF_USER_INPUT (drop-in fix!)'
FZF $@ --print-query | sed '/^$/d' | tail -n1;
}
#####################################################################

View File

@ -0,0 +1,38 @@
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
MESSAGE="$(echo "$MESSAGE" | sed 's/%/%%/g')"
local STYLED_MESSAGE="$({
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
})"
STYLED_MESSAGE="${COLOR}$(echo "$STYLED_MESSAGE" | sed 's/%/%%/g')${__COLOR_RESET}${LAST_LINE_END}"
[[ $STDERR -eq 1 ]] && printf $STYLED_MESSAGE >&2
[[ $STDOUT -eq 1 ]] && printf $STYLED_MESSAGE
return 0
}

View File

@ -0,0 +1,65 @@
USAGE() { # formatter for USAGE variable
[ ! $USAGE ] && return 0
local USAGE_LINE=$(echo $USAGE | grep -i '^[ ]*usage *:' | sed 's/^[ ]*//')
[ $USAGE__usage ] && echo $USAGE_LINE | grep -q 'usage: -' \
&& USAGE_LINE=$(echo $USAGE_LINE | sed "s/usage: -/usage: $USAGE__usage/")
[ $__SCWRYPT ] \
&& USAGE_LINE=$(
echo $USAGE_LINE \
| sed "s;^[^:]*:;& scwrypts $SCWRYPT_NAME --;" \
| sed 's/ \{2,\}/ /g; s/scwrypts -- scwrypts/scwrypts/' \
)
local THE_REST=$(echo $USAGE | grep -vi '^[ ]*usage *:' )
local DYNAMIC_USAGE_ELEMENT
#
# create dynamic usage elements (like 'args') by defining USAGE__<element>
# then using the syntax "<element>: -" in your USAGE variable
#
# e.g.
#
# USAGE__args="
# subcommand arg 1 arg 1 description
# subcommand arg 2 some other description
# "
#
# USAGE="
# usage: some-command [...args...]
#
# args: -
# -h, --help some arguments are applicable everywhere
# "
#
for DYNAMIC_USAGE_ELEMENT in $(echo $THE_REST | sed -n 's/^\([^:]*\): -$/\1/p')
do
DYNAMIC_USAGE_ELEMENT_TEXT=$(eval echo '$USAGE__'$DYNAMIC_USAGE_ELEMENT)
#[ $DYNAMIC_USAGE_ELEMENT_TEXT ] || continue
case $DYNAMIC_USAGE_ELEMENT in
description )
DYNAMIC_USAGE_ELEMENT_TEXT=$(echo "$DYNAMIC_USAGE_ELEMENT_TEXT" | perl -p0e 's/^[\n\s]+//')
DYNAMIC_USAGE_ELEMENT_TEXT="$__YELLOW\\033[03m$DYNAMIC_USAGE_ELEMENT_TEXT\\033[0m"
;;
* )
DYNAMIC_USAGE_ELEMENT_TEXT=$(echo $DYNAMIC_USAGE_ELEMENT_TEXT | sed 's/[^ ]/ &/')
;;
esac
THE_REST=$(echo $THE_REST | perl -pe "s$DYNAMIC_USAGE_ELEMENT: -$DYNAMIC_USAGE_ELEMENT:\n$DYNAMIC_USAGE_ELEMENT_TEXT\n\n")
done
# allow for dynamic 'description: -' but delete the 'description:' header line
THE_REST=$(echo $THE_REST | sed '/^[ ]*description:$/d')
echo "$__BLUE$USAGE_LINE$__COLOR_RESET\n\n$THE_REST" \
| sed "s/^\t\+//; s/\s\+$//; s/^\\s*$//;" \
| sed '/./,$!d; :a; /^\n*$/{$d;N;ba;};' \
| perl -p0e 's/\n{2,}/\n\n/g' \
| perl -p0e 's/:\n{2,}/:\n/g' \
| perl -p0e 's/([a-z]+:)\n([a-z]+:)/\2/g' \
>&2
}

View File

@ -1,160 +1,131 @@
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
MESSAGE="$(echo "$MESSAGE" | sed 's/%/%%/g')"
local STYLED_MESSAGE="$({
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
})"
STYLED_MESSAGE="${COLOR}$(echo "$STYLED_MESSAGE" | sed 's/%/%%/g')${__COLOR_RESET}${LAST_LINE_END}"
[[ $STDERR -eq 1 ]] && printf $STYLED_MESSAGE >&2
[[ $STDOUT -eq 1 ]] && printf $STYLED_MESSAGE
return 0
}
#####################################################################
### basic colorized print messages ##################################
#####################################################################
source "${0:a:h}/io.print.zsh"
[ ! $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
local DISPLAY_USAGE=1
while [[ $# -gt 0 ]]
do
case $1 in
--no-fail ) FAIL_OUT=0 ;;
--no-usage ) DISPLAY_USAGE=0 ;;
esac
shift 1
done
[ ! $ERRORS ] && ERRORS=0
[[ $ERRORS -eq 0 ]] && return 0
[[ $DISPLAY_USAGE -eq 1 ]] && USAGE
[[ $FAIL_OUT -eq 1 ]] && exit $ERRORS
ERROR() { # command encountered an error
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="ERROR ✖" COLOR=$__RED PRINT "$@"
((ERRORS+=1))
return $ERRORS
}
USAGE() { # formatter for USAGE variable
[ ! $USAGE ] && return 0
local USAGE_LINE=$(echo $USAGE | grep -i '^[ ]*usage *:' | sed 's/^[ ]*//')
[ $USAGE__usage ] && echo $USAGE_LINE | grep -q 'usage: -' \
&& USAGE_LINE=$(echo $USAGE_LINE | sed "s/usage: -/usage: $USAGE__usage/")
[ $__SCWRYPT ] \
&& USAGE_LINE=$(
echo $USAGE_LINE \
| sed "s;^[^:]*:;& scwrypts $SCWRYPT_NAME --;" \
| sed 's/ \{2,\}/ /g; s/scwrypts -- scwrypts/scwrypts/' \
)
local THE_REST=$(echo $USAGE | grep -vi '^[ ]*usage *:' )
local DYNAMIC_USAGE_ELEMENT
#
# create dynamic usage elements (like 'args') by defining USAGE__<element>
# then using the syntax "<element>: -" in your USAGE variable
#
# e.g.
#
# USAGE__args="
# subcommand arg 1 arg 1 description
# subcommand arg 2 some other description
# "
#
# USAGE="
# usage: some-command [...args...]
#
# args: -
# -h, --help some arguments are applicable everywhere
# "
#
for DYNAMIC_USAGE_ELEMENT in $(echo $THE_REST | sed -n 's/^\([^:]*\): -$/\1/p')
do
DYNAMIC_USAGE_ELEMENT_TEXT=$(eval echo '$USAGE__'$DYNAMIC_USAGE_ELEMENT)
[[ ! $DYNAMIC_USAGE_ELEMENT =~ ^description$ ]] \
&& DYNAMIC_USAGE_ELEMENT_TEXT=$(echo $DYNAMIC_USAGE_ELEMENT_TEXT | sed 's/[^ ]/ &/')
THE_REST=$(echo $THE_REST | perl -pe "s/$DYNAMIC_USAGE_ELEMENT: -/$DYNAMIC_USAGE_ELEMENT:\n$DYNAMIC_USAGE_ELEMENT_TEXT\n\n/")
done
# allow for dynamic 'description: -' but delete the 'description:' header line
THE_REST=$(echo $THE_REST | sed '/^[ ]*description:$/d')
echo "$__DARK_BLUE$USAGE_LINE$__COLOR_RESET\n\n$THE_REST" \
| sed "s/^\t\+//; s/\s\+$//; s/^\\s*$//;" \
| sed '/./,$!d; :a; /^\n*$/{$d;N;ba;};' \
| perl -p0e 's/\n{2,}/\n\n/g' \
| perl -p0e 's/:\n{2,}/:\n/g' \
>&2
SUCCESS() { # command completed successfully
[[ $SCWRYPTS_LOG_LEVEL -ge 1 ]] \
&& PREFIX="SUCCESS ✔" COLOR=$__GREEN PRINT "$@"
}
INPUT() {
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 ]
}
Yn() {
PROMPT "$@ [Yn]"
[ $CI ] && { echo y; return 0; }
source "${0:a:h}/io.fzf.zsh" # allow user to select from a list of inputs
local Yn; READ -k Yn; echo >&2
[[ $Yn =~ [nN] ]] && return 1 || return 0
EDIT() { # edit a file in user's preferred editor
[ $CI ] && {
WARNING 'currently in CI, skipping EDIT'
return 0
}
yN() {
PROMPT "$@ [yN]"
[ $CI ] && { echo y; return 0; }
local yN; READ -k yN; echo >&2
[[ $yN =~ [yY] ]] && return 0 || return 1
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...]
@ -167,7 +138,6 @@ CAPTURE() {
} < <((printf '\0%s\0' "$(${@:3})" 1>&2) 2>&1)
}
#####################################################################
GETSUDO() {
echo "\\033[1;36mPROMPT  : checking sudo password...\\033[0m" >&2
@ -176,45 +146,85 @@ GETSUDO() {
|| { 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 FZF_ARGS=()
FZF_ARGS+=(-i)
FZF_ARGS+=(--ansi)
FZF_ARGS+=(--bind=ctrl-c:cancel)
FZF_ARGS+=(--height=50%)
FZF_ARGS+=(--layout=reverse)
local SELECTION=$(fzf ${FZF_ARGS[@]} --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
[ $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
}
EDIT() {
[ $CI ] && {
INFO 'currently in CI, skipping EDIT'
return 0
}
READ_YN() { # yes/no read is suprisingly tricky
local FORCE_USER_INPUT=false
local USERPROMPT=()
local READ_ARGS=()
STATUS "opening '$1' for editing"
$EDITOR $@ </dev/tty >/dev/tty
SUCCESS "finished editing '$1'!"
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
}

View File

@ -1,16 +1,11 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use media/youtube
CHECK_ENVIRONMENT
#####################################################################
DOWNLOAD_VIDEO() {
MAIN() {
local URLS=($@)
[[ ${#URLS[@]} -eq 0 ]] && URLS=($(echo '' | FZF_HEAD 'enter URL'))
[[ ${#URLS[@]} -eq 0 ]] && URLS=($(echo '' | FZF_USER_INPUT 'enter URL'))
[[ ${#URLS[@]} -eq 0 ]] && ABORT
local FILENAME=$(YT__GET_FILENAME $URLS)
@ -23,6 +18,3 @@ DOWNLOAD_VIDEO() {
&& SUCCESS "downloaded to '$YT__OUTPUT_DIR/$FILENAME'" \
|| { ERROR "failed to download '$FILENAME'"; return 2; }
}
#####################################################################
DOWNLOAD_VIDEO $@

View File

@ -1,16 +1,11 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use media/youtube
CHECK_ENVIRONMENT
#####################################################################
GET_AUDIO_CLIP() {
MAIN() {
local URLS=($@)
[[ ${#URLS[@]} -eq 0 ]] && URLS=($(echo '' | FZF_HEAD 'enter URL'))
[[ ${#URLS[@]} -eq 0 ]] && URLS=($(echo '' | FZF_USER_INPUT 'enter URL'))
[[ ${#URLS[@]} -eq 0 ]] && ABORT
local FILENAME=$(YT__GET_FILENAME $URLS)
@ -26,9 +21,9 @@ GET_AUDIO_CLIP() {
LENGTH=$(GET_VIDEO_LENGTH "$INPUT_FILE")
[ ! $LENGTH ] && { ERROR "unable to determine video length for '$INPUT_FILE'"; return 2; }
START_TIME=$(echo 0 | FZF_HEAD "enter start time (0 ≤ t < $LENGTH)")
START_TIME=$(echo 0 | FZF_USER_INPUT "enter start time (0 ≤ t < $LENGTH)")
[ ! $START_TIME ] && ABORT
END_TIME=$(echo $LENGTH | FZF_HEAD "enter end time ($START_TIME > t ≥ $LENGTH)")
END_TIME=$(echo $LENGTH | FZF_USER_INPUT "enter end time ($START_TIME > t ≥ $LENGTH)")
[ ! $END_TIME ] && ABORT
STATUS
@ -37,7 +32,7 @@ GET_AUDIO_CLIP() {
STATUS "end time : $END_TIME"
STATUS
OUTPUT_FILE=$(echo '' \
| FZF_HEAD 'what should I call this clip? (.mp3)' \
| FZF_USER_INPUT 'what should I call this clip? (.mp3)' \
| sed 's/\.mp3$//' \
)
[ ! $OUTPUT_FILE ] && ABORT
@ -49,6 +44,3 @@ GET_AUDIO_CLIP() {
&& SUCCESS "created clip '$OUTPUT_FILE'" \
|| { ERROR "error creating clip '$(basename $OUTPUT_FILE)' (see above)"; return 3; }
}
#####################################################################
GET_AUDIO_CLIP $@

8
zsh/misc/tally Executable file
View File

@ -0,0 +1,8 @@
#!/bin/zsh
use misc/tally
#####################################################################
MAIN() {
unset USAGE
TALLY $@
}

View File

@ -1,13 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use office/latex
CHECK_ENVIRONMENT
#####################################################################
PDFLATEX() {
MAIN() {
[ ! $1 ] && FAIL 1 'must provide filename'
local FILENAME=$(LATEX__GET_MAIN_FILENAME "$1")
@ -26,6 +21,3 @@ PDFLATEX() {
SUCCESS "created '$(echo $FILENAME | sed 's/\.[^.]*$/.pdf/')'"
}
#####################################################################
PDFLATEX $@

View File

@ -1,13 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use office/latex
CHECK_ENVIRONMENT
#####################################################################
CLEAN_LATEX_LOGFILES() {
MAIN() {
local DIRECTORY=$(SCWRYPTS__GET_REALPATH ".")
[ $1 ] && DIRECTORY="$(dirname "$(LATEX__GET_MAIN_FILENAME "$1")")"
[ $DIRECTORY ] && [ -d $DIRECTORY ] \
@ -18,6 +13,3 @@ CLEAN_LATEX_LOGFILES() {
SUCCESS "cleaned up latex artifacts in '$DIRECTORY'"
}
#####################################################################
CLEAN_LATEX_LOGFILES $@

View File

@ -1,15 +1,10 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use office/latex
CHECK_ENVIRONMENT
#####################################################################
TEMPLATE_DIR="${0:a:h}/templates"
CREATE_NEW_LATEX_DOCUMENT_FROM_TEMPLATE() {
MAIN() {
local DOCUMENT_DIR="$EXECUTION_DIR"
local TEMPLATE=$(GET_TEMPLATES | FZF 'select a template')
[ ! $TEMPLATE ] && ABORT
@ -39,6 +34,8 @@ CREATE_NEW_LATEX_DOCUMENT_FROM_TEMPLATE() {
SUCCESS "finished generating '$(basename $DOCUMENT_FILE)' from '$TEMPLATE'"
}
#####################################################################
GET_TEMPLATES() {
find "$TEMPLATE_DIR" -type d | sed "s^$TEMPLATE_DIR/*^^; /^$/d"
}
@ -59,4 +56,4 @@ SLUGIFY_TITLE() {
}
#####################################################################
CREATE_NEW_LATEX_DOCUMENT_FROM_TEMPLATE $@
MAIN $@

View File

@ -1,10 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use office/latex
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
LATEX__GET_PDF $@
}

View File

@ -1,18 +1,10 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use office/latex
CHECK_ENVIRONMENT
#####################################################################
OPEN_PDF() {
MAIN() {
local PDF=$(LATEX__GET_PDF $@)
[ ! $PDF ] && return 1
OPEN "$PDF"
}
#####################################################################
OPEN_PDF $@

View File

@ -1,14 +1,9 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use office/memo
CHECK_ENVIRONMENT
#####################################################################
OPEN_MEMO() {
local MEMO_NAME=$(MEMO__LIST_ALL | FZF_TAIL 'select/create a memo')
MAIN() {
local MEMO_NAME=$(MEMO__LIST_ALL | FZF_USER_INPUT 'select/create a memo')
[ ! "$MEMO_NAME" ] && ABORT
MEMO_FILE="$MEMO__DIR/$MEMO_NAME.$MEMO__FILETYPE"
@ -26,6 +21,3 @@ OPEN_MEMO() {
EDIT "$MEMO_FILE"
}
#####################################################################
OPEN_MEMO $@

View File

@ -1,13 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use office/memo
CHECK_ENVIRONMENT
#####################################################################
DELETE_MEMO() {
MAIN() {
local MEMO_NAME=$(MEMO__LIST_ALL | FZF 'select a memo to delete')
local MEMO_FILE="$MEMO__DIR/$MEMO_NAME.$MEMO__FILETYPE"
[ "$MEMO_NAME" ] && [ -f "$MEMO_FILE" ] || ABORT
@ -28,6 +23,3 @@ DELETE_MEMO() {
|| FAIL 1 "failed to remove memo '$MEMO_NAME'" \
;
}
#####################################################################
DELETE_MEMO $@

View File

@ -1,13 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use redis
CHECK_ENVIRONMENT
#####################################################################
CURL_WITH_CACHE() {
MAIN() {
[ ! $TTL ] && TTL=10
[[ $(REDIS__ENABLED) -eq 0 ]] && {
@ -46,7 +41,6 @@ CURL_WITH_CACHE() {
echo $OUTPUT
}
GET_URL_KEY() { echo "scwrypts:curl:$1" | sed 's/\s\+/+/g'; }
#####################################################################
CURL_WITH_CACHE $@
GET_URL_KEY() { echo "scwrypts:curl:$1" | sed 's/\s\+/+/g'; }

52
zsh/sanity-check Executable file
View File

@ -0,0 +1,52 @@
#!/bin/zsh
#####################################################################
USAGE__options='
-e, --exit-code desired exit code of the function (default "0")
-f, --output-function one of the zsh/utils output functions (default "SUCCESS")
-m, --message a message to display (default "Hello, World!")
'
USAGE__description='
a simple hello-world-style script which allows specific scwrypts
conditions to be quickly emulated
'
#####################################################################
MAIN() {
local OUTPUT_FUNCTION=SUCCESS
local EXIT_CODE=0
local MESSAGE='Hello, world!'
ARGUMENT_REQUIRED() { ERROR "'$1' requires an argument"; }
while [[ $# -gt 0 ]]
do
case $1 in
-e | --exit-code )
EXIT_CODE="$2"
[ $2 ] && shift 1 || ARGUMENT_REQUIRED
;;
-f | --output-function )
OUTPUT_FUNCTION="$2"
[ $2 ] && shift 1 || ARGUMENT_REQUIRED
;;
-m | --message )
MESSAGE="$2"
[ $2 ] && shift 1 || ARGUMENT_REQUIRED
;;
* ) ERROR "unknown argument '$1'" ;;
esac
shift 1
done
CHECK_ERRORS
##########################################
[[ $OUTPUT_FUNCTION =~ ^FAIL$ ]] && FAIL $EXIT_CODE "$MESSAGE"
$OUTPUT_FUNCTION "$MESSAGE"
return $EXIT_CODE
}

View File

@ -1,13 +1,9 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/meta
CHECK_ENVIRONMENT
#####################################################################
USER_CONFIG_OVERRIDES="$SCWRYPTS_CONFIG_PATH/config.zsh"
MAIN() {
local USER_CONFIG_OVERRIDES="$SCWRYPTS_CONFIG_PATH/config.zsh"
[ ! -f "$USER_CONFIG_OVERRIDES" ] && {
STATUS 'first-time setup detected; creating local configuration override...'
@ -34,3 +30,4 @@ USER_CONFIG_OVERRIDES="$SCWRYPTS_CONFIG_PATH/config.zsh"
SUCCESS 'saved new configuration'
REMINDER 'changes which affect the hot-key plugin will require a ZSHRC reload'
}

View File

@ -1,12 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/environment-files
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
PROMPT 'choose an environment to copy'
TEMPLATE_ENV_NAME=$(SCWRYPTS__SELECT_ENV)
[ ! $TEMPLATE_ENV_NAME ] && ABORT
@ -14,7 +10,7 @@ TEMPLATE_ENV_NAME=$(SCWRYPTS__SELECT_ENV)
STATUS "selected '$TEMPLATE_ENV_NAME'"
PROMPT 'enter new environment name'
ENV_NAME=$(echo '' | FZF_HEAD 'new environment')
ENV_NAME=$(echo '' | FZF_USER_INPUT 'new environment')
[ ! $ENV_NAME ] && ABORT
SCWRYPTS__GET_ENV_NAMES | grep -q $ENV_NAME && FAIL 1 "'$ENV_NAME' already exists"
@ -36,3 +32,4 @@ SCWRYPTS__RUN --name scwrypts/environment/synchronize --group scwrypts --type zs
&& SUCCESS "finished copy environment '$TEMPLATE_ENV_NAME > $ENV_NAME'" \
|| FAIL 3 'error during synchronization of new environment (see above)' \
;
}

View File

@ -1,12 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/environment-files
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
PROMPT 'choose an environment to delete'
ENV_NAME=$(SCWRYPTS__SELECT_ENV)
[ ! $ENV_NAME ] && ABORT
@ -34,3 +30,4 @@ do
done
CHECK_ERRORS -n || FAIL 2 "some errors ocurred when cleaning up $ENV_NAME"
}

View File

@ -1,12 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/environment-files
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
[ $1 ] && ENV_NAME="$1"
[ ! $1 ] && {
@ -43,3 +39,4 @@ SCWRYPTS__RUN --name scwrypts/environment/synchronize --group scwrypts --type zs
;
SUCCESS "environment '$ENV_NAME' successfully modified"
}

View File

@ -1,13 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/environment-files
CHECK_ENVIRONMENT
#####################################################################
SYNCHRONIZE() {
MAIN() {
while [[ $# -gt 0 ]]
do
case $1 in
@ -176,4 +171,4 @@ CLEANUP_ENVIRONMENT_FILE() {
rm "$1.temp"
}
#####################################################################
#####################################################################

View File

@ -1,10 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
cd $SCWRYPTS_ROOT
STATUS "Found $(ls $SCWRYPTS_LOG_PATH | wc -l) log files"
@ -20,3 +17,4 @@ rm -rf $SCWRYPTS_LOG_PATH/* \
&& SUCCESS 'done' \
|| { ERROR 'failed :c'; exit 2; }\
;
}

View File

@ -1,10 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
cd $SCWRYPTS_ROOT
PROMPT 'select a script log'
LOG_FILE=$(ls -t $SCWRYPTS_LOG_PATH | FZF 'logfile')
@ -13,3 +10,4 @@ LOG_FILE=$(ls -t $SCWRYPTS_LOG_PATH | FZF 'logfile')
STATUS 'opening logfile'
LESS "$SCWRYPTS_LOG_PATH/$LOG_FILE"
SUCCESS 'done'
}

View File

@ -1,12 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/virtualenv
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
while [[ $# -gt 0 ]]
do
case $1 in
@ -28,3 +24,4 @@ REMINDER "
Yn "drop and recreate $ENV_TYPE virtual environment?" || ABORT
REFRESH_VIRTUALENV $ENV_GROUP $ENV_TYPE
}

View File

@ -1,13 +1,8 @@
#!/bin/zsh
echo hey
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/virtualenv
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
STATUS 'beginning update for all environments'
FAILED_COUNT=0
@ -23,3 +18,4 @@ done
[[ $FAILED_COUNT -eq 0 ]] \
&& SUCCESS 'all environments up-to-date' \
|| FAIL $FAILED_COUNT "failed to update $FAILED_COUNT more environment(s)"
}

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use system/config
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
EDIT "$CONFIG__USER_SETTINGS"
}

View File

@ -1,19 +1,16 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use system/config
CHECK_ENVIRONMENT
#####################################################################
SETUP_SYMLINKS() {
MAIN() {
while read SYMLINK
do
SETUP_SYMLINK $(echo $SYMLINK | awk '{print $1;}') $(echo $SYMLINK | awk '{print $2}')
done < <(echo $SYMLINKS | sed -n '/^[^#]/p')
}
#####################################################################
SETUP_SYMLINK() {
[ ! $2 ] && FAIL 1 'must provide SOURCE_CONFIG and TARGET_CONFIG'
@ -21,7 +18,7 @@ SETUP_SYMLINK() {
local TARGET_CONFIG="$HOME/.config/$2"
[ ! -f "$SOURCE_CONFIG" ] && [ ! -d "$SOURCE_CONFIG" ] && [ -f "$TARGET_CONFIG" ] && {
INFO 'SOURCE_CONFIG is not tracked; copying from TARGET_CONFIG'
DEBUG 'SOURCE_CONFIG is not tracked; copying from TARGET_CONFIG'
mkdir -p "$(dirname "$SOURCE_CONFIG")"
cp "$TARGET_CONFIG" "$SOURCE_CONFIG"
}
@ -42,6 +39,3 @@ SETUP_SYMLINK() {
|| FAIL 3 "failed to create link '$TARGET_CONFIG'" \
;
}
#####################################################################
SETUP_SYMLINKS $@

View File

@ -1,13 +1,10 @@
#!/bin/zsh
DEPENDENCIES+=(tic)
REQUIRED_ENV+=()
use system/config
CHECK_ENVIRONMENT
#####################################################################
SETUP_TERMINFO() {
MAIN() {
[ ! $TERMINFO_PATH ] && return 0
[ ! -d $TERMINFO_PATH ] && FAIL 1 "TERMINFO_PATH='$TERMINFO_PATH' does not exist"
@ -22,6 +19,3 @@ SETUP_TERMINFO() {
CHECK_ERRORS
}
#####################################################################
SETUP_TERMINFO $@

View File

@ -1,13 +1,10 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/meta
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
STATUS 'updating all config files and links'
SCWRYPTS__RUN --name system/config/symlinks --group scwrypts --type zsh || exit 1
SCWRYPTS__RUN --name system/config/terminfo --group scwrypts --type zsh || exit 2
SUCCESS 'finished updating config files and links'
}

View File

@ -1,102 +0,0 @@
#!/bin/zsh
DEPENDENCIES+=(
diff
)
REQUIRED_ENV+=(
I3__MODEL_CONFIG
)
CHECK_ENVIRONMENT
#####################################################################
REGEX_FONT='^\(font [^0-9]*\)\(.*\)'
REGEX_DMENU="^\\(.*dmenu_run .*-fn '[^0-9]*\\)\\([0-9]*\\)'"
REGEX_BORDER='^\(for_window.*border pixel \)\(.*\)'
INSTALL() {
local USAGE="
usage: [...options...]
options
-f, --force force replacement of existing i3config
-n, --no-link if output config and template are the same, don't create link
-h, --help print this message and exit
environment
I3__MODEL_CONFIG fully-qualified path to sourced i3config
I3__GLOBAL_FONT_SIZE global font size
I3__DMENU_FONT_SIZE (optional) font size for 'dmenu' command
I3__BORDER_PIXEL_SIZE (optional) pixel-width of window borders
I3 provides no way to include dynamic variables in your config.
The main difference I want between my i3 configurations is font-size
to match the current monitor. Since i3-msg provides no way to change
font size, I run this command to update those variables on a local
copy of my sourced config
"
local FORCE=0
local AUTOLINK=1
while [[ $# -gt 0 ]]
do
case $1 in
-f | --force ) FORCE=1 ;;
-n | --no-link ) AUTOLINK=0 ;;
-h | --help ) USAGE; exit 0 ;;
esac
shift 1
done
STATUS 'reading local i3config'
[[ ^$I3__MODEL_CONFIG$ =~ ^$HOME/.config/i3/config$ ]] && {
STATUS "model configuration is default configuration"
I3__MODEL_CONFIG="$I3__MODEL_CONFIG.template"
[ ! -f "$I3__MODEL_CONFIG" ] && {
STATUS "creating template"
cp "$HOME/.config/i3/config" "$I3__MODEL_CONFIG.template"
}
STATUS "referring to '$I3__MODEL_CONFIG'"
}
local CONFIG=$(cat "$I3__MODEL_CONFIG")
[ ! $CONFIG ] && FAIL 1 "failed to read config at '$I3__MODEL_CONFIG'"
local CONFIG_FILE="$HOME/.config/i3/config"
[ ! -d $(dirname "$CONFIG_FILE") ] && mkdir -p "$(dirname "$CONFIG_FILE")"
[ -f "$CONFIG_FILE" ] && mv "$CONFIG_FILE" "$CONFIG_FILE.bak"
[ $I3__GLOBAL_FONT_SIZE ] && {
STATUS "setting global font size to '$I3__GLOBAL_FONT_SIZE'"
CONFIG=$(echo $CONFIG | sed "s/$REGEX_FONT/\\1$I3__GLOBAL_FONT_SIZE/")
}
[ $I3__DMENU_FONT_SIZE ] && {
STATUS "setting dmenu font size to '$I3__DMENU_FONT_SIZE'"
CONFIG=$(echo $CONFIG | sed "s/$REGEX_DMENU/\\1$I3__DMENU_FONT_SIZE'/")
}
[ $I3__BORDER_PIXEL_SIZE ] && {
STATUS "setting border pixel size to '$I3__BORDER_PIXEL_SIZE'"
CONFIG=$(echo $CONFIG | sed "s/$REGEX_BORDER/\\1$I3__BORDER_PIXEL_SIZE/")
}
echo $CONFIG > "$CONFIG_FILE"
[ -f "$CONFIG_FILE.bak" ] \
&& diff "$CONFIG_FILE" "$CONFIG_FILE.bak" -q >/dev/null \
&& mv "$CONFIG_FILE.bak" "$CONFIG_FILE" \
&& INFO "no changes were made" \
;
[[ $AUTOLINK -eq 1 ]] \
&& diff "$CONFIG_FILE" "$I3__MODEL_CONFIG" -q >/dev/null \
&& rm "$CONFIG_FILE" \
&& ln -s "$I3__MODEL_CONFIG" "$CONFIG_FILE" \
&& INFO "output is the same as model, i3config has been linked to model" \
;
[[ $FORCE -eq 1 ]] && rm "$CONFIG.bak" >/dev/null 2>&1
return 0
}
#####################################################################
INSTALL $@

View File

@ -1,136 +0,0 @@
#!/bin/zsh
DEPENDENCIES+=(
i3-msg
xdotool
xrandr
)
REQUIRED_ENV+=()
use system/desktop/notify
CHECK_ENVIRONMENT
#####################################################################
LAUNCH_OR_SHOW() {
INFO $@
local USAGE="
usage: <path-executable> [client-class] [...options...]
options
-c, --client <string> if different from the executable name, xprop CLIENT_CLASS
-s, --scale <value> (default: 0.8 or 0.5 if screen width >3000px)
-x, --x-offset <value> (default: 0.0)
-y, --y-offset <value> (default: 0.0)
-a, --always-launch invoke executable even if client-class exists
-n, --no-resize don't resize the window (ignores -sxy flags)
-l, --no-center leave the window wherever it was last positioned
--has-statusbar-icon (default: false) use if program has a statusbar icon
-h, --help print this message and exit
Makes it easy to bind appications to key shortcuts without having to
spin up redundant instances or cycle through the scratchpad queue.
Performs a variety of tasks based on states:
1) starts and application
2) adds all instances of the specified application to the scratchpad
3) (toggle) hides all visible instances
4) (toggle) shows all scratchpad-hidden instances
"
local APPLICATION CLIENT_CLASS
local XFFSET=0.0
local YFFSET=0.0
local SCALE=0.8
[[ $(xrandr | grep primary | awk '{print $4;}' | sed 's/x.*//') -gt 3000 ]] \
&& SCALE=0.5
local ALWAYS_LAUNCH=0
local RESIZE=1
local MOVE=1
local MIN_ACTIVE=1
while [[ $# -gt 0 ]]
do
case $1 in
-c | --client ) CLIENT_CLASS="$2"; shift 1 ;;
-x | --x-offset ) XFFSET=$2; shift 1 ;;
-y | --y-offset ) YFFSET=$2; shift 1 ;;
-s | --scale ) SCALE=$2; shift 1 ;;
-a | --always-launch ) ALWAYS_LAUNCH=1 ;;
-n | --no-resize ) RESIZE=0 ;;
-l | --no-center ) MOVE=0 ;;
--has-statusbar-icon ) MIN_ACTIVE=2 ;;
-h | --help ) USAGE; exit 0 ;;
* )
[ ! $APPLICATION ] && APPLICATION="$1" \
|| ERROR "extra positional argument '$1'"
esac
shift 1
done
[ ! $APPLICATION ] && ERROR 'path-executable required'
[ ! $CLIENT_CLASS ] && CLIENT_CLASS=$APPLICATION
[ $APPLICATION ] && {
__CHECK_DEPENDENCY $APPLICATION || {
ERROR "$APPLICATION is not installed"
NOTIFY "ERROR: $APPLICATION not found"
}
}
CHECK_ERRORS
xrandr | grep primary | awk '{print $4;}' | grep -q '^[0-9]\+$' || {
xrandr --output $(xrandr | grep ' connected' | awk '{print $1;}' | head -n1) --primary
}
local CURRENTLY_ACTIVE=$(xdotool search --onlyvisible --classname $CLIENT_CLASS 2>/dev/null | wc -l)
local LAUNCH_APP=$ALWAYS_LAUNCH
STATUS "looking for window process ids"
xdotool search --class $CLIENT_CLASS || LAUNCH_APP=1
[[ $LAUNCH_APP -eq 1 ]] && {
STATUS 'launching application'
i3-msg "exec --no-startup-id $APPLICATION;"
sleep .5
}
STATUS 'getting target window size'
WINDOW_SIZE=$(\
xrandr \
| grep 'connected primary' \
| sed 's/.*connected primary \([^x]*\)x\([^+]*\).*/\1 \2/' \
| awk -v f=$SCALE -v x=$XFFSET -v y=$YFFSET \
'{print int($1*f+x)," ",int($2*f+y);}'\
)
INFO "window size: $WINDOW_SIZE"
STATUS 'moving window to scratchpad'
i3-msg "[class=$CLIENT_CLASS] move scratchpad"
[[ $RESIZE -eq 1 ]] \
&& STATUS 'resizing window' \
&& i3-msg "[class=$CLIENT_CLASS] resize set $WINDOW_SIZE"
[[ $CURRENTLY_ACTIVE -lt $MIN_ACTIVE ]] && {
STATUS 'pulling window from scratchpad to foreground'
i3-msg "[class=$CLIENT_CLASS] scratchpad show"
}
STATUS 'moving window to center of current screen'
[[ $MOVE -eq 1 ]] && i3-msg "[class=$CLIENT_CLASS] move position center"
return 0
}
#####################################################################
LAUNCH_OR_SHOW $@

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/meta
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
SCWRYPTS__RUN --name system/packages/install --group scwrypts --type zsh -- --only-build $@
}

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/meta
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
SCWRYPTS__RUN --name system/packages/install --group scwrypts --type zsh -- --only-pull $@
}

View File

@ -1,13 +1,8 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use system/packages/git
CHECK_ENVIRONMENT
#####################################################################
INSTALL() {
MAIN() {
local USAGE="
usage: [...options...]
@ -71,7 +66,7 @@ INSTALL() {
[ ! $NAME ] && {
NAME=$(echo $TARGET | sed 's/.*\///; s/\.git$//')
INFO "using default name '$NAME'"
DEBUG "using default name '$NAME'"
}
[ -d "$PACKAGE_INSTALL_DIR/$NAME" ] && [[ $SKIP_PULL -eq 0 ]] && {
@ -86,6 +81,3 @@ INSTALL() {
[[ $SKIP_BUILD -eq 1 ]] && return 0
BUILD
}
#####################################################################
INSTALL $@

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use scwrypts/meta
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
SCWRYPTS__RUN --name system/packages/install --group scwrypts --type zsh -- --update $@
}

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use system/vim/vundle
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
EDIT "$VUNDLE__BUILD_DEFINITIONS"
}

View File

@ -1,16 +1,13 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use system/vim/vundle
CHECK_ENVIRONMENT
#####################################################################
PLUGIN_INSTALL() {
MAIN() {
[ -d "$HOME/.vim/bundle/Vundle.vim" ] || {
mkdir -p "$HOME/.vim/bundle/"
git clone https://github.com/VundleVim/Vundle.vim.git "$HOME/.vim/bundle/Vundle.vim"
}
VUNDLE__PLUGIN_INSTALL || return 1
VUNDLE__REBUILD_PLUGINS || return 2
}
#####################################################################
PLUGIN_INSTALL $@

View File

@ -1,9 +1,7 @@
#!/bin/zsh
DEPENDENCIES+=()
REQUIRED_ENV+=()
use system/vim/vundle
CHECK_ENVIRONMENT
#####################################################################
MAIN() {
VUNDLE__REBUILD_PLUGINS $@
}