diff --git a/cli/command/container/kill.go b/cli/command/container/kill.go index e83a580bd7..f1e8d8820e 100644 --- a/cli/command/container/kill.go +++ b/cli/command/container/kill.go @@ -26,10 +26,8 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command { Short: "Kill one or more running containers", Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - for _, name := range args { - if !cli.CheckContainerName(name) { - return fmt.Errorf("container name %s is invalid", name) - } + if err := cli.CheckContainerNames(args...); err != nil { + return err } opts.containers = args return runKill(dockerCli, &opts) diff --git a/cli/command/container/rm.go b/cli/command/container/rm.go index cf0be9c51b..3e738dbcf9 100644 --- a/cli/command/container/rm.go +++ b/cli/command/container/rm.go @@ -29,10 +29,8 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command { Short: "Remove one or more containers", Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - for _, name := range args { - if !cli.CheckContainerName(name) { - return fmt.Errorf("container name %s is invalid", name) - } + if err := cli.CheckContainerNames(args...); err != nil { + return err } opts.containers = args return runRm(dockerCli, &opts) diff --git a/cli/command/container/stop.go b/cli/command/container/stop.go index 13ddcfd5bc..7fd4f11dab 100644 --- a/cli/command/container/stop.go +++ b/cli/command/container/stop.go @@ -28,10 +28,8 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command { Short: "Stop one or more running containers", Args: cli.RequiresMinArgs(1), RunE: func(cmd *cobra.Command, args []string) error { - for _, name := range args { - if !cli.CheckContainerName(name) { - return fmt.Errorf("container name %s is invalid", name) - } + if err := cli.CheckContainerNames(args...); err != nil { + return err } opts.containers = args opts.timeChanged = cmd.Flags().Changed("time") diff --git a/cli/names.go b/cli/names.go index 4b7c687125..aca492093c 100644 --- a/cli/names.go +++ b/cli/names.go @@ -1,19 +1,29 @@ package cli import ( + "fmt" + "regexp" "strings" - - "github.com/docker/cli/cli/names" ) var ( - validContainerNamePattern = names.RestrictedNamePattern + validContainerNamePattern = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9_.-]+$") ) // CheckContainerName check container's name is valid or not -func CheckContainerName(name string) bool { - if len(name) == 0 { - return false +func CheckContainerName(name string) error { + if !validContainerNamePattern.MatchString(strings.TrimPrefix(name, "/")) { + return fmt.Errorf("container name %s is invalid", name) } - return validContainerNamePattern.MatchString(strings.TrimPrefix(name, "/")) + return nil +} + +// CheckContainerNames check containers' name is valid or not +func CheckContainerNames(names ...string) error { + for _, name := range names { + if err := CheckContainerName(name); err != nil { + return err + } + } + return nil } diff --git a/cli/names/names.go b/cli/names/names.go deleted file mode 100644 index 1d77e3ab14..0000000000 --- a/cli/names/names.go +++ /dev/null @@ -1,11 +0,0 @@ -package names - -import ( - "regexp" -) - -// RestrictedNameChars collects the characters allowed to represent a name, normally used to validate container and volume names. -const RestrictedNameChars = `[a-zA-Z0-9][a-zA-Z0-9_.-]` - -// RestrictedNamePattern is a regular expression to validate names against the collection of restricted characters. -var RestrictedNamePattern = regexp.MustCompile(`^` + RestrictedNameChars + `+$`)