From ab9f516c089fbaa4196d6bfce3f161124cd063ed Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Thu, 14 Mar 2019 12:37:33 -0400 Subject: [PATCH] refactor: clean up duplication in retrieving and verifying the APP argument --- plugins/resource/subcommands.go | 46 ++++++++++++++------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/plugins/resource/subcommands.go b/plugins/resource/subcommands.go index 444cc12b0..80631225f 100644 --- a/plugins/resource/subcommands.go +++ b/plugins/resource/subcommands.go @@ -8,16 +8,9 @@ import ( // CommandLimit implements resource:limit func CommandLimit(args []string, processType string, r Resource, global bool) (err error) { - appName := "_all_" - if !global { - appName, err = getAppName(args) - if err != nil { - return - } - - if err = common.VerifyAppName(appName); err != nil { - common.LogFail(err.Error()) - } + appName, err = getAppName(args, global) + if err != nil { + return err } return setRequestType(appName, processType, r, "limit") @@ -30,16 +23,9 @@ func CommandLimitClear(args []string, processType string, global bool) (err erro // CommandReserve implements resource:reserve func CommandReserve(args []string, processType string, r Resource, global bool) (err error) { - appName := "_all_" - if !global { - appName, err = getAppName(args) - if err != nil { - return - } - - if err = common.VerifyAppName(appName); err != nil { - common.LogFail(err.Error()) - } + appName, err = getAppName(args, global) + if err != nil { + return err } return setRequestType(appName, processType, r, "reserve") @@ -141,12 +127,20 @@ func propertyKey(processType string, requestType string, key string) string { return fmt.Sprintf("%v.%v.%v", processType, requestType, key) } -func getAppName(args []string) (appName string, err error) { - if len(args) >= 1 { - appName = args[0] - } else { - err = errors.New("Please specify an app to run the command on") +func getAppName(args []string, global bool) (appName string, err error) { + appName := "_all_" + if global { + return appName, nil } - return + if len(args) < 1 { + return "", errors.New("Please specify an app to run the command on") + } + + appName = args[0] + if err = common.VerifyAppName(appName); err != nil { + return "", err + } + + return appName, nil }