github.com/cspotcode/docker-cli@v20.10.0-rc1.0.20201201121459-3faad7acc5b8+incompatible/cli/command/context/cmd.go (about)

     1  package context
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"regexp"
     7  
     8  	"github.com/docker/cli/cli"
     9  	"github.com/docker/cli/cli/command"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // NewContextCommand returns the context cli subcommand
    14  func NewContextCommand(dockerCli command.Cli) *cobra.Command {
    15  	cmd := &cobra.Command{
    16  		Use:   "context",
    17  		Short: "Manage contexts",
    18  		Args:  cli.NoArgs,
    19  		RunE:  command.ShowHelp(dockerCli.Err()),
    20  	}
    21  	cmd.AddCommand(
    22  		newCreateCommand(dockerCli),
    23  		newListCommand(dockerCli),
    24  		newUseCommand(dockerCli),
    25  		newExportCommand(dockerCli),
    26  		newImportCommand(dockerCli),
    27  		newRemoveCommand(dockerCli),
    28  		newUpdateCommand(dockerCli),
    29  		newInspectCommand(dockerCli),
    30  	)
    31  	return cmd
    32  }
    33  
    34  const restrictedNamePattern = "^[a-zA-Z0-9][a-zA-Z0-9_.+-]+$"
    35  
    36  var restrictedNameRegEx = regexp.MustCompile(restrictedNamePattern)
    37  
    38  func validateContextName(name string) error {
    39  	if name == "" {
    40  		return errors.New("context name cannot be empty")
    41  	}
    42  	if name == "default" {
    43  		return errors.New(`"default" is a reserved context name`)
    44  	}
    45  	if !restrictedNameRegEx.MatchString(name) {
    46  		return fmt.Errorf("context name %q is invalid, names are validated against regexp %q", name, restrictedNamePattern)
    47  	}
    48  	return nil
    49  }