feat: add json output format support to storage:list

This will allow the command to be used in a programmatic fashion.
This commit is contained in:
Jose Diaz-Gonzalez
2022-12-26 16:33:40 -05:00
parent c0449d2bf1
commit 852dfc6287
7 changed files with 96 additions and 12 deletions

View File

@@ -26,6 +26,6 @@ get_bind_mounts() {
declare desc="strips docker options and prints mounts"
local -r phase_file_path=$1
if [[ -r "$phase_file_path" ]]; then
sed -e '/^-v/!d' -e 's/^-v/ /' <"$phase_file_path"
sed -e '/^-v/!d' -e 's/^-v //' <"$phase_file_path"
fi
}

View File

@@ -28,7 +28,7 @@ fn-help-content() {
declare desc="return help content"
cat <<help_content
storage:ensure-directory [--chown option] <directory>, Creates a persistent storage directory in the recommended storage path
storage:list <app>, List bind mounts for app's container(s) (host:container)
storage:list <app> [--format text|json], List bind mounts for app's container(s) (host:container)
storage:mount <app> <host-dir:container-dir>, Create a new bind mount
storage:report [<app>] [<flag>], Displays a checks report for one or more apps
storage:unmount <app> <host-dir:container-dir>, Remove an existing bind mount

View File

@@ -135,10 +135,24 @@ cmd-storage-list() {
declare desc="List all bound mounts"
declare cmd="storage:list"
[[ "$1" == "$cmd" ]] && shift 1
declare APP="$1"
local phase="deploy"
declare APP="$1" FORMAT_FLAG="$2" FORMAT_FLAG_VALUE="$3"
local phase="deploy" format="text"
if [[ "$FORMAT_FLAG" == "--format" ]]; then
if [[ "$FORMAT_FLAG_VALUE" == "json" ]] || [[ "$FORMAT_FLAG_VALUE" == "text" ]]; then
format="$FORMAT_FLAG_VALUE"
else
dokku_log_fail "Invalid --format value specified"
fi
elif [[ -n "$FORMAT_FLAG" ]]; then
dokku_log_fail "Invalid argument specified"
fi
verify_app_name "$APP"
dokku_log_quiet "$APP volume bind-mounts:"
plugn trigger storage-list "$APP" "$phase"
if [[ "$FORMAT_FLAG_VALUE" == "text" ]]; then
dokku_log_info1_quiet "$APP volume bind-mounts:"
plugn trigger storage-list "$APP" "$phase" "$format" | sed "s/^/ /"
else
plugn trigger storage-list "$APP" "$phase" "$format"
fi
}

View File

@@ -7,9 +7,18 @@ source "$PLUGIN_AVAILABLE_PATH/storage/functions"
trigger-storage-storage-list() {
declare desc="storage storage-list trigger"
declare trigger="storage-list"
declare APP="$1" PHASE="$2"
declare APP="$1" PHASE="$2" FORMAT="$3"
get_bind_mounts "$(fn-get-phase-file-path "$APP" "$PHASE")"
if [[ "$FORMAT" != "json" ]]; then
get_bind_mounts "$(fn-get-phase-file-path "$APP" "$PHASE")"
else
while read -r line; do
local host_path="$(awk -F: '{print $1}' <<< "$line")"
local container_path="$(awk -F: '{print $2}' <<< "$line")"
local volume_options="$(awk -F: '{print $3}' <<< "$line")"
jq -n --arg host_path "$host_path" --arg container_path "$container_path" --arg volume_options "$volume_options" '{host_path: $host_path, container_path: $container_path, volume_options: $volume_options}'
done < <(get_bind_mounts "$(fn-get-phase-file-path "$APP" "$PHASE")") | jq -M -n '[inputs]'
fi
}
trigger-storage-storage-list "$@"