Goodbye, parse_subcommand! 👋

Ref #18
This commit is contained in:
3wc 2020-10-26 16:06:40 +02:00
parent b49b510c43
commit c50b1d8760

264
abra
View File

@ -5,8 +5,21 @@
DOC=" DOC="
The cooperative cloud utility belt 🎩🐇 The cooperative cloud utility belt 🎩🐇
Usage: abra [-c|--config=<path>] [-e|--env=<path>] [-h|--help] Usage:
[-s|--stack=<path>] [-v|--version] <command> [<args>...] abra [options] deploy
abra [options] upgrade
abra [options] logs [<app>]
abra [options] multilogs [<app>]
abra [options] cp (<app>:<path> <path>|<path> <app>:<path>)
abra [options] run [--user=<user>] <app> <args>...
abra [options] run <app> <args>...
abra [options] secret generate <secret> <version> [<cmd>]
abra [options] secret insert <secret> <version> <data>
abra [options] server add <host> [<user>] [<port>]
abra [options] server rm <host>
abra [options] server use <host>
abra [options] server init <host>
abra [options] server ls
Options: Options:
-c, --config=<path> Stack configuration to use -c, --config=<path> Stack configuration to use
@ -15,26 +28,6 @@ Options:
-s, --stack=<path> Name of the target stack -s, --stack=<path> Name of the target stack
-v, --version Show program version -v, --version Show program version
These are common Abra commands used in various situations:
Manage an application lifecycle:
deploy let 'em rip
upgrade upgrade to the latest version
View logs from running applications:
logs tail logs from a deployed service
multilogs tail logs from a whole stack
Interact with running containers:
cp <src> <dest> copy files to a container
run <service> <cmd> run <cmd> in <service>'s container
Manage servers:
server add <host> [user] [port] add docker context
server rm <host> remove docker context
server use <host> activate docker context
server init <host> activate docker swarm mode
Manage secrets: Manage secrets:
secret generate <secret> <version> [pwgen] generate & store secret secret generate <secret> <version> [pwgen] generate & store secret
secret insert <secret> <version> <password> save password in docker & pass secret insert <secret> <version> <password> save password in docker & pass
@ -120,8 +113,14 @@ left=("${initial_left[@]}"); return 1; }; optional() { local node_idx
for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() { for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() {
local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done [[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; switch() { local i if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; _command() { local i
for i in "${!left[@]}"; do local l=${left[$i]} local name=${2:-$1}; for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = 'a' ]]; then
if [[ ${parsed_values[$l]} != "$name" ]]; then return 1; fi
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
eval "((var_$1++)) || true"; else eval "var_$1=true"; fi; return 0; fi; done
return 1; }; switch() { local i; for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}") left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then [[ $testdepth -gt 0 ]] && return 0; if [[ $3 = true ]]; then
@ -135,37 +134,88 @@ eval "var_$1+=($value)"; else eval "var_$1=$value"; fi; return 0; fi; done
return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() { return 1; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() { printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() { [[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1353} printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1102}
usage=${DOC:40:131}; digest=0d574; shorts=(-c -e -h -s -v) usage=${DOC:40:574}; digest=fab9b; shorts=(-h -v -e -c -s '')
longs=(--config --env --help --stack --version); argcounts=(1 1 0 1 0) longs=(--help --version --env --config --stack --user); argcounts=(0 0 1 1 1 1)
node_0(){ value __config 0 true; }; node_1(){ value __env 1 true; }; node_2(){ node_0(){ switch __help 0; }; node_1(){ switch __version 1; }; node_2(){
switch __help 2; }; node_3(){ value __stack 3 true; }; node_4(){ value __env 2; }; node_3(){ value __config 3; }; node_4(){ value __stack 4; }
switch __version 4; }; node_5(){ value _command_ a; }; node_6(){ node_5(){ value __user 5; }; node_6(){ value _app_ a; }; node_7(){
value _args_ a true; }; node_7(){ optional 0 0; }; node_8(){ optional 1 1; } value _app___path_ a; }; node_8(){ value _path_ a; }; node_9(){
node_9(){ either 2 2; }; node_10(){ optional 9; }; node_11(){ optional 3 3; } value _args_ a true; }; node_10(){ value _secret_ a; }; node_11(){
node_12(){ either 4 4; }; node_13(){ optional 12; }; node_14(){ oneormore 6; } value _version_ a; }; node_12(){ value _cmd_ a; }; node_13(){ value _data_ a; }
node_15(){ optional 14; }; node_16(){ required 7 8 10 11 13 5 15; }; node_17(){ node_14(){ value _host_ a; }; node_15(){ value _user_ a; }; node_16(){
required 16; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2 value _port_ a; }; node_17(){ _command deploy; }; node_18(){ _command upgrade; }
printf "%s\n" "${DOC:40:131}" >&2; exit 1; }'; unset var___config var___env \ node_19(){ _command logs; }; node_20(){ _command multilogs; }; node_21(){
var___help var___stack var___version var__command_ var__args_; parse 17 "$@" _command cp; }; node_22(){ _command run; }; node_23(){ _command secret; }
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__config" "${prefix}__env" \ node_24(){ _command generate; }; node_25(){ _command insert; }; node_26(){
"${prefix}__help" "${prefix}__stack" "${prefix}__version" "${prefix}_command_" \ _command server; }; node_27(){ _command add; }; node_28(){ _command rm; }
"${prefix}_args_"; if declare -p var___config >/dev/null 2>&1; then node_29(){ _command use; }; node_30(){ _command init; }; node_31(){ _command ls
eval "${prefix}"'__config=("${var___config[@]}")'; else }; node_32(){ optional 0 1 2 3 4; }; node_33(){ optional 32; }; node_34(){
eval "${prefix}"'__config=()'; fi; if declare -p var___env >/dev/null 2>&1; then required 33 17; }; node_35(){ required 33 18; }; node_36(){ optional 6; }
eval "${prefix}"'__env=("${var___env[@]}")'; else eval "${prefix}"'__env=()'; fi node_37(){ required 33 19 36; }; node_38(){ required 33 20 36; }; node_39(){
eval "${prefix}"'__help=${var___help:-false}' required 7 8; }; node_40(){ required 8 7; }; node_41(){ either 39 40; }
if declare -p var___stack >/dev/null 2>&1; then node_42(){ required 41; }; node_43(){ required 33 21 42; }; node_44(){
eval "${prefix}"'__stack=("${var___stack[@]}")'; else optional 5; }; node_45(){ oneormore 9; }; node_46(){ required 33 22 44 6 45; }
eval "${prefix}"'__stack=()'; fi node_47(){ required 33 22 6 45; }; node_48(){ optional 12; }; node_49(){
required 33 23 24 10 11 48; }; node_50(){ required 33 23 25 10 11 13; }
node_51(){ optional 15; }; node_52(){ optional 16; }; node_53(){
required 33 26 27 14 51 52; }; node_54(){ required 33 26 28 14; }; node_55(){
required 33 26 29 14; }; node_56(){ required 33 26 30 14; }; node_57(){
required 33 26 31; }; node_58(){
either 34 35 37 38 43 46 47 49 50 53 54 55 56 57; }; node_59(){ required 58; }
cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
printf "%s\n" "${DOC:40:574}" >&2; exit 1; }'; unset var___help var___version \
var___env var___config var___stack var___user var__app_ var__app___path_ \
var__path_ var__args_ var__secret_ var__version_ var__cmd_ var__data_ \
var__host_ var__user_ var__port_ var_deploy var_upgrade var_logs var_multilogs \
var_cp var_run var_secret var_generate var_insert var_server var_add var_rm \
var_use var_init var_ls; parse 59 "$@"; local prefix=${DOCOPT_PREFIX:-''}
unset "${prefix}__help" "${prefix}__version" "${prefix}__env" \
"${prefix}__config" "${prefix}__stack" "${prefix}__user" "${prefix}_app_" \
"${prefix}_app___path_" "${prefix}_path_" "${prefix}_args_" \
"${prefix}_secret_" "${prefix}_version_" "${prefix}_cmd_" "${prefix}_data_" \
"${prefix}_host_" "${prefix}_user_" "${prefix}_port_" "${prefix}deploy" \
"${prefix}upgrade" "${prefix}logs" "${prefix}multilogs" "${prefix}cp" \
"${prefix}run" "${prefix}secret" "${prefix}generate" "${prefix}insert" \
"${prefix}server" "${prefix}add" "${prefix}rm" "${prefix}use" "${prefix}init" \
"${prefix}ls"; eval "${prefix}"'__help=${var___help:-false}'
eval "${prefix}"'__version=${var___version:-false}' eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'_command_=${var__command_:-}' eval "${prefix}"'__env=${var___env:-}'
eval "${prefix}"'__config=${var___config:-}'
eval "${prefix}"'__stack=${var___stack:-}'
eval "${prefix}"'__user=${var___user:-}'; eval "${prefix}"'_app_=${var__app_:-}'
eval "${prefix}"'_app___path_=${var__app___path_:-}'
eval "${prefix}"'_path_=${var__path_:-}'
if declare -p var__args_ >/dev/null 2>&1; then if declare -p var__args_ >/dev/null 2>&1; then
eval "${prefix}"'_args_=("${var__args_[@]}")'; else eval "${prefix}"'_args_=()' eval "${prefix}"'_args_=("${var__args_[@]}")'; else eval "${prefix}"'_args_=()'
fi; local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2 fi; eval "${prefix}"'_secret_=${var__secret_:-}'
for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__config" \ eval "${prefix}"'_version_=${var__version_:-}'
"${prefix}__env" "${prefix}__help" "${prefix}__stack" "${prefix}__version" \ eval "${prefix}"'_cmd_=${var__cmd_:-}'; eval "${prefix}"'_data_=${var__data_:-}'
"${prefix}_command_" "${prefix}_args_"; done; } eval "${prefix}"'_host_=${var__host_:-}'
eval "${prefix}"'_user_=${var__user_:-}'
eval "${prefix}"'_port_=${var__port_:-}'
eval "${prefix}"'deploy=${var_deploy:-false}'
eval "${prefix}"'upgrade=${var_upgrade:-false}'
eval "${prefix}"'logs=${var_logs:-false}'
eval "${prefix}"'multilogs=${var_multilogs:-false}'
eval "${prefix}"'cp=${var_cp:-false}'; eval "${prefix}"'run=${var_run:-false}'
eval "${prefix}"'secret=${var_secret:-false}'
eval "${prefix}"'generate=${var_generate:-false}'
eval "${prefix}"'insert=${var_insert:-false}'
eval "${prefix}"'server=${var_server:-false}'
eval "${prefix}"'add=${var_add:-false}'; eval "${prefix}"'rm=${var_rm:-false}'
eval "${prefix}"'use=${var_use:-false}'
eval "${prefix}"'init=${var_init:-false}'; eval "${prefix}"'ls=${var_ls:-false}'
local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__help" \
"${prefix}__version" "${prefix}__env" "${prefix}__config" "${prefix}__stack" \
"${prefix}__user" "${prefix}_app_" "${prefix}_app___path_" "${prefix}_path_" \
"${prefix}_args_" "${prefix}_secret_" "${prefix}_version_" "${prefix}_cmd_" \
"${prefix}_data_" "${prefix}_host_" "${prefix}_user_" "${prefix}_port_" \
"${prefix}deploy" "${prefix}upgrade" "${prefix}logs" "${prefix}multilogs" \
"${prefix}cp" "${prefix}run" "${prefix}secret" "${prefix}generate" \
"${prefix}insert" "${prefix}server" "${prefix}add" "${prefix}rm" \
"${prefix}use" "${prefix}init" "${prefix}ls"; done; }
# docopt parser above, complete command for generating this parser is `docopt.sh abra` # docopt parser above, complete command for generating this parser is `docopt.sh abra`
PROGRAM_NAME=$(basename "$0") PROGRAM_NAME=$(basename "$0")
@ -186,29 +236,6 @@ yml_pattern_exists() {
return 1 return 1
} }
parse_subcommand() {
SUBCOMMAND="$1"
PREFIX=$2
if [ -n "$PREFIX" ]; then
PPREFIX="_$2"
SPREFIX="$2 "
fi
case $SUBCOMMAND in
"" | "-h" | "--help")
"sub${PPREFIX}_help"
;;
*)
shift 2
"sub${PPREFIX}_${SUBCOMMAND}" "$@"
if [ $? = 127 ]; then
error "Error: '$SPREFIX$SUBCOMMAND' is not a known subcommand."
fi
;;
esac
}
error() { error() {
echo "$(tput setaf 1)ERROR: $*$(tput sgr0)" echo "$(tput setaf 1)ERROR: $*$(tput sgr0)"
exit 1 exit 1
@ -292,25 +319,25 @@ sub_secret_insert() {
require_stack require_stack
load_context load_context
SECRET=$1 SECRET="$abra__secret_"
VERSION=$2 VERSION="$abra__version_"
PW=$3 PW="$abra__data_"
if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then if [ -z "$SECRET" ] || [ -z "$VERSION" ] || [ -z "$PW" ]; then
error "Required arguments missing" error "Required arguments missing"
echo
echo "Usage: $PROGRAM_NAME secret insert SECRET VERSION PW"
exit
fi fi
echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null echo "$PW" | docker secret create "${STACK_NAME}_${SECRET}_${VERSION}" - > /dev/null
if type pass > /dev/null 2>&1; then
echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null echo "$PW" | pass insert "hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}" -m > /dev/null
success "pass: hosts/$DOCKER_CONTEXT/${STACK_NAME}/${SECRET}"
fi
} }
sub_secret_generate(){ sub_secret_generate(){
SECRET=$1 SECRET="$abra__secret_"
VERSION=$2 VERSION="$abra__version_"
PWGEN=${3:-pwqgen} PWGEN=${abra__cmd_:-pwqgen}
if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then if [ -z "$SECRET" ] || [ -z "$VERSION" ]; then
error "Required arguments missing" error "Required arguments missing"
@ -323,14 +350,11 @@ sub_secret_generate(){
success "Password: $PW" success "Password: $PW"
sub_secret_insert "$SECRET" "$VERSION" "$PW" # FIXME 3wc: this is a little janky, might be better to make a
} # util_secret_insert function which this and sub_secret_insert can call
abra__data_="$PW"
sub_secret() { sub_secret_insert
SUBCOMMAND=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND" "secret" $@
} }
###### Subcommand `run` ###### Subcommand `run`
@ -338,25 +362,15 @@ sub_secret() {
sub_run(){ sub_run(){
require_stack require_stack
if [ "$1" == "-u" ]; then if [ -n "$abra___user" ]; then
RUN_USER="-u $2" RUN_USER="-u $abra___user"
shift 2
fi
SERVICE=$1
shift
if [ -z "$SERVICE" ]; then
echo "Usage: $PROGRAM_NAME run [-u USER] SERVICE [CMD]"
exit
fi fi
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \ CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
| grep "${STACK_NAME}_${SERVICE}" | head -n1 | cut -d',' -f1) | grep "${STACK_NAME}_${abra__app_}" | head -n1 | cut -d',' -f1)
if [ -z "$CONTAINER" ]; then if [ -z "$CONTAINER" ]; then
error "Can't find a container for ${STACK_NAME}_${SERVICE}" error "Can't find a container for ${STACK_NAME}_${abra__app_}"
exit exit
fi fi
@ -507,9 +521,9 @@ sub_server_init() {
} }
sub_server_add() { sub_server_add() {
HOST="$1" HOST="$abra__host_"
USERNAME="$2" USERNAME="$abra__user_"
PORT="$3" PORT="$abra__port_"
if [ -n "$PORT" ]; then if [ -n "$PORT" ]; then
PORT=":$PORT" PORT=":$PORT"
@ -524,26 +538,17 @@ sub_server_add() {
} }
sub_server_rm() { sub_server_rm() {
HOST="$1" docker context rm "$abra__host_"
docker context rm "$HOST"
} }
sub_server_use() { sub_server_use() {
docker context use "$1" docker context use "$abra__host_"
} }
sub_server_ls() { sub_server_ls() {
docker context ls docker context ls
} }
sub_server() {
SUBCOMMAND2=$1
shift
# shellcheck disable=SC2068
parse_subcommand "$SUBCOMMAND2" "server" $@
}
###### Subcommand `upgrade` ###### Subcommand `upgrade`
sub_upgrade() { sub_upgrade() {
@ -565,7 +570,11 @@ sub_volume() {
###### Main ###### Main
abra() { abra() {
declare abra___stack abra___env abra___config abra__command_ abra__args_ # TODO (3wc): we either need to do this, or add s'hellcheck disable' all over
# the place to handle the dynamically-defined vars
declare abra___stack abra___env abra___config abra__command_ abra__args_ \
abra__secret_ abra__version_ abra__data_ abra___user abra__host_ \
abra__app_ abra__port_ abra__user_
DOCOPT_PREFIX=abra_ DOCOPT_PREFIX=abra_
eval "$(docopt "$@")" eval "$(docopt "$@")"
@ -588,6 +597,21 @@ abra() {
load_custom_commands load_custom_commands
# Search for sub_* functions, and check if any of them matches enabled
# arguments (i.e. is a command and is specified)
for SUBCOMMAND in $(declare -Ff | grep 'sub_' | cut -d' ' -f3 | sort -r); do
IFS='_' read -r -a PARTS <<< "$SUBCOMMAND"
for PART in "${PARTS[@]:1}"; do
VAR=$(eval "echo \$abra_$PART")
if [ ! "$VAR" == "true" ]; then
continue 2
fi
done
abra__command_=$(IFS="_"; echo "${PARTS[*]:1}")
break
done
# Fallback for non-docopt-defined options
# shellcheck disable=SC2086 # shellcheck disable=SC2086
"sub_${abra__command_}" ${abra__args_[*]} "sub_${abra__command_}" ${abra__args_[*]}
} }