From ffa303092fb1a0b3cc89a1fe31919e0609ac6061 Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Fri, 25 Dec 2020 00:00:26 -0500 Subject: [PATCH] feat: add app name verification to all golang plugins Moving verification of input to the earliest place is the best way to decrease the need to validate this further downstream. Additionally, some of the existing checks were.... lacking :) --- plugins/apps/subcommands.go | 16 -------------- plugins/buildpacks/subcommands.go | 20 ++++++++--------- plugins/logs/subcommands.go | 5 ----- plugins/network/subcommands.go | 4 ++++ plugins/proxy/subcommands.go | 36 ++++++++++++++----------------- plugins/ps/subcommands.go | 24 --------------------- plugins/repo/subcommands.go | 12 +++-------- plugins/resource/subcommands.go | 18 +++++++--------- 8 files changed, 41 insertions(+), 94 deletions(-) diff --git a/plugins/apps/subcommands.go b/plugins/apps/subcommands.go index fe23dce64..b07ee1a7c 100644 --- a/plugins/apps/subcommands.go +++ b/plugins/apps/subcommands.go @@ -79,10 +79,6 @@ func CommandDestroy(appName string, force bool) error { // CommandExists checks if an app exists func CommandExists(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - return appExists(appName) } @@ -104,10 +100,6 @@ func CommandList() error { // CommandLock locks an app for deployment func CommandLock(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -123,10 +115,6 @@ func CommandLock(appName string) error { // CommandLocked checks if an app is locked for deployment func CommandLocked(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -206,10 +194,6 @@ func CommandReport(appName string, infoFlag string) error { // CommandUnlock unlocks an app for deployment func CommandUnlock(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } diff --git a/plugins/buildpacks/subcommands.go b/plugins/buildpacks/subcommands.go index 539f01a62..a6caea1ba 100644 --- a/plugins/buildpacks/subcommands.go +++ b/plugins/buildpacks/subcommands.go @@ -9,8 +9,8 @@ import ( // CommandAdd implements buildpacks:add func CommandAdd(appName string, buildpack string, index int) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } buildpack, err := validBuildpackURL(buildpack) @@ -23,8 +23,8 @@ func CommandAdd(appName string, buildpack string, index int) error { // CommandClear implements buildpacks:clear func CommandClear(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } return common.PropertyDelete("buildpacks", appName, "buildpacks") @@ -32,8 +32,8 @@ func CommandClear(appName string) error { // CommandList implements buildpacks:list func CommandList(appName string) (err error) { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } buildpacks, err := common.PropertyListGet("buildpacks", appName, "buildpacks") @@ -50,8 +50,8 @@ func CommandList(appName string) (err error) { // CommandRemove implements buildpacks:remove func CommandRemove(appName string, buildpack string, index int) (err error) { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } if index != 0 && buildpack != "" { @@ -123,8 +123,8 @@ func CommandReport(appName string, infoFlag string) error { // CommandSet implements buildpacks:set func CommandSet(appName string, buildpack string, index int) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } buildpack, err := validBuildpackURL(buildpack) diff --git a/plugins/logs/subcommands.go b/plugins/logs/subcommands.go index bb7a77068..a1256151b 100644 --- a/plugins/logs/subcommands.go +++ b/plugins/logs/subcommands.go @@ -1,7 +1,6 @@ package logs import ( - "errors" "fmt" "strconv" @@ -10,10 +9,6 @@ import ( // CommandDefault displays recent log output func CommandDefault(appName string, num int64, process string, tail, quiet bool) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } diff --git a/plugins/network/subcommands.go b/plugins/network/subcommands.go index a4db4cc93..5cf6e4d16 100644 --- a/plugins/network/subcommands.go +++ b/plugins/network/subcommands.go @@ -142,6 +142,10 @@ func CommandReport(appName string, infoFlag string) error { // CommandSet set or clear a network property for an app func CommandSet(appName string, property string, value string) error { + if err := common.VerifyAppName(appName); err != nil { + return err + } + if property == "bind-all-interfaces" && value == "" { value = "false" } diff --git a/plugins/proxy/subcommands.go b/plugins/proxy/subcommands.go index dcb37425c..ed114a49f 100644 --- a/plugins/proxy/subcommands.go +++ b/plugins/proxy/subcommands.go @@ -10,10 +10,6 @@ import ( // CommandBuildConfig rebuilds config for a given app func CommandBuildConfig(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -23,8 +19,8 @@ func CommandBuildConfig(appName string) error { // CommandDisable disables the proxy for app via command line func CommandDisable(appName string, skipRestart bool) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } if !IsAppProxyEnabled(appName) { @@ -46,8 +42,8 @@ func CommandDisable(appName string, skipRestart bool) error { // CommandEnable enables the proxy for app via command line func CommandEnable(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } if IsAppProxyEnabled(appName) { @@ -66,8 +62,8 @@ func CommandEnable(appName string) error { // CommandPorts is a cmd wrapper to list proxy port mappings for an app func CommandPorts(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } return listAppProxyPorts(appName) @@ -75,8 +71,8 @@ func CommandPorts(appName string) error { // CommandPortsAdd adds proxy port mappings to an app func CommandPortsAdd(appName string, portMaps []string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } if len(portMaps) == 0 { @@ -97,8 +93,8 @@ func CommandPortsAdd(appName string, portMaps []string) error { // CommandPortsClear clears all proxy port mappings for an app func CommandPortsClear(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } keys := []string{"DOKKU_PROXY_PORT_MAP"} @@ -111,8 +107,8 @@ func CommandPortsClear(appName string) error { // CommandPortsRemove removes specific proxy port mappings from an app func CommandPortsRemove(appName string, portMaps []string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } if len(portMaps) == 0 { @@ -133,8 +129,8 @@ func CommandPortsRemove(appName string, portMaps []string) error { // CommandPortsSet sets proxy port mappings for an app func CommandPortsSet(appName string, portMaps []string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } if len(portMaps) == 0 { @@ -173,8 +169,8 @@ func CommandReport(appName string, infoFlag string) error { // CommandSet sets a proxy for an app func CommandSet(appName string, proxyType string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } if len(proxyType) < 2 { diff --git a/plugins/ps/subcommands.go b/plugins/ps/subcommands.go index 2cc6bd131..dec35936a 100644 --- a/plugins/ps/subcommands.go +++ b/plugins/ps/subcommands.go @@ -13,10 +13,6 @@ import ( // CommandInspect displays a sanitized version of docker inspect for an app func CommandInspect(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -31,10 +27,6 @@ func CommandRebuild(appName string, allApps bool, parallelCount int) error { return common.RunCommandAgainstAllApps(Rebuild, "rebuild", parallelCount) } - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -66,10 +58,6 @@ func CommandRestart(appName string, allApps bool, parallelCount int) error { return common.RunCommandAgainstAllApps(Restart, "restart", parallelCount) } - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -128,10 +116,6 @@ func CommandRetire() error { // CommandScale gets or sets how many instances of a given process to run func CommandScale(appName string, skipDeploy bool, processTuples []string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -189,10 +173,6 @@ func CommandStart(appName string, allApps bool, parallelCount int) error { return common.RunCommandAgainstAllApps(Start, "start", parallelCount) } - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } @@ -206,10 +186,6 @@ func CommandStop(appName string, allApps bool, parallelCount int) error { return common.RunCommandAgainstAllApps(Stop, "stop", parallelCount) } - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - if err := common.VerifyAppName(appName); err != nil { return err } diff --git a/plugins/repo/subcommands.go b/plugins/repo/subcommands.go index ab61e4102..3f72740ff 100644 --- a/plugins/repo/subcommands.go +++ b/plugins/repo/subcommands.go @@ -1,18 +1,12 @@ package repo import ( - "errors" - "github.com/dokku/dokku/plugins/common" ) // CommandGc runs 'git gc --aggressive' against the application's repo func CommandGc(appName string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") - } - err := common.VerifyAppName(appName) - if err != nil { + if err := common.VerifyAppName(appName); err != nil { return err } @@ -28,8 +22,8 @@ func CommandGc(appName string) error { // CommandPurgeCache deletes the contents of the build cache stored in the repository func CommandPurgeCache(appName string) error { - if appName == "" { - common.LogFail("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } return PurgeCache(appName) diff --git a/plugins/resource/subcommands.go b/plugins/resource/subcommands.go index 8dc67fc5c..0410eaa59 100644 --- a/plugins/resource/subcommands.go +++ b/plugins/resource/subcommands.go @@ -1,15 +1,13 @@ package resource import ( - "errors" - "github.com/dokku/dokku/plugins/common" ) // CommandLimit implements resource:limit func CommandLimit(appName string, processType string, r Resource) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } return setResourceType(appName, processType, r, "limit") @@ -17,8 +15,8 @@ func CommandLimit(appName string, processType string, r Resource) error { // CommandLimitClear implements resource:limit-clear func CommandLimitClear(appName string, processType string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } clearByResourceType(appName, processType, "limit") @@ -45,8 +43,8 @@ func CommandReport(appName string, infoFlag string) error { // CommandReserve implements resource:reserve func CommandReserve(appName string, processType string, r Resource) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } return setResourceType(appName, processType, r, "reserve") @@ -54,8 +52,8 @@ func CommandReserve(appName string, processType string, r Resource) error { // CommandReserveClear implements resource:reserve-clear func CommandReserveClear(appName string, processType string) error { - if appName == "" { - return errors.New("Please specify an app to run the command on") + if err := common.VerifyAppName(appName); err != nil { + return err } clearByResourceType(appName, processType, "reserve")