From c2034e16acc9540450937b98c01fe858ba8b842c Mon Sep 17 00:00:00 2001 From: Debarshi Ray Date: Sat, 30 Jul 2022 12:46:48 +0200 Subject: [PATCH] cmd: Refactor common code into a function --- src/cmd/create.go | 9 ++------- src/cmd/enter.go | 10 ++-------- src/cmd/run.go | 9 ++------- src/cmd/utils.go | 12 ++++++++++++ 4 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/cmd/create.go b/src/cmd/create.go index 24d589df2..f2a33e1b4 100644 --- a/src/cmd/create.go +++ b/src/cmd/create.go @@ -149,13 +149,8 @@ func create(cmd *cobra.Command, args []string) error { if container != "" { if !utils.IsContainerNameValid(container) { - var builder strings.Builder - fmt.Fprintf(&builder, "invalid argument for '%s'\n", containerArg) - fmt.Fprintf(&builder, "Container names must match '%s'\n", utils.ContainerNameRegexp) - fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase) - - errMsg := builder.String() - return errors.New(errMsg) + err := createErrorInvalidContainer(containerArg) + return err } } diff --git a/src/cmd/enter.go b/src/cmd/enter.go index c2bf416dd..1f0471779 100644 --- a/src/cmd/enter.go +++ b/src/cmd/enter.go @@ -20,7 +20,6 @@ import ( "errors" "fmt" "os" - "strings" "github.com/containers/toolbox/pkg/utils" "github.com/sirupsen/logrus" @@ -103,13 +102,8 @@ func enter(cmd *cobra.Command, args []string) error { defaultContainer = false if !utils.IsContainerNameValid(container) { - var builder strings.Builder - fmt.Fprintf(&builder, "invalid argument for '%s'\n", containerArg) - fmt.Fprintf(&builder, "Container names must match '%s'\n", utils.ContainerNameRegexp) - fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase) - - errMsg := builder.String() - return errors.New(errMsg) + err := createErrorInvalidContainer(containerArg) + return err } } diff --git a/src/cmd/run.go b/src/cmd/run.go index 764d9a8dd..106c867fe 100644 --- a/src/cmd/run.go +++ b/src/cmd/run.go @@ -102,13 +102,8 @@ func run(cmd *cobra.Command, args []string) error { defaultContainer = false if !utils.IsContainerNameValid(runFlags.container) { - var builder strings.Builder - fmt.Fprintf(&builder, "invalid argument for '--container'\n") - fmt.Fprintf(&builder, "Container names must match '%s'\n", utils.ContainerNameRegexp) - fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase) - - errMsg := builder.String() - return errors.New(errMsg) + err := createErrorInvalidContainer("--container") + return err } } diff --git a/src/cmd/utils.go b/src/cmd/utils.go index a201f8165..eff0c8546 100644 --- a/src/cmd/utils.go +++ b/src/cmd/utils.go @@ -23,6 +23,8 @@ import ( "os/exec" "strings" "syscall" + + "github.com/containers/toolbox/pkg/utils" ) // askForConfirmation prints prompt to stdout and waits for response from the @@ -69,6 +71,16 @@ func createErrorContainerNotFound(container string) error { return errors.New(errMsg) } +func createErrorInvalidContainer(containerArg string) error { + var builder strings.Builder + fmt.Fprintf(&builder, "invalid argument for '%s'\n", containerArg) + fmt.Fprintf(&builder, "Container names must match '%s'\n", utils.ContainerNameRegexp) + fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase) + + errMsg := builder.String() + return errors.New(errMsg) +} + func createErrorInvalidRelease() error { var builder strings.Builder fmt.Fprintf(&builder, "invalid argument for '--release'\n")