Finalise docopt parser integration
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Luke Murphy 2020-10-26 09:46:14 +01:00
parent ae6c2c26ae
commit 44c41830a7
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
1 changed files with 65 additions and 54 deletions

119
abra
View File

@ -5,54 +5,28 @@
DOC="The cooperative cloud utility belt 🎩🐇
Usage:
abra
abra [--config=<path>] [--env=<path>] [--help]
[--stack=<path>] [--version] <command> [<args>]
Options:
-c, --config=<path> Stack configuration to use
-e, --env=<path> Environment variables to load
-h, --help Show this message and exit
-s, --stack=<path> Name of the target stack
-v, --version Show program version
These are common Abra commands used in various situations:
Commands:
"
server manage remote host
cp copy files to a container
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
### TODO(decentral1se): convert this into `DOC`
sub_help() {
echo "Usage: $PROGRAM_NAME [OPTIONS] COMMAND [ARGS]..."
echo ""
echo "Options:"
echo " --config Stack configuration to use"
echo " --env Environment variables to load"
echo " --help Show this message and exit"
echo " --stack Name of the target stack"
echo " --version Show program version"
echo ""
echo "Commands:"
echo " server manage remote host"
echo " cp copy files to a container"
echo " deploy let 'em rip"
echo " logs tail logs from a deployed service"
echo " multilogs tail logs from a whole stackk"
echo " run run a command in the specified service's container"
echo " secret manage secrets"
echo " upgrade upgrade to the latest version"
}
sub_secret_help() {
echo "Usage: $PROGRAM_NAME [global opts] secret <subcommand> [sub opts]"
echo ""
echo "Subcommands:"
echo " generate SECRET VERSION [PWGEN] generate & store secret"
echo " insert SECRET VERSION PW save PW in docker and pass"
}
sub_server_help() {
echo "Usage: $PROGRAM_NAME [global opts] server <subcommand> [sub opts]"
echo ""
echo "Subcommands:"
echo " add HOST [USER] [PORT] set up remote Docker context"
echo " rm HOST"
echo " use HOST activate remote Docker context"
echo " init HOST activate swarm mode"
}
###
See 'abra help <command>' to read about a specific subcommand."
# docopt parser below, refresh this parser with `docopt.sh abra`
# shellcheck disable=2016,1075
@ -121,16 +95,47 @@ local node_idx; ((testdepth++)) || true; for node_idx in "$@"; do
if ! "node_$node_idx"; then left=("${initial_left[@]}"); ((testdepth--)) || true
return 1; fi; done; if [[ $((--testdepth)) -eq 0 ]]; then
left=("${initial_left[@]}"); for node_idx in "$@"; do "node_$node_idx"; done; fi
return 0; }; stdout() { printf -- "cat <<'EOM'\n%s\nEOM\n" "$1"; }; stderr() {
return 0; }; optional() { local node_idx; for node_idx in "$@"; do
"node_$node_idx"; done; return 0; }; switch() { local i
for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
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; }; value() { local i; for i in "${!left[@]}"; do local l=${left[$i]}
if [[ ${parsed_params[$l]} = "$2" ]]; then
left=("${left[@]:0:$i}" "${left[@]:((i+1))}")
[[ $testdepth -gt 0 ]] && return 0; local value
value=$(printf -- "%q" "${parsed_values[$l]}"); if [[ $3 = true ]]; then
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() {
printf -- "cat <<'EOM' >&2\n%s\nEOM\n" "$1"; }; error() {
[[ -n $1 ]] && stderr "$1"; stderr "$usage"; _return 1; }; _return() {
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:0:74}
usage=${DOC:39:13}; digest=8c7d3; shorts=(); longs=(); argcounts=(); node_0(){
required ; }; node_1(){ required 0; }; cat <<<' docopt_exit() {
[[ -n $1 ]] && printf "%s\n" "$1" >&2; printf "%s\n" "${DOC:39:13}" >&2; exit 1
}'; unset ; parse 1 "$@"; return 0; local prefix=${DOCOPT_PREFIX:-''}; unset
local docopt_i=1; [[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2
for ((;docopt_i>0;docopt_i--)); do declare -p ; done; }
printf -- "exit %d\n" "$1"; exit "$1"; }; set -e; trimmed_doc=${DOC:0:857}
usage=${DOC:39:110}; digest=29484; shorts=(-c -e -h -s -v)
longs=(--config --env --help --stack --version); argcounts=(1 1 0 1 0)
node_0(){ value __config 0; }; node_1(){ value __env 1; }; node_2(){
switch __help 2; }; node_3(){ value __stack 3; }; node_4(){ switch __version 4
}; node_5(){ value _command_ a; }; node_6(){ value _args_ a; }; node_7(){
optional 0; }; node_8(){ optional 1; }; node_9(){ optional 2; }; node_10(){
optional 3; }; node_11(){ optional 4; }; node_12(){ optional 6; }; node_13(){
required 7 8 9 10 11 5 12; }; node_14(){ required 13; }
cat <<<' docopt_exit() { [[ -n $1 ]] && printf "%s\n" "$1" >&2
printf "%s\n" "${DOC:39:110}" >&2; exit 1; }'; unset var___config var___env \
var___help var___stack var___version var__command_ var__args_; parse 14 "$@"
local prefix=${DOCOPT_PREFIX:-''}; unset "${prefix}__config" "${prefix}__env" \
"${prefix}__help" "${prefix}__stack" "${prefix}__version" "${prefix}_command_" \
"${prefix}_args_"; eval "${prefix}"'__config=${var___config:-}'
eval "${prefix}"'__env=${var___env:-}'
eval "${prefix}"'__help=${var___help:-false}'
eval "${prefix}"'__stack=${var___stack:-}'
eval "${prefix}"'__version=${var___version:-false}'
eval "${prefix}"'_command_=${var__command_:-}'
eval "${prefix}"'_args_=${var__args_:-}'; local docopt_i=1
[[ $BASH_VERSION =~ ^4.3 ]] && docopt_i=2; for ((;docopt_i>0;docopt_i--)); do
declare -p "${prefix}__config" "${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`
PROGRAM_NAME=$(basename "$0")
@ -554,7 +559,13 @@ sub_volume() {
###### Main
SUBCOMMAND=$1
shift
# SUBCOMMAND=$1
# shift
# shellcheck disable=SC2086,SC2068
parse_subcommand $SUBCOMMAND "" $@
# parse_subcommand $SUBCOMMAND "" $@
abra() {
eval "$(docopt "$@")"
}
abra "$@"