refactor: split out label subcommands into distinct commands

This commit is contained in:
Jose Diaz-Gonzalez
2025-11-16 16:30:56 -05:00
parent 4eafd3b5b7
commit 12cc31427b
13 changed files with 146 additions and 83 deletions

View File

@@ -5,14 +5,28 @@ source "$PLUGIN_AVAILABLE_PATH/caddy-vhosts/internal-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
cmd-caddy-label() { cmd-caddy-label-add() {
declare desc="set/unset/show container proxy labels" declare desc="adds container label to app for specified proxy"
declare cmd="caddy:label" declare cmd="caddy:label:add"
[[ "$1" == "$cmd" ]] && shift 1 [[ "$1" == "$cmd" ]] && shift 1
declare APP="$1"
set -- caddy "$@" cmd-proxy-label-add "proxy:label:add" "caddy" "$@"
cmd-proxy-label "$@" }
cmd-caddy-label-remove() {
declare desc="removes container label from app for specified proxy"
declare cmd="caddy:label:remove"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-remove "proxy:label:remove" "caddy" "$@"
}
cmd-caddy-label-show() {
declare desc="shows container labels for app for specified proxy"
declare cmd="caddy:label:show"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-show "proxy:label:show" "caddy" "$@"
} }
cmd-caddy-report() { cmd-caddy-report() {

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
[[ " caddy:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT" [[ " caddy:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
source "$PLUGIN_AVAILABLE_PATH/caddy-vhosts/help-functions" source "$PLUGIN_AVAILABLE_PATH/caddy-vhosts/help-functions"
source "$PLUGIN_AVAILABLE_PATH/caddy-vhosts/command-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
@@ -9,6 +10,18 @@ case "$1" in
cmd-caddy-help "$@" cmd-caddy-help "$@"
;; ;;
caddy:label:add)
cmd-caddy-label-add "$@"
;;
caddy:label:remove)
cmd-caddy-label-remove "$@"
;;
caddy:label:show)
cmd-caddy-label-show "$@"
;;
*) *)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT" exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;; ;;

View File

@@ -1,6 +0,0 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/caddy-vhosts/command-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-caddy-label "$@"

View File

@@ -5,14 +5,28 @@ source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/internal-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
cmd-haproxy-label() { cmd-haproxy-label-add() {
declare desc="set/unset/show container proxy labels" declare desc="adds container label to app for specified proxy"
declare cmd="haproxy:label" declare cmd="haproxy:label:add"
[[ "$1" == "$cmd" ]] && shift 1 [[ "$1" == "$cmd" ]] && shift 1
declare APP="$1"
set -- haproxy "$@" cmd-proxy-label-add "proxy:label:add" "haproxy" "$@"
cmd-proxy-label "$@" }
cmd-haproxy-label-remove() {
declare desc="removes container label from app for specified proxy"
declare cmd="haproxy:label:remove"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-remove "proxy:label:remove" "haproxy" "$@"
}
cmd-haproxy-label-show() {
declare desc="shows container labels for app for specified proxy"
declare cmd="haproxy:label:show"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-show "proxy:label:show" "haproxy" "$@"
} }
cmd-haproxy-report() { cmd-haproxy-report() {

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
[[ " haproxy:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT" [[ " haproxy:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/help-functions" source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/help-functions"
source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/command-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
@@ -9,6 +10,18 @@ case "$1" in
cmd-haproxy-help "$@" cmd-haproxy-help "$@"
;; ;;
haproxy:label:add)
cmd-haproxy-label-add "$@"
;;
haproxy:label:remove)
cmd-haproxy-label-remove "$@"
;;
haproxy:label:show)
cmd-haproxy-label-show "$@"
;;
*) *)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT" exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;; ;;

View File

@@ -1,6 +0,0 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/haproxy-vhosts/command-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-haproxy-label "$@"

View File

@@ -5,14 +5,28 @@ source "$PLUGIN_AVAILABLE_PATH/openresty-vhosts/internal-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
cmd-openresty-label() { cmd-openresty-label-add() {
declare desc="set/unset/show container proxy labels" declare desc="adds container label to app for specified proxy"
declare cmd="openresty:label" declare cmd="openresty:label:add"
[[ "$1" == "$cmd" ]] && shift 1 [[ "$1" == "$cmd" ]] && shift 1
declare APP="$1"
set -- openresty "$@" cmd-proxy-label-add "proxy:label:add" "openresty" "$@"
cmd-proxy-label "$@" }
cmd-openresty-label-remove() {
declare desc="removes container label from app for specified proxy"
declare cmd="openresty:label:remove"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-remove "proxy:label:remove" "openresty" "$@"
}
cmd-openresty-label-show() {
declare desc="shows container labels for app for specified proxy"
declare cmd="openresty:label:show"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-show "proxy:label:show" "openresty" "$@"
} }
cmd-openresty-report() { cmd-openresty-report() {

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
[[ " openresty:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT" [[ " openresty:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
source "$PLUGIN_AVAILABLE_PATH/openresty-vhosts/help-functions" source "$PLUGIN_AVAILABLE_PATH/openresty-vhosts/help-functions"
source "$PLUGIN_AVAILABLE_PATH/openresty-vhosts/command-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
@@ -9,6 +10,18 @@ case "$1" in
cmd-openresty-help "$@" cmd-openresty-help "$@"
;; ;;
openresty:label:add)
cmd-openresty-label-add "$@"
;;
openresty:label:remove)
cmd-openresty-label-remove "$@"
;;
openresty:label:show)
cmd-openresty-label-show "$@"
;;
*) *)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT" exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;; ;;

View File

@@ -1,6 +0,0 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/openresty-vhosts/command-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-openresty-label "$@"

View File

@@ -11,25 +11,27 @@ cmd-proxy-label-show() {
declare PROXY="$1" APP="$2" PASSED_LABEL_NAME="$3" declare PROXY="$1" APP="$2" PASSED_LABEL_NAME="$3"
shift 2 shift 2
[[ -z "$PROXY" ]] && dokku_log_fail "Error invoking proxy:label command"
verify_app_name "$APP"
if [[ -n "$PASSED_LABEL_NAME" ]]; then if [[ -n "$PASSED_LABEL_NAME" ]]; then
fn-proxy-show-label "$PROXY" "$APP" "$PASSED_LABEL_NAME" fn-proxy-show-label "$PROXY" "$APP" "$PASSED_LABEL_NAME"
return return
fi fi
fn-proxy-display-labels "$PROXY" "$APP" fn-proxy-display-labels "$PROXY" "$APP"
return 0
} }
cmd-proxy-label-add() { cmd-proxy-label-add() {
declare desc="adds container label to app for specified proxy" declare desc="adds container label to app for specified proxy"
declare cmd="proxy:label:add"
[[ "$1" == "$cmd" ]] && shift 1
declare PROXY="$1" APP="$2" PASSED_LABEL_NAME="$3" PASSED_LABEL_VALUE="$4" declare PROXY="$1" APP="$2" PASSED_LABEL_NAME="$3" PASSED_LABEL_VALUE="$4"
if [[ -z "$PASSED_LABEL_NAME" ]]; then
dokku_log_fail "Please specify a container label for the app"
fi
if [[ -z "$PASSED_LABEL_VALUE" ]]; then [[ -z "$PROXY" ]] && dokku_log_fail "Error invoking proxy:label command"
dokku_log_fail "Please specify a value for the container label" [[ -z "$PASSED_LABEL_NAME" ]] && dokku_log_fail "Please specify a container label for the app"
fi [[ -z "$PASSED_LABEL_VALUE" ]] && dokku_log_fail "Please specify a value for the container label"
verify_app_name "$APP"
local proxy_labels_file_path="$(fn-proxy-get-labels-file-path "$PROXY" "$APP")" local proxy_labels_file_path="$(fn-proxy-get-labels-file-path "$PROXY" "$APP")"
touch "$proxy_labels_file_path" touch "$proxy_labels_file_path"
@@ -41,11 +43,13 @@ cmd-proxy-label-add() {
cmd-proxy-label-remove() { cmd-proxy-label-remove() {
declare desc="removes container label from app for specified proxy" declare desc="removes container label from app for specified proxy"
declare cmd="proxy:label:remove"
[[ "$1" == "$cmd" ]] && shift 1
declare PROXY="$1" APP="$2" PASSED_LABEL_NAME="$3" declare PROXY="$1" APP="$2" PASSED_LABEL_NAME="$3"
if [[ -z "$PASSED_LABEL_NAME" ]]; then [[ -z "$PROXY" ]] && dokku_log_fail "Error invoking proxy:label command"
dokku_log_fail "Please specify a container label for the app" [[ -z "$PASSED_LABEL_NAME" ]] && dokku_log_fail "Please specify a container label for the app"
fi verify_app_name "$APP"
local proxy_labels_file_path="$(fn-proxy-get-labels-file-path "$PROXY" "$APP")" local proxy_labels_file_path="$(fn-proxy-get-labels-file-path "$PROXY" "$APP")"
if [[ ! -s "$proxy_labels_file_path" ]]; then if [[ ! -s "$proxy_labels_file_path" ]]; then
@@ -57,28 +61,3 @@ cmd-proxy-label-remove() {
local all_proxy_labels="$(echo -e "${all_proxy_labels}" | sed -E "s/^--label ['\"]${escaped_label_name}=.*\$//")" local all_proxy_labels="$(echo -e "${all_proxy_labels}" | sed -E "s/^--label ['\"]${escaped_label_name}=.*\$//")"
echo -e "${all_proxy_labels}" | sed '/^$/d' | sort -u >"$proxy_labels_file_path" echo -e "${all_proxy_labels}" | sed '/^$/d' | sort -u >"$proxy_labels_file_path"
} }
cmd-proxy-label() {
declare desc="manage proxy labels for an app"
declare cmd="proxy:label"
shift 1 # the command comes from the upstream plugin
declare PROXY="$1" ACTION="$2" APP="$3" PASSED_LABEL_NAME="$4" PASSED_LABEL_VALUE="$5"
[[ -z "$PROXY" ]] && dokku_log_fail "Error invoking proxy:label command"
verify_app_name "$APP"
if [[ -n "$PASSED_LABEL_NAME" ]] && [[ ! "$PASSED_LABEL_NAME" =~ ^$PROXY ]]; then
dokku_log_fail "Please specify a valid $PROXY label name for the app"
fi
if [[ "$action" == "show" ]]; then
cmd-proxy-label-show "$PROXY" "$APP" "$PASSED_LABEL_NAME"
return 0
elif [[ "$ACTION" == "set" ]]; then
cmd-proxy-label-add "$PROXY" "$APP" "$PASSED_LABEL_NAME" "$PASSED_LABEL_VALUE"
elif [[ "$action" == "unset" ]]; then
cmd-proxy-label-remove "$PROXY" "$APP" "$PASSED_LABEL_NAME"
else
dokku_log_fail "Please specify a valid action ('set', 'unset' or 'show')"
fi
}

View File

@@ -5,14 +5,28 @@ source "$PLUGIN_AVAILABLE_PATH/traefik-vhosts/internal-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
cmd-traefik-label() { cmd-traefik-label-add() {
declare desc="set/unset/show container proxy labels" declare desc="adds container label to app for specified proxy"
declare cmd="traefik:label" declare cmd="traefik:label:add"
[[ "$1" == "$cmd" ]] && shift 1 [[ "$1" == "$cmd" ]] && shift 1
declare APP="$1"
set -- traefik "$@" cmd-proxy-label-add "proxy:label:add" "traefik" "$@"
cmd-proxy-label "$@" }
cmd-traefik-label-remove() {
declare desc="removes container label from app for specified proxy"
declare cmd="traefik:label:remove"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-remove "proxy:label:remove" "traefik" "$@"
}
cmd-traefik-label-show() {
declare desc="shows container labels for app for specified proxy"
declare cmd="traefik:label:show"
[[ "$1" == "$cmd" ]] && shift 1
cmd-proxy-label-show "proxy:label:show" "traefik" "$@"
} }
cmd-traefik-report() { cmd-traefik-report() {

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
[[ " traefik:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT" [[ " traefik:help help " == *" $1 "* ]] || exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
source "$PLUGIN_AVAILABLE_PATH/traefik-vhosts/help-functions" source "$PLUGIN_AVAILABLE_PATH/traefik-vhosts/help-functions"
source "$PLUGIN_AVAILABLE_PATH/traefik-vhosts/command-functions"
set -eo pipefail set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x [[ $DOKKU_TRACE ]] && set -x
@@ -9,6 +10,18 @@ case "$1" in
cmd-traefik-help "$@" cmd-traefik-help "$@"
;; ;;
traefik:label:add)
cmd-traefik-label-add "$@"
;;
traefik:label:remove)
cmd-traefik-label-remove "$@"
;;
traefik:label:show)
cmd-traefik-label-show "$@"
;;
*) *)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT" exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;; ;;

View File

@@ -1,6 +0,0 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/traefik-vhosts/command-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-traefik-label "$@"