diff --git a/docs/deployment/methods/git.md b/docs/deployment/methods/git.md index b27371a07..5093f9272 100644 --- a/docs/deployment/methods/git.md +++ b/docs/deployment/methods/git.md @@ -13,7 +13,7 @@ git:load-image [--build-dir DIRECTORY] [ [] # Clone or fetch an app from remote git repo git:initialize # Initialize a git repository for an app git:public-key # Outputs the dokku public deploy key -git:report [] [] # Displays a git report for one or more apps +git:report [] [|--format json] # Displays a git report for one or more apps git:set () # Set or clear a git property for an app git:status # Show the working tree status for an app ``` @@ -241,3 +241,42 @@ dokku git:public-key ``` If there is no key, an error message is shown that displays the command that can be run on the Dokku server to generate a new public/private ssh key pair. + +### Displaying git reports for an app + +You can get a report about the app's git configuration using the `git:report` command: + +```shell +dokku git:report +``` + +``` +=====> node-js-app git information + Git deploy branch: master + Git global deploy branch: master + Git keep git dir: false + Git rev env var: GIT_REV + Git sha: a1b2c3d + Git source image: + Git last updated at: 1700000000 +``` + +You can run the command for a specific app also. + +```shell +dokku git:report node-js-app +``` + +You can pass flags which will output only the value of the specific information you want. For example: + +```shell +dokku git:report node-js-app --git-deploy-branch +``` + +The `git:report` command also takes a `--format` flag, with the valid options including `stdout` (default) and `json`. The `json` output format can be used for automation purposes: + +```shell +dokku git:report node-js-app --format json +``` + +The `--format` flag cannot be combined with an info flag. diff --git a/docs/networking/proxies/nginx.md b/docs/networking/proxies/nginx.md index 9e6c63d56..9dec59cfb 100644 --- a/docs/networking/proxies/nginx.md +++ b/docs/networking/proxies/nginx.md @@ -5,7 +5,7 @@ Dokku uses nginx as its server for routing requests to specific applications. By ``` nginx:access-logs [-t] # Show the nginx access logs for an application (-t follows) nginx:error-logs [-t] # Show the nginx error logs for an application (-t follows) -nginx:report [] [] # Displays a nginx report for one or more apps +nginx:report [] [|--format json] # Displays a nginx report for one or more apps nginx:set () # Set or clear an nginx property for an app nginx:show-config # Display app nginx config nginx:start # Starts the nginx server @@ -128,6 +128,34 @@ The `--clean` flag may also be specified for a given app: dokku nginx:validate-config node-js-app --clean ``` +### Displaying nginx reports for an app + +You can get a report about the app's nginx configuration using the `nginx:report` command: + +```shell +dokku nginx:report +``` + +You can run the command for a specific app also. + +```shell +dokku nginx:report node-js-app +``` + +You can pass flags which will output only the value of the specific information you want. For example: + +```shell +dokku nginx:report node-js-app --nginx-bind-address-ipv4 +``` + +The `nginx:report` command also takes a `--format` flag, with the valid options including `stdout` (default) and `json`. The `json` output format can be used for automation purposes: + +```shell +dokku nginx:report node-js-app --format json +``` + +The `--format` flag cannot be combined with an info flag. + ### Custom Error Pages By default, Dokku provides custom error pages for the following three categories of errors: diff --git a/plugins/git/internal-functions b/plugins/git/internal-functions index 18c2f651b..ec8ac2b4c 100755 --- a/plugins/git/internal-functions +++ b/plugins/git/internal-functions @@ -278,7 +278,23 @@ cmd-git-report() { declare desc="displays a git report for one or more apps" declare cmd="git:report" [[ "$1" == "$cmd" ]] && shift 1 - declare APP="$1" INFO_FLAG="$2" + local FORMAT="stdout" + local args=() + + while [[ $# -gt 0 ]]; do + case "$1" in + --format) + FORMAT="$2" + shift 2 + ;; + *) + args+=("$1") + shift + ;; + esac + done + + declare APP="${args[0]:-}" INFO_FLAG="${args[1]:-}" if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then INFO_FLAG="$APP" @@ -291,15 +307,15 @@ cmd-git-report() { if [[ -z "$APP" ]]; then for app in $(dokku_apps); do - cmd-git-report-single "$app" "$INFO_FLAG" | tee || true + cmd-git-report-single "$app" "$INFO_FLAG" "$FORMAT" | tee || true done else - cmd-git-report-single "$APP" "$INFO_FLAG" + cmd-git-report-single "$APP" "$INFO_FLAG" "$FORMAT" fi } cmd-git-report-single() { - declare APP="$1" INFO_FLAG="$2" + declare APP="$1" INFO_FLAG="$2" FORMAT="${3:-stdout}" local APP_ROOT="$DOKKU_ROOT/$APP" if [[ "$INFO_FLAG" == "true" ]]; then INFO_FLAG="" @@ -315,6 +331,22 @@ cmd-git-report-single() { "--git-last-updated-at: $(fn-git-last-updated-at "$APP")" ) + if [[ "$FORMAT" != "stdout" ]] && [[ -n "$INFO_FLAG" ]]; then + dokku_log_fail "--format flag cannot be specified when specifying an info flag" + fi + + if [[ "$FORMAT" == "json" ]]; then + local json_output="{}" + for flag in "${flag_map[@]}"; do + local key value + key="$(echo "$flag" | cut -d':' -f1 | sed 's/^--git-//')" + value="$(echo "$flag" | cut -d':' -f2- | sed 's/^ //')" + json_output="$(echo "$json_output" | jq -c --arg key "$key" --arg value "$value" '. + {($key): $value}')" + done + echo "$json_output" + return + fi + if [[ -z "$INFO_FLAG" ]]; then dokku_log_info2_quiet "${APP} git information" for flag in "${flag_map[@]}"; do diff --git a/plugins/nginx-vhosts/command-functions b/plugins/nginx-vhosts/command-functions index 84490ec8a..a2de9beaf 100755 --- a/plugins/nginx-vhosts/command-functions +++ b/plugins/nginx-vhosts/command-functions @@ -10,7 +10,23 @@ cmd-nginx-report() { declare desc="displays a nginx report for one or more apps" declare cmd="nginx:report" [[ "$1" == "$cmd" ]] && shift 1 - declare APP="$1" INFO_FLAG="$2" + local FORMAT="stdout" + local args=() + + while [[ $# -gt 0 ]]; do + case "$1" in + --format) + FORMAT="$2" + shift 2 + ;; + *) + args+=("$1") + shift + ;; + esac + done + + declare APP="${args[0]:-}" INFO_FLAG="${args[1]:-}" if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then INFO_FLAG="$APP" @@ -23,15 +39,15 @@ cmd-nginx-report() { if [[ -z "$APP" ]]; then for app in $(dokku_apps); do - cmd-nginx-report-single "$app" "$INFO_FLAG" | tee || true + cmd-nginx-report-single "$app" "$INFO_FLAG" "$FORMAT" | tee || true done else - cmd-nginx-report-single "$APP" "$INFO_FLAG" + cmd-nginx-report-single "$APP" "$INFO_FLAG" "$FORMAT" fi } cmd-nginx-report-single() { - declare APP="$1" INFO_FLAG="$2" + declare APP="$1" INFO_FLAG="$2" FORMAT="${3:-stdout}" if [[ "$INFO_FLAG" == "true" ]]; then INFO_FLAG="" fi @@ -130,6 +146,22 @@ cmd-nginx-report-single() { "--nginx-global-x-forwarded-ssl: $(fn-nginx-global-x-forwarded-ssl "$APP")" ) + if [[ "$FORMAT" != "stdout" ]] && [[ -n "$INFO_FLAG" ]]; then + dokku_log_fail "--format flag cannot be specified when specifying an info flag" + fi + + if [[ "$FORMAT" == "json" ]]; then + local json_output="{}" + for flag in "${flag_map[@]}"; do + local key value + key="$(echo "$flag" | cut -d':' -f1 | sed 's/^--nginx-//')" + value="$(echo "$flag" | cut -d':' -f2- | sed 's/^ //')" + json_output="$(echo "$json_output" | jq -c --arg key "$key" --arg value "$value" '. + {($key): $value}')" + done + echo "$json_output" + return + fi + if [[ -z "$INFO_FLAG" ]]; then dokku_log_info2_quiet "${APP} nginx information" for flag in "${flag_map[@]}"; do diff --git a/tests/unit/git_3.bats b/tests/unit/git_3.bats index 8e7119d41..28bb789b0 100644 --- a/tests/unit/git_3.bats +++ b/tests/unit/git_3.bats @@ -731,3 +731,38 @@ teardown() { assert_success assert_output "master" } + +@test "(git:report) --format json" { + run /bin/bash -c "dokku git:set $TEST_APP deploy-branch main" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:report $TEST_APP --format json | jq -r '.\"deploy-branch\"'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "main" + + run /bin/bash -c "dokku git:report $TEST_APP --format json | jq -r '.\"global-deploy-branch\"'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "master" + + run /bin/bash -c "dokku git:report $TEST_APP --format json | jq -e ." + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:report --format json | jq -e ." + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku git:report $TEST_APP --format json --git-deploy-branch" + echo "output: $output" + echo "status: $status" + assert_failure + assert_output_contains "--format flag cannot be specified when specifying an info flag" +} diff --git a/tests/unit/nginx-vhosts_1.bats b/tests/unit/nginx-vhosts_1.bats index 265a84202..cd038bef8 100644 --- a/tests/unit/nginx-vhosts_1.bats +++ b/tests/unit/nginx-vhosts_1.bats @@ -28,6 +28,40 @@ teardown() { assert_output "$help_output" } +@test "(nginx:report) --format json" { + run create_app + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:set $TEST_APP bind-address-ipv4 127.0.0.1" + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:report $TEST_APP --format json | jq -r '.\"bind-address-ipv4\"'" + echo "output: $output" + echo "status: $status" + assert_success + assert_output "127.0.0.1" + + run /bin/bash -c "dokku nginx:report $TEST_APP --format json | jq -e ." + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:report --format json | jq -e ." + echo "output: $output" + echo "status: $status" + assert_success + + run /bin/bash -c "dokku nginx:report $TEST_APP --format json --nginx-bind-address-ipv4" + echo "output: $output" + echo "status: $status" + assert_failure + assert_output_contains "--format flag cannot be specified when specifying an info flag" +} + @test "(nginx-vhosts) proxy:build-config (domains:disable/enable)" { run deploy_app echo "output: $output"