diff --git a/src/cmd/create.go b/src/cmd/create.go index b4e923651..41560cebe 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 b7b73cb53..45b64f8ed 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 d65143a6a..91b21c49e 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 ea332b366..0b4b94994 100644 --- a/src/cmd/utils.go +++ b/src/cmd/utils.go @@ -71,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 createErrorInvalidDistro() error { var builder strings.Builder fmt.Fprintf(&builder, "invalid argument for '--distro'\n")