Skip to content

Commit

Permalink
cmd: Refactor common code into a function
Browse files Browse the repository at this point in the history
  • Loading branch information
debarshiray committed Aug 29, 2022
1 parent a9a5b96 commit 4867629
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
9 changes: 2 additions & 7 deletions src/cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/cmd/enter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/containers/toolbox/pkg/utils"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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
}
}

Expand Down
9 changes: 2 additions & 7 deletions src/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 4867629

Please sign in to comment.