code optimization after review

Signed-off-by: Lifubang <lifubang@acmcoder.com>
This commit is contained in:
Lifubang 2018-09-03 20:02:48 +08:00
parent 8a4e508830
commit 518c3e61bc
5 changed files with 23 additions and 30 deletions

View File

@ -26,10 +26,8 @@ func NewKillCommand(dockerCli command.Cli) *cobra.Command {
Short: "Kill one or more running containers", Short: "Kill one or more running containers",
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
for _, name := range args { if err := cli.CheckContainerNames(args...); err != nil {
if !cli.CheckContainerName(name) { return err
return fmt.Errorf("container name %s is invalid", name)
}
} }
opts.containers = args opts.containers = args
return runKill(dockerCli, &opts) return runKill(dockerCli, &opts)

View File

@ -29,10 +29,8 @@ func NewRmCommand(dockerCli command.Cli) *cobra.Command {
Short: "Remove one or more containers", Short: "Remove one or more containers",
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
for _, name := range args { if err := cli.CheckContainerNames(args...); err != nil {
if !cli.CheckContainerName(name) { return err
return fmt.Errorf("container name %s is invalid", name)
}
} }
opts.containers = args opts.containers = args
return runRm(dockerCli, &opts) return runRm(dockerCli, &opts)

View File

@ -28,10 +28,8 @@ func NewStopCommand(dockerCli command.Cli) *cobra.Command {
Short: "Stop one or more running containers", Short: "Stop one or more running containers",
Args: cli.RequiresMinArgs(1), Args: cli.RequiresMinArgs(1),
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
for _, name := range args { if err := cli.CheckContainerNames(args...); err != nil {
if !cli.CheckContainerName(name) { return err
return fmt.Errorf("container name %s is invalid", name)
}
} }
opts.containers = args opts.containers = args
opts.timeChanged = cmd.Flags().Changed("time") opts.timeChanged = cmd.Flags().Changed("time")

View File

@ -1,19 +1,29 @@
package cli package cli
import ( import (
"fmt"
"regexp"
"strings" "strings"
"github.com/docker/cli/cli/names"
) )
var ( var (
validContainerNamePattern = names.RestrictedNamePattern validContainerNamePattern = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9_.-]+$")
) )
// CheckContainerName check container's name is valid or not // CheckContainerName check container's name is valid or not
func CheckContainerName(name string) bool { func CheckContainerName(name string) error {
if len(name) == 0 { if !validContainerNamePattern.MatchString(strings.TrimPrefix(name, "/")) {
return false 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
} }

View File

@ -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 + `+$`)