refactor: use LogFailWithError to allow a unified exit code for golang commands when the app is missing

This commit is contained in:
Jose Diaz-Gonzalez
2021-02-03 15:20:46 -05:00
parent d09a7506df
commit d55e34a55f
28 changed files with 52 additions and 29 deletions

View File

@@ -36,6 +36,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -82,6 +82,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -41,6 +41,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -76,6 +76,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -46,6 +46,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -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") 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 { type AppDoesNotExist struct {
appName string appName string
} }
func (err *AppDoesNotExist) ExitCode() int {
return 20
}
func (err *AppDoesNotExist) Error() string { func (err *AppDoesNotExist) Error() string {
return fmt.Sprintf("App %s does not exist", err.appName) return fmt.Sprintf("App %s does not exist", err.appName)
} }

View File

@@ -8,6 +8,11 @@ import (
"strings" "strings"
) )
// ErrWithExitCode wraps error and exposes an ExitCode method
type ErrWithExitCode interface {
ExitCode() int
}
// LogFail is the failure log formatter // LogFail is the failure log formatter
// prints text to stderr and exits with status 1 // prints text to stderr and exits with status 1
func LogFail(text string) { func LogFail(text string) {
@@ -15,6 +20,16 @@ func LogFail(text string) {
os.Exit(1) 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) // LogFailQuiet is the failure log formatter (with quiet option)
// prints text to stderr and exits with status 1 // prints text to stderr and exits with status 1
func LogFailQuiet(text string) { func LogFailQuiet(text string) {

View File

@@ -16,7 +16,7 @@ import (
func CommandPropertySet(pluginName, appName, property, value string, properties map[string]string, globalProperties map[string]bool) { func CommandPropertySet(pluginName, appName, property, value string, properties map[string]string, globalProperties map[string]bool) {
if appName != "--global" { if appName != "--global" {
if err := VerifyAppName(appName); err != nil { if err := VerifyAppName(appName); err != nil {
LogFail(err.Error()) LogFailWithError(err)
} }
} }
if appName == "--global" && !globalProperties[property] { if appName == "--global" && !globalProperties[property] {
@@ -42,9 +42,8 @@ func CommandPropertySet(pluginName, appName, property, value string, properties
PropertyWrite(pluginName, appName, property, value) PropertyWrite(pluginName, appName, property, value)
} else { } else {
LogInfo2Quiet(fmt.Sprintf("Unsetting %s", property)) LogInfo2Quiet(fmt.Sprintf("Unsetting %s", property))
err := PropertyDelete(pluginName, appName, property) if err := PropertyDelete(pluginName, appName, property); err != nil {
if err != nil { LogFailWithError(err)
LogFail(err.Error())
} }
} }
} }

View File

@@ -137,9 +137,11 @@ func getEnvironment(appName string, merged bool) (env *Env) {
} else { } else {
env, err = loadAppOrGlobalEnv(appName) env, err = loadAppOrGlobalEnv(appName)
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
return env return env
} }

View File

@@ -77,7 +77,7 @@ func LoadMergedAppEnv(appName string) (env *Env, err error) {
} }
global, err := LoadGlobalEnv() global, err := LoadGlobalEnv()
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
global.Merge(env) global.Merge(env)
global.filename = "" global.filename = ""

View File

@@ -48,7 +48,7 @@ func main() {
appName := args.Arg(0) appName := args.Arg(0)
err := config.CommandShow(appName, *global, *merged, *shell, *export) err := config.CommandShow(appName, *global, *merged, *shell, *export)
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
case "config:help": case "config:help":
usage() usage()

View File

@@ -109,6 +109,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -36,6 +36,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -37,6 +37,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -32,6 +32,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -54,7 +54,7 @@ func main() {
appName := args.Arg(0) appName := args.Arg(0)
err := logs.CommandDefault(appName, *num, *ps, *tail, *quiet) err := logs.CommandDefault(appName, *num, *ps, *tail, *quiet)
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
case "logs:help": case "logs:help":
usage() usage()

View File

@@ -66,6 +66,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -38,6 +38,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -72,6 +72,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -94,6 +94,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -87,6 +87,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -38,6 +38,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -90,6 +90,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -79,6 +79,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -33,6 +33,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -29,6 +29,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -92,6 +92,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }

View File

@@ -49,6 +49,6 @@ func main() {
} }
if err != nil { if err != nil {
common.LogFail(err.Error()) common.LogFailWithError(err)
} }
} }