diff --git a/plugins/app-json/subcommands.go b/plugins/app-json/subcommands.go index b2f396281..c7490db5e 100644 --- a/plugins/app-json/subcommands.go +++ b/plugins/app-json/subcommands.go @@ -1,6 +1,8 @@ package appjson import ( + "errors" + "github.com/dokku/dokku/plugins/common" ) @@ -9,6 +11,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/apps/subcommands.go b/plugins/apps/subcommands.go index 56e6f7cc4..40291c2c8 100644 --- a/plugins/apps/subcommands.go +++ b/plugins/apps/subcommands.go @@ -209,6 +209,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/builder/subcommands.go b/plugins/builder/subcommands.go index 9278f8493..ad6e1081a 100644 --- a/plugins/builder/subcommands.go +++ b/plugins/builder/subcommands.go @@ -1,6 +1,8 @@ package builder import ( + "errors" + "github.com/dokku/dokku/plugins/common" ) @@ -9,6 +11,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/buildpacks/subcommands.go b/plugins/buildpacks/subcommands.go index 24d3125ef..7a2a13d08 100644 --- a/plugins/buildpacks/subcommands.go +++ b/plugins/buildpacks/subcommands.go @@ -108,6 +108,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/common/common.go b/plugins/common/common.go index 29b94b308..80aed6746 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -336,7 +336,7 @@ func UnfilteredDokkuApps() ([]string, error) { dokkuRoot := MustGetEnv("DOKKU_ROOT") files, err := os.ReadDir(dokkuRoot) if err != nil { - return apps, fmt.Errorf("You haven't deployed any applications yet") + return apps, NoAppsExist } for _, f := range files { @@ -351,7 +351,7 @@ func UnfilteredDokkuApps() ([]string, error) { } if len(apps) == 0 { - return apps, fmt.Errorf("You haven't deployed any applications yet") + return apps, NoAppsExist } return apps, nil @@ -653,6 +653,11 @@ func (err *AppDoesNotExist) Error() string { return fmt.Sprintf("App %s does not exist", err.appName) } +// NoAppsExist wraps error to include the app name +// and is used to distinguish between a normal error and an error +// where the app is missing +var NoAppsExist = errors.New("You haven't deployed any applications yet") + // VarArgs skips a number of incoming arguments, returning what is left over func VarArgs(arguments []string, skip int) []string { if len(arguments) <= skip { diff --git a/plugins/common/functions.go b/plugins/common/functions.go index 0ae83bb9e..7d05eb513 100644 --- a/plugins/common/functions.go +++ b/plugins/common/functions.go @@ -1,7 +1,6 @@ package common import ( - "fmt" "os" "strings" ) @@ -33,7 +32,7 @@ func filterApps(apps []string) ([]string, error) { filteredApps = removeEmptyEntries(filteredApps) if len(filteredApps) == 0 { - return filteredApps, fmt.Errorf("You haven't deployed any applications yet") + return filteredApps, NoAppsExist } return filteredApps, nil diff --git a/plugins/cron/subcommands.go b/plugins/cron/subcommands.go index 981f1deab..593f6212a 100644 --- a/plugins/cron/subcommands.go +++ b/plugins/cron/subcommands.go @@ -2,6 +2,7 @@ package cron import ( "encoding/json" + "errors" "fmt" "os" @@ -55,6 +56,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/logs/subcommands.go b/plugins/logs/subcommands.go index 9ea758b32..357af9eec 100644 --- a/plugins/logs/subcommands.go +++ b/plugins/logs/subcommands.go @@ -52,6 +52,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/network/subcommands.go b/plugins/network/subcommands.go index 2dc907055..891a4e461 100644 --- a/plugins/network/subcommands.go +++ b/plugins/network/subcommands.go @@ -163,6 +163,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/ports/subcommands.go b/plugins/ports/subcommands.go index 6f4f3793f..5046f0552 100644 --- a/plugins/ports/subcommands.go +++ b/plugins/ports/subcommands.go @@ -135,6 +135,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/proxy/subcommands.go b/plugins/proxy/subcommands.go index 7a92920e8..96a90535b 100644 --- a/plugins/proxy/subcommands.go +++ b/plugins/proxy/subcommands.go @@ -65,6 +65,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/ps/subcommands.go b/plugins/ps/subcommands.go index 2d5302403..6c5882e5e 100644 --- a/plugins/ps/subcommands.go +++ b/plugins/ps/subcommands.go @@ -44,6 +44,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/registry/subcommands.go b/plugins/registry/subcommands.go index bce13ffe8..eb7e68757 100644 --- a/plugins/registry/subcommands.go +++ b/plugins/registry/subcommands.go @@ -72,6 +72,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/resource/subcommands.go b/plugins/resource/subcommands.go index f13217389..409d1a75e 100644 --- a/plugins/resource/subcommands.go +++ b/plugins/resource/subcommands.go @@ -1,6 +1,8 @@ package resource import ( + "errors" + "github.com/dokku/dokku/plugins/common" ) @@ -28,6 +30,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/scheduler-k3s/subcommands.go b/plugins/scheduler-k3s/subcommands.go index 4eb30f854..aa7dc51e9 100644 --- a/plugins/scheduler-k3s/subcommands.go +++ b/plugins/scheduler-k3s/subcommands.go @@ -4,6 +4,7 @@ import ( "context" "crypto/rand" "encoding/json" + "errors" "fmt" "net/url" "os" @@ -940,6 +941,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/plugins/scheduler/subcommands.go b/plugins/scheduler/subcommands.go index 98ed46e44..67caf8f9f 100644 --- a/plugins/scheduler/subcommands.go +++ b/plugins/scheduler/subcommands.go @@ -1,6 +1,8 @@ package scheduler import ( + "errors" + "github.com/dokku/dokku/plugins/common" ) @@ -9,6 +11,10 @@ func CommandReport(appName string, format string, infoFlag string) error { if len(appName) == 0 { apps, err := common.DokkuApps() if err != nil { + if errors.Is(err, common.NoAppsExist) { + common.LogWarn(err.Error()) + return nil + } return err } for _, appName := range apps { diff --git a/tests/unit/report.bats b/tests/unit/report.bats index fc280fae8..a5f7271b6 100644 --- a/tests/unit/report.bats +++ b/tests/unit/report.bats @@ -4,17 +4,18 @@ load test_helper setup() { global_setup - deploy_app rm -f /tmp/fake-docker-bin } teardown() { rm -f /tmp/fake-docker-bin - destroy_app + destroy_app || true global_teardown } @test "(report) report" { + deploy_app + run /bin/bash -c "dokku report" echo "output: $output" echo "status: $status" @@ -57,6 +58,8 @@ teardown() { } @test "(report) custom docker bin" { + deploy_app + export DOCKER_BIN="docker" run /bin/bash -c "dokku report" echo "output: $output" @@ -85,3 +88,52 @@ teardown() { unset DOCKER_BIN } + +@test "(report) exit 0 when no apps exist" { + run /bin/bash -c "dokku report" + echo "output: $output" + echo "status: $status" + assert_success + + plugins=( + app-json + apps + builder + builder-dockerfile + builder-herokuish + builder-lambda + builder-nixpacks + builder-pack + buildpacks + caddy + certs + checks + cron + docker-options + domains + git + haproxy + logs + network + nginx + openresty + ports + proxy + ps + registry + resource + scheduler + scheduler-docker-local + scheduler-k3s + storage + traefik + ) + + for plugin in "${plugins[@]}"; do + run /bin/bash -c "dokku $plugin:report 2>&1" + echo "output: $output" + echo "status: $status" + assert_success + assert_output_contains "You haven't deployed any applications yet" + done +}