Finally work out subcommands

This commit is contained in:
Luke Murphy 2020-10-26 10:05:03 +01:00
parent 44c41830a7
commit a13e58c6c0
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC

96
abra
View File

@ -2,11 +2,11 @@
###### Global help ###### Global help
DOC="The cooperative cloud utility belt 🎩🐇 DOC="
The cooperative cloud utility belt 🎩🐇
Usage: Usage: abra [-c|--config=<path>] [-e|--env=<path>] [-h|--help]
abra [--config=<path>] [--env=<path>] [--help] [-s|--stack=<path>] [-v|--version] <command> [<args>...]
[--stack=<path>] [--version] <command> [<args>]
Options: Options:
-c, --config=<path> Stack configuration to use -c, --config=<path> Stack configuration to use
@ -17,19 +17,33 @@ Options:
These are common Abra commands used in various situations: These are common Abra commands used in various situations:
server manage remote host Manage an application lifecycle:
cp copy files to a container
deploy let 'em rip deploy let 'em rip
logs tail logs from a deployed service
multilogs tail logs from a whole stack
run run a command in the specified service's container
secret manage secrets
upgrade upgrade to the latest version upgrade upgrade to the latest version
See 'abra help <command>' to read about a specific subcommand." View logs from running applications:
logs tail logs from a deployed service
multilogs tail logs from a whole stack
Interact with running containers:
cp copy files to a container
run run a command in the specified 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:
secret generate <secret> <version> [pwgen] generate & store secret
secret insert <secret> <version> <password> save password in docker & pass
See 'abra help <command>' to read about a specific subcommand.
"
# docopt parser below, refresh this parser with `docopt.sh abra` # docopt parser below, refresh this parser with `docopt.sh abra`
# shellcheck disable=2016,1075 # shellcheck disable=2016,1075,2154
docopt() { parse() { if ${DOCOPT_DOC_CHECK:-true}; then local doc_hash docopt() { parse() { if ${DOCOPT_DOC_CHECK:-true}; then local doc_hash
if doc_hash=$(printf "%s" "$DOC" | (sha256sum 2>/dev/null || shasum -a 256)); then if doc_hash=$(printf "%s" "$DOC" | (sha256sum 2>/dev/null || shasum -a 256)); then
if [[ ${doc_hash:0:5} != "$digest" ]]; then if [[ ${doc_hash:0:5} != "$digest" ]]; then
@ -95,8 +109,18 @@ local node_idx; ((testdepth++)) || true; for node_idx in "$@"; do
if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true
return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then
left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi
return 0; }; optional() { local node_idx; for node_idx in "$@"; do return 0; }; either() { local initial_left=("${left[@]}"); local best_match_idx
"node_$node_idx"; done; return 0; }; switch() { local i local match_count; local node_idx; ((testdepth++)) || true
for node_idx in "$@"; do if "node_$node_idx"; then
if [[ -z $match_count || ${#left[@]} -lt $match_count ]]; then
best_match_idx=$node_idx; match_count=${#left[@]}; fi; fi
left=("${initial_left[@]}"); done; ((testdepth--)) || true
if [[ -n $best_match_idx ]]; then "node_$best_match_idx"; return 0; fi
left=("${initial_left[@]}"); return 1; }; optional() { local node_idx
for node_idx in "$@"; do "node_$node_idx"; done; return 0; }; oneormore() {
local i=0; local prev=${#left[@]}; while "node_$1"; do ((i++)) || true
[[ $prev -eq ${#left[@]} ]] && break; prev=${#left[@]}; done
if [[ $i -ge 1 ]]; then return 0; fi; return 1; }; switch() { local i
for i in "${!left[@]}"; do local l=${left[$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))}")
@ -111,31 +135,37 @@ 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:0:857} printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:1:1345}
usage=${DOC:39:110}; digest=29484; shorts=(-c -e -h -s -v) usage=${DOC:40:131}; digest=f2937; shorts=(-c -e -h -s -v)
longs=(--config --env --help --stack --version); argcounts=(1 1 0 1 0) longs=(--config --env --help --stack --version); argcounts=(1 1 0 1 0)
node_0(){ value __config 0; }; node_1(){ value __env 1; }; node_2(){ node_0(){ value __config 0 true; }; node_1(){ value __env 1 true; }; node_2(){
switch __help 2; }; node_3(){ value __stack 3; }; node_4(){ switch __version 4 switch __help 2; }; node_3(){ value __stack 3 true; }; node_4(){
}; node_5(){ value _command_ a; }; node_6(){ value _args_ a; }; node_7(){ switch __version 4; }; node_5(){ value _command_ a; }; node_6(){
optional 0; }; node_8(){ optional 1; }; node_9(){ optional 2; }; node_10(){ value _args_ a true; }; node_7(){ optional 0 0; }; node_8(){ optional 1 1; }
optional 3; }; node_11(){ optional 4; }; node_12(){ optional 6; }; node_13(){ node_9(){ either 2 2; }; node_10(){ optional 9; }; node_11(){ optional 3 3; }
required 7 8 9 10 11 5 12; }; node_14(){ required 13; } node_12(){ either 4 4; }; node_13(){ optional 12; }; node_14(){ oneormore 6; }
cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2 node_15(){ optional 14; }; node_16(){ required 7 8 10 11 13 5 15; }; node_17(){
printf "%s\n" "${DOC:39:110}" >&2; exit 1; }'; unset var___config var___env \ required 16; }; cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
var___help var___stack var___version var__command_ var__args_; parse 14 "$@" printf "%s\n" "${DOC:40:131}" >&2; exit 1; }'; unset var___config var___env \
var___help var___stack var___version var__command_ var__args_; parse 17 "$@"
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__config" "${prefix}__env" \ local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__config" "${prefix}__env" \
"${prefix}__help" "${prefix}__stack" "${prefix}__version" "${prefix}_command_" \ "${prefix}__help" "${prefix}__stack" "${prefix}__version" "${prefix}_command_" \
"${prefix}_args_"; eval "${prefix}"'__config=${var___config:-}' "${prefix}_args_"; if declare -p var___config >/dev/null 2>&1; then
eval "${prefix}"'__env=${var___env:-}' eval "${prefix}"'__config=("${var___config[@]}")'; else
eval "${prefix}"'__config=()'; fi; if declare -p var___env >/dev/null 2>&1; then
eval "${prefix}"'__env=("${var___env[@]}")'; else eval "${prefix}"'__env=()'; fi
eval "${prefix}"'__help=${var___help:-false}' eval "${prefix}"'__help=${var___help:-false}'
eval "${prefix}"'__stack=${var___stack:-}' if declare -p var___stack >/dev/null 2>&1; then
eval "${prefix}"'__stack=("${var___stack[@]}")'; else
eval "${prefix}"'__stack=()'; fi
eval "${prefix}"'__version=${var___version:-false}' eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'_command_=${var__command_:-}' eval "${prefix}"'_command_=${var__command_:-}'
eval "${prefix}"'_args_=${var__args_:-}'; local docopt_i=1 if declare -p var__args_ >/dev/null 2>&1; then
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do eval "${prefix}"'_args_=("${var__args_[@]}")'; else eval "${prefix}"'_args_=()'
declare -p "${prefix}__config" "${prefix}__env" "${prefix}__help" \ fi; local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
"${prefix}__stack" "${prefix}__version" "${prefix}_command_" "${prefix}_args_" for ((;docopt_i>0;docopt_i--)); do declare -p "${prefix}__config" \
done; } "${prefix}__env" "${prefix}__help" "${prefix}__stack" "${prefix}__version" \
"${prefix}_command_" "${prefix}_args_"; 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")