Skip to content

Commit

Permalink
add support for config-format flag
Browse files Browse the repository at this point in the history
  • Loading branch information
juanvallejo committed Oct 13, 2017
1 parent 6a82320 commit 338a5d0
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions pkg/oc/cli/secrets/dockercfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type CreateDockerConfigOptions struct {
Password string
EmailAddress string

DockerCfgFormat string
SecretsInterface kcoreclient.SecretInterface

Out io.Writer
Expand All @@ -70,7 +71,7 @@ func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, o
Long: createDockercfgLong,
Example: fmt.Sprintf(createDockercfgExample, fullName, newSecretFullName, ocEditFullName),
Run: func(c *cobra.Command, args []string) {
if err := o.Complete(f, args); err != nil {
if err := o.Complete(f, c, args); err != nil {
kcmdutil.CheckErr(kcmdutil.UsageError(c, err.Error()))
}

Expand Down Expand Up @@ -98,6 +99,7 @@ func NewCmdCreateDockerConfigSecret(name, fullName string, f kcmdutil.Factory, o
cmd.Flags().StringVar(&o.Password, "docker-password", "", "Password for Docker registry authentication")
cmd.Flags().StringVar(&o.EmailAddress, "docker-email", "", "Email for Docker registry")
cmd.Flags().StringVar(&o.RegistryLocation, "docker-server", "https://index.docker.io/v1/", "Server location for Docker registry")
cmd.Flags().StringVar(&o.DockerCfgFormat, "config-format", ".dockercfg", "Docker authentication file format to use. Can be one of \"config.json\" or \".dockercfg\". Defaults to \".dockercfg\" (legacy format).")
kcmdutil.AddPrinterFlags(cmd)

return cmd
Expand Down Expand Up @@ -125,11 +127,27 @@ func (o CreateDockerConfigOptions) NewDockerSecret() (*api.Secret, error) {
Email: o.EmailAddress,
}

dockerCfg := credentialprovider.DockerConfigJson{
Auths: map[string]credentialprovider.DockerConfigEntry{o.RegistryLocation: dockercfgAuth},
var dockercfgContent []byte
var err error

dockerCfg := map[string]credentialprovider.DockerConfigEntry{o.RegistryLocation: dockercfgAuth}

if len(o.DockerCfgFormat) > 0 {
switch o.DockerCfgFormat {
case credentialprovider.DockerCfgFormatLegacy:
dockercfgContent, err = json.Marshal(dockerCfg)
case credentialprovider.DockerCfgFormatJson:
dockerCfgJson := credentialprovider.DockerConfigJson{
Auths: map[string]credentialprovider.DockerConfigEntry{o.RegistryLocation: dockercfgAuth},
}
dockercfgContent, err = json.Marshal(dockerCfgJson)
default:
return nil, fmt.Errorf("the docker config format specified (%s) is not supported.", o.DockerCfgFormat)
}
} else {
dockercfgContent, err = json.Marshal(dockerCfg)
}

dockercfgContent, err := json.Marshal(dockerCfg)
if err != nil {
return nil, err
}
Expand All @@ -143,11 +161,12 @@ func (o CreateDockerConfigOptions) NewDockerSecret() (*api.Secret, error) {
return secret, nil
}

func (o *CreateDockerConfigOptions) Complete(f kcmdutil.Factory, args []string) error {
func (o *CreateDockerConfigOptions) Complete(f kcmdutil.Factory, cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("must have exactly one argument: secret name")
}
o.SecretName = args[0]
o.DockerCfgFormat = kcmdutil.GetFlagString(cmd, "config-format")

client, err := f.ClientSet()
if err != nil {
Expand Down

0 comments on commit 338a5d0

Please sign in to comment.