From d55e34a55f307c6fc0548b645e62b7a23aa7ddee Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 3 Feb 2021 15:20:46 -0500 Subject: [PATCH] refactor: use LogFailWithError to allow a unified exit code for golang commands when the app is missing --- plugins/app-json/src/triggers/triggers.go | 2 +- plugins/apps/src/subcommands/subcommands.go | 2 +- plugins/apps/src/triggers/triggers.go | 2 +- plugins/buildpacks/src/subcommands/subcommands.go | 2 +- plugins/buildpacks/src/triggers/triggers.go | 2 +- plugins/common/common.go | 7 +++++++ plugins/common/log.go | 15 +++++++++++++++ plugins/common/properties.go | 7 +++---- plugins/config/config.go | 4 +++- plugins/config/environment.go | 2 +- plugins/config/src/commands/commands.go | 2 +- plugins/config/src/subcommands/subcommands.go | 2 +- plugins/config/src/triggers/triggers.go | 2 +- plugins/cron/src/subcommands/subcommands.go | 2 +- plugins/cron/src/triggers/triggers.go | 2 +- plugins/logs/src/commands/commands.go | 2 +- plugins/logs/src/subcommands/subcommands.go | 2 +- plugins/logs/src/triggers/triggers.go | 2 +- plugins/network/src/subcommands/subcommands.go | 2 +- plugins/network/src/triggers/triggers.go | 2 +- plugins/proxy/src/subcommands/subcommands.go | 2 +- plugins/proxy/src/triggers/triggers.go | 2 +- plugins/ps/src/subcommands/subcommands.go | 2 +- plugins/ps/src/triggers/triggers.go | 2 +- plugins/repo/src/subcommands/subcommands.go | 2 +- plugins/repo/src/triggers/triggers.go | 2 +- plugins/resource/src/subcommands/subcommands.go | 2 +- plugins/resource/src/triggers/triggers.go | 2 +- 28 files changed, 52 insertions(+), 29 deletions(-) diff --git a/plugins/app-json/src/triggers/triggers.go b/plugins/app-json/src/triggers/triggers.go index c76f4c728..7b1a67b1e 100644 --- a/plugins/app-json/src/triggers/triggers.go +++ b/plugins/app-json/src/triggers/triggers.go @@ -36,6 +36,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/apps/src/subcommands/subcommands.go b/plugins/apps/src/subcommands/subcommands.go index e8ba5eb84..0874f7084 100644 --- a/plugins/apps/src/subcommands/subcommands.go +++ b/plugins/apps/src/subcommands/subcommands.go @@ -82,6 +82,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/apps/src/triggers/triggers.go b/plugins/apps/src/triggers/triggers.go index f0e6de8ea..0fc2dbebe 100644 --- a/plugins/apps/src/triggers/triggers.go +++ b/plugins/apps/src/triggers/triggers.go @@ -41,6 +41,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/buildpacks/src/subcommands/subcommands.go b/plugins/buildpacks/src/subcommands/subcommands.go index 94640f925..f245238f7 100644 --- a/plugins/buildpacks/src/subcommands/subcommands.go +++ b/plugins/buildpacks/src/subcommands/subcommands.go @@ -76,6 +76,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/buildpacks/src/triggers/triggers.go b/plugins/buildpacks/src/triggers/triggers.go index d9ff5d96b..cf33fd006 100644 --- a/plugins/buildpacks/src/triggers/triggers.go +++ b/plugins/buildpacks/src/triggers/triggers.go @@ -46,6 +46,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/common/common.go b/plugins/common/common.go index f390d6d8f..8bb572afa 100644 --- a/plugins/common/common.go +++ b/plugins/common/common.go @@ -515,10 +515,17 @@ func isValidAppNameOld(appName string) error { return errors.New("App name must begin with lowercase alphanumeric character, and cannot include uppercase characters, or colons") } +// AppDoesNotExist wraps error to include the app name +// and is used to distinguish between a normal error and an error +// where the app is missing type AppDoesNotExist struct { appName string } +func (err *AppDoesNotExist) ExitCode() int { + return 20 +} + func (err *AppDoesNotExist) Error() string { return fmt.Sprintf("App %s does not exist", err.appName) } diff --git a/plugins/common/log.go b/plugins/common/log.go index 6b9e053e2..7cfc87b62 100644 --- a/plugins/common/log.go +++ b/plugins/common/log.go @@ -8,6 +8,11 @@ import ( "strings" ) +// ErrWithExitCode wraps error and exposes an ExitCode method +type ErrWithExitCode interface { + ExitCode() int +} + // LogFail is the failure log formatter // prints text to stderr and exits with status 1 func LogFail(text string) { @@ -15,6 +20,16 @@ func LogFail(text string) { os.Exit(1) } +// LogFailWithError is the failure log formatter +// prints text to stderr and exits with the specified exit code +func LogFailWithError(err error) { + fmt.Fprintln(os.Stderr, fmt.Sprintf(" ! %s", err.Error())) + if errExit, ok := err.(ErrWithExitCode); ok { + os.Exit(errExit.ExitCode()) + } + os.Exit(1) +} + // LogFailQuiet is the failure log formatter (with quiet option) // prints text to stderr and exits with status 1 func LogFailQuiet(text string) { diff --git a/plugins/common/properties.go b/plugins/common/properties.go index 0fc20a38a..9b9f9fec7 100644 --- a/plugins/common/properties.go +++ b/plugins/common/properties.go @@ -16,7 +16,7 @@ import ( func CommandPropertySet(pluginName, appName, property, value string, properties map[string]string, globalProperties map[string]bool) { if appName != "--global" { if err := VerifyAppName(appName); err != nil { - LogFail(err.Error()) + LogFailWithError(err) } } if appName == "--global" && !globalProperties[property] { @@ -42,9 +42,8 @@ func CommandPropertySet(pluginName, appName, property, value string, properties PropertyWrite(pluginName, appName, property, value) } else { LogInfo2Quiet(fmt.Sprintf("Unsetting %s", property)) - err := PropertyDelete(pluginName, appName, property) - if err != nil { - LogFail(err.Error()) + if err := PropertyDelete(pluginName, appName, property); err != nil { + LogFailWithError(err) } } } diff --git a/plugins/config/config.go b/plugins/config/config.go index 71feb4788..45fb9fb20 100644 --- a/plugins/config/config.go +++ b/plugins/config/config.go @@ -137,9 +137,11 @@ func getEnvironment(appName string, merged bool) (env *Env) { } else { env, err = loadAppOrGlobalEnv(appName) } + if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } + return env } diff --git a/plugins/config/environment.go b/plugins/config/environment.go index 2b4602f0b..0798c3200 100644 --- a/plugins/config/environment.go +++ b/plugins/config/environment.go @@ -77,7 +77,7 @@ func LoadMergedAppEnv(appName string) (env *Env, err error) { } global, err := LoadGlobalEnv() if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } global.Merge(env) global.filename = "" diff --git a/plugins/config/src/commands/commands.go b/plugins/config/src/commands/commands.go index fef47579c..df63fabaf 100644 --- a/plugins/config/src/commands/commands.go +++ b/plugins/config/src/commands/commands.go @@ -48,7 +48,7 @@ func main() { appName := args.Arg(0) err := config.CommandShow(appName, *global, *merged, *shell, *export) if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } case "config:help": usage() diff --git a/plugins/config/src/subcommands/subcommands.go b/plugins/config/src/subcommands/subcommands.go index 5598ed86a..f7f1f0d09 100644 --- a/plugins/config/src/subcommands/subcommands.go +++ b/plugins/config/src/subcommands/subcommands.go @@ -109,6 +109,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/config/src/triggers/triggers.go b/plugins/config/src/triggers/triggers.go index 936cb30fb..814c2e83f 100644 --- a/plugins/config/src/triggers/triggers.go +++ b/plugins/config/src/triggers/triggers.go @@ -36,6 +36,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/cron/src/subcommands/subcommands.go b/plugins/cron/src/subcommands/subcommands.go index eb384b81e..12daa862f 100644 --- a/plugins/cron/src/subcommands/subcommands.go +++ b/plugins/cron/src/subcommands/subcommands.go @@ -37,6 +37,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/cron/src/triggers/triggers.go b/plugins/cron/src/triggers/triggers.go index 4733cba55..f48bdc58d 100644 --- a/plugins/cron/src/triggers/triggers.go +++ b/plugins/cron/src/triggers/triggers.go @@ -32,6 +32,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/logs/src/commands/commands.go b/plugins/logs/src/commands/commands.go index c4fcc3a93..42d348e07 100644 --- a/plugins/logs/src/commands/commands.go +++ b/plugins/logs/src/commands/commands.go @@ -54,7 +54,7 @@ func main() { appName := args.Arg(0) err := logs.CommandDefault(appName, *num, *ps, *tail, *quiet) if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } case "logs:help": usage() diff --git a/plugins/logs/src/subcommands/subcommands.go b/plugins/logs/src/subcommands/subcommands.go index dcc4f1704..0ed04c277 100644 --- a/plugins/logs/src/subcommands/subcommands.go +++ b/plugins/logs/src/subcommands/subcommands.go @@ -66,6 +66,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/logs/src/triggers/triggers.go b/plugins/logs/src/triggers/triggers.go index cba2d1d9a..a4d35dc01 100644 --- a/plugins/logs/src/triggers/triggers.go +++ b/plugins/logs/src/triggers/triggers.go @@ -38,6 +38,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/network/src/subcommands/subcommands.go b/plugins/network/src/subcommands/subcommands.go index a79a54a79..8208299fd 100644 --- a/plugins/network/src/subcommands/subcommands.go +++ b/plugins/network/src/subcommands/subcommands.go @@ -72,6 +72,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/network/src/triggers/triggers.go b/plugins/network/src/triggers/triggers.go index 7ba918bf2..0ce27d43a 100644 --- a/plugins/network/src/triggers/triggers.go +++ b/plugins/network/src/triggers/triggers.go @@ -94,6 +94,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/proxy/src/subcommands/subcommands.go b/plugins/proxy/src/subcommands/subcommands.go index 9bba1b38c..d0a8b3960 100644 --- a/plugins/proxy/src/subcommands/subcommands.go +++ b/plugins/proxy/src/subcommands/subcommands.go @@ -87,6 +87,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/proxy/src/triggers/triggers.go b/plugins/proxy/src/triggers/triggers.go index 956fcf91d..11c5bd277 100644 --- a/plugins/proxy/src/triggers/triggers.go +++ b/plugins/proxy/src/triggers/triggers.go @@ -38,6 +38,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/ps/src/subcommands/subcommands.go b/plugins/ps/src/subcommands/subcommands.go index e1d8b7378..b77b0d7ba 100644 --- a/plugins/ps/src/subcommands/subcommands.go +++ b/plugins/ps/src/subcommands/subcommands.go @@ -90,6 +90,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/ps/src/triggers/triggers.go b/plugins/ps/src/triggers/triggers.go index 3b51e1214..d60afa0c4 100644 --- a/plugins/ps/src/triggers/triggers.go +++ b/plugins/ps/src/triggers/triggers.go @@ -79,6 +79,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/repo/src/subcommands/subcommands.go b/plugins/repo/src/subcommands/subcommands.go index c173860f7..55925129d 100644 --- a/plugins/repo/src/subcommands/subcommands.go +++ b/plugins/repo/src/subcommands/subcommands.go @@ -33,6 +33,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/repo/src/triggers/triggers.go b/plugins/repo/src/triggers/triggers.go index 1abc176d4..0dd1c21d0 100644 --- a/plugins/repo/src/triggers/triggers.go +++ b/plugins/repo/src/triggers/triggers.go @@ -29,6 +29,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/resource/src/subcommands/subcommands.go b/plugins/resource/src/subcommands/subcommands.go index e8f61d2fc..c54697ba6 100644 --- a/plugins/resource/src/subcommands/subcommands.go +++ b/plugins/resource/src/subcommands/subcommands.go @@ -92,6 +92,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } } diff --git a/plugins/resource/src/triggers/triggers.go b/plugins/resource/src/triggers/triggers.go index a0d461fa0..01e6a9cfe 100644 --- a/plugins/resource/src/triggers/triggers.go +++ b/plugins/resource/src/triggers/triggers.go @@ -49,6 +49,6 @@ func main() { } if err != nil { - common.LogFail(err.Error()) + common.LogFailWithError(err) } }