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",
|
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)
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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")
|
||||||
|
|
24
cli/names.go
24
cli/names.go
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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