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

@@ -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)
}

View File

@@ -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) {

View File

@@ -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)
}
}
}