mirror of https://github.com/docker/cli.git
code optimization after review
Signed-off-by: Lifubang <lifubang@acmcoder.com>
This commit is contained in:
parent
8a4e508830
commit
518c3e61bc
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
24
cli/names.go
24
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
|
||||
}
|
||||
|
|
|
@ -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 + `+$`)
|
Loading…
Reference in New Issue