Cleaner way to handle special actions 'help' and 'version'

This commit is contained in:
Tortue Torche 2019-08-10 22:13:34 -04:00 committed by Tortue Torche
parent 3b6a8bf7e6
commit 7f94f8b5da

68
psu Executable file → Normal file
View File

@ -30,37 +30,35 @@ main() {
set_globals "$@" set_globals "$@"
if [ "$ACTION" != "help" ] && [ "$ACTION" != "version" ]; then # Get Portainer auth token. Will be used on every API request.
# Get Portainer auth token. Will be used on every API request. echo_verbose "Getting auth token..."
echo_verbose "Getting auth token..." AUTH_TOKEN=$(http \
AUTH_TOKEN=$(http \ --check-status \
--check-status \ --ignore-stdin \
--ignore-stdin \ --verify=$HTTPIE_VERIFY_SSL \
--verify=$HTTPIE_VERIFY_SSL \ $PORTAINER_URL/api/auth \
$PORTAINER_URL/api/auth \ username=$PORTAINER_USER \
username=$PORTAINER_USER \ password=$PORTAINER_PASSWORD)
password=$PORTAINER_PASSWORD) check_for_errors $? "$AUTH_TOKEN"
check_for_errors $? "$AUTH_TOKEN" echo_debug "Get auth token response -> $(echo $AUTH_TOKEN | jq -C .)"
echo_debug "Get auth token response -> $(echo $AUTH_TOKEN | jq -C .)" AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt)
AUTH_TOKEN=$(echo $AUTH_TOKEN | jq -r .jwt) echo_debug "Auth token -> $AUTH_TOKEN"
echo_debug "Auth token -> $AUTH_TOKEN"
# Get list of all stacks # Get list of all stacks
echo_verbose "Getting stack $PORTAINER_STACK_NAME..." echo_verbose "Getting stack $PORTAINER_STACK_NAME..."
STACKS=$(http \ STACKS=$(http \
--check-status \ --check-status \
--ignore-stdin \ --ignore-stdin \
--verify=$HTTPIE_VERIFY_SSL \ --verify=$HTTPIE_VERIFY_SSL \
"$PORTAINER_URL/api/stacks" \ "$PORTAINER_URL/api/stacks" \
"Authorization: Bearer $AUTH_TOKEN") "Authorization: Bearer $AUTH_TOKEN")
check_for_errors $? "$STACKS" check_for_errors $? "$STACKS"
echo_debug "Get stacks response -> $(echo $STACKS | jq -C .)" echo_debug "Get stacks response -> $(echo $STACKS | jq -C .)"
# Get desired stack from stacks list by it's name # Get desired stack from stacks list by it's name
STACK=$(echo "$STACKS" \ STACK=$(echo "$STACKS" \
| jq --arg PORTAINER_STACK_NAME "$PORTAINER_STACK_NAME" -jc '.[] | select(.Name == $PORTAINER_STACK_NAME)') | jq --arg PORTAINER_STACK_NAME "$PORTAINER_STACK_NAME" -jc '.[] | select(.Name == $PORTAINER_STACK_NAME)')
echo_debug "Stack ${PORTAINER_STACK_NAME} -> $(echo $STACK | jq -C .)" echo_debug "Stack ${PORTAINER_STACK_NAME} -> $(echo $STACK | jq -C .)"
fi
if [ $ACTION == "deploy" ]; then if [ $ACTION == "deploy" ]; then
deploy deploy
@ -273,11 +271,9 @@ set_globals() {
# Check required arguments have been provided # Check required arguments have been provided
check_argument "$ACTION" "action" "ACTION" "a" "action" check_argument "$ACTION" "action" "ACTION" "a" "action"
if [ "$ACTION" != "help" ] && [ "$ACTION" != "version" ]; then check_argument "$PORTAINER_USER" "portainer user" "PORTAINER_USER" "u" "user"
check_argument "$PORTAINER_USER" "portainer user" "PORTAINER_USER" "u" "user" check_argument "$PORTAINER_PASSWORD" "portainer password" "PORTAINER_PASSWORD" "p" "password"
check_argument "$PORTAINER_PASSWORD" "portainer password" "PORTAINER_PASSWORD" "p" "password" check_argument "$PORTAINER_URL" "portainer url" "PORTAINER_URL" "l" "url"
check_argument "$PORTAINER_URL" "portainer url" "PORTAINER_URL" "l" "url"
fi
if [ "$ACTION" == "deploy" ]; then if [ "$ACTION" == "deploy" ]; then
check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c" "compose-file" check_argument "$DOCKER_COMPOSE_FILE" "docker compose file" "DOCKER_COMPOSE_FILE" "c" "compose-file"
if [ -n "$ENVIRONMENT_VARIABLES_FILE" ] && [[ ! -f "$ENVIRONMENT_VARIABLES_FILE" ]]; then if [ -n "$ENVIRONMENT_VARIABLES_FILE" ] && [[ ! -f "$ENVIRONMENT_VARIABLES_FILE" ]]; then
@ -285,7 +281,7 @@ set_globals() {
exit 1 exit 1
fi fi
fi fi
if [ "$ACTION" != "list" ] && [ "$ACTION" != "help" ] && [ "$ACTION" != "version" ]; then if [ "$ACTION" != "list" ]; then
check_argument "$PORTAINER_STACK_NAME" "portainer stack name" "PORTAINER_STACK_NAME" "n" "name" check_argument "$PORTAINER_STACK_NAME" "portainer stack name" "PORTAINER_STACK_NAME" "n" "name"
fi fi
} }
@ -436,6 +432,7 @@ inputs() {
if [ -n "$version_message" ]; then if [ -n "$version_message" ]; then
ACTION="version" ACTION="version"
echo "$version_message" echo "$version_message"
exit 0
fi fi
;; ;;
-h|--help|help) -h|--help|help)
@ -490,6 +487,7 @@ inputs() {
exit 1 exit 1
fi fi
fi fi
exit 0
;; ;;
-a=*|--action=*|-a|--action) -a=*|--action=*|-a|--action)
# DEPRECATED: To keep backwards compatibility with psu 0.1.x # DEPRECATED: To keep backwards compatibility with psu 0.1.x