Skip to content

Commit

Permalink
Introduce set subcommand + harmonize flags descriptions
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Ferquel <simon.ferquel@docker.com>
  • Loading branch information
simonferquel committed Dec 17, 2018
1 parent b4a5e8c commit 3662466
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 65 deletions.
7 changes: 2 additions & 5 deletions cli/command/context/cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package context

import (
"github.com/spf13/cobra"

"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)

// NewContextCommand returns the context cli subcommand
Expand All @@ -22,9 +21,7 @@ func NewContextCommand(dockerCli command.Cli) *cobra.Command {
newExportCommand(dockerCli),
newImportCommand(dockerCli),
newRemoveCommand(dockerCli),
newSetDockerEndpointCommand(dockerCli),
newSetKubernetesEndpointCommand(dockerCli),
newSetOptionsCommand(dockerCli),
newSetCommand(dockerCli),
)
return cmd
}
23 changes: 9 additions & 14 deletions cli/command/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/docker/cli/cli/context/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func newCreateCommand(dockerCli command.Cli) *cobra.Command {
Expand All @@ -22,8 +21,15 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command {
return opts.process(dockerCli, dockerCli.ContextStore())
},
}

opts.addFlags(cmd.Flags())
flags := cmd.Flags()
flags.StringVar(&opts.description, "description", "", "Description of the context")
flags.StringVar(
&opts.defaultStackOrchestrator,
"default-stack-orchestrator", "",
"Default orchestrator for stack operations to use with this context (swarm|kubernetes|all)")
flags.BoolVar(&opts.use, "use", false, "Use this context by default")
opts.docker.addFlags(flags, "docker-")
opts.kubernetes.addFlags(flags, "kubernetes-")
return cmd
}

Expand All @@ -36,17 +42,6 @@ type createOptions struct {
use bool
}

func (o *createOptions) addFlags(flags *pflag.FlagSet) {
flags.StringVar(&o.description, "description", "", "set the description of the context")
flags.StringVar(
&o.defaultStackOrchestrator,
"default-stack-orchestrator", "",
"set the default orchestrator for stack operations if different to the default one, to use with this context (swarm|kubernetes|all)")
flags.BoolVar(&o.use, "use", false, "use this context by default")
o.docker.addFlags(flags, "docker-")
o.kubernetes.addFlags(flags, "kubernetes-")
}

func (o *createOptions) process(cli command.Cli, s store.Store) error {
if _, err := s.GetContextMetadata(o.name); !store.IsErrContextDoesNotExist(err) {
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/context/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func newExportCommand(dockerCli command.Cli) *cobra.Command {
}

flags := cmd.Flags()
flags.BoolVarP(&opts.kubeconfig, "kubeconfig", "k", false, "export as a kubeconfig file")
flags.BoolVarP(&opts.kubeconfig, "kubeconfig", "k", false, "Export as a kubeconfig file")
return cmd
}

Expand Down
6 changes: 3 additions & 3 deletions cli/command/context/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func newImportCommand(dockerCli command.Cli) *cobra.Command {
}

flags := cmd.Flags()
flags.BoolVar(&opts.use, "use", false, "make this context the default one")
flags.BoolVar(&opts.force, "force", false, "overwrite any existing context with the same name")
flags.StringVar(&opts.name, "name", "", "name of the context")
flags.BoolVar(&opts.use, "use", false, "Use this context by default")
flags.BoolVar(&opts.force, "force", false, "Overwrite any existing context with the same name")
flags.StringVar(&opts.name, "name", "", "Name of the context")
return cmd
}

Expand Down
23 changes: 12 additions & 11 deletions cli/command/context/options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package context

import (
"fmt"
"os"
"path/filepath"

Expand All @@ -27,37 +28,37 @@ func (o *dockerEndpointOptions) addFlags(flags *pflag.FlagSet, prefix string) {
&o.host,
prefix+"host",
"",
"specify the docker endpoint on wich to connect")
"Docker endpoint on wich to connect")
flags.StringVar(
&o.apiVersion,
prefix+"api-version",
"",
"override negociated api version")
"Override negociated api version")
flags.StringVar(
&o.ca,
prefix+"tls-ca",
"",
"path to the ca file to validate docker endpoint")
"Trust certs signed only by this CA")
flags.StringVar(
&o.cert,
prefix+"tls-cert",
"",
"path to the cert file to authenticate to the docker endpoint")
"Path to TLS certificate file")
flags.StringVar(
&o.key,
prefix+"tls-key",
"",
"path to the key file to authenticate to the docker endpoint")
"Path to TLS key file")
flags.BoolVar(
&o.skipTLSVerify,
prefix+"tls-skip-verify",
false,
"skip tls verify when connecting to the docker endpoint")
"Skip TLS certificate validation")
flags.BoolVar(
&o.fromCurrent,
prefix+"from-current",
false,
"copy current docker endpoint configuration")
"Copy current docker endpoint configuration")
}

func (o *dockerEndpointOptions) toEndpoint(dockerCli command.Cli) (docker.Endpoint, error) {
Expand Down Expand Up @@ -92,22 +93,22 @@ func (o *kubernetesEndpointOptions) addFlags(flags *pflag.FlagSet, prefix string
&o.defaultNamespace,
prefix+"default-namespace",
"default",
"override default namespace when connecting to kubernetes endpoint")
"Default namespace when connecting to the kubernetes endpoint")
flags.StringVar(
&o.kubeconfigFile,
prefix+"kubeconfig",
"",
"path to an existing kubeconfig file")
"Path to an existing kubeconfig file")
flags.StringVar(
&o.kubeconfigContext,
prefix+"kubeconfig-context",
"",
`context to use in the kubeconfig file referenced in "kubernetes-kubeconfig"`)
fmt.Sprintf(`Context to use in the kubeconfig file referenced in "%skubeconfig"`, prefix))
flags.BoolVar(
&o.fromCurrent,
prefix+"from-current",
false,
"copy current kubernetes endpoint configuration")
"Copy current kubernetes endpoint configuration")
}

func (o *kubernetesEndpointOptions) toEndpoint(dockerCli command.Cli) (*kubernetes.Endpoint, error) {
Expand Down
23 changes: 23 additions & 0 deletions cli/command/context/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package context

import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/spf13/cobra"
)

func newSetCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "set",
Short: "Modify an existing context",
Args: cli.NoArgs,
RunE: command.ShowHelp(dockerCli.Err()),
}
cmd.AddCommand(
newSetDescriptionCommand(dockerCli),
newSetStackOrchestratorCommand(dockerCli),
newSetDockerEndpointCommand(dockerCli),
newSetKubernetesEndpointCommand(dockerCli),
)
return cmd
}
2 changes: 1 addition & 1 deletion cli/command/context/setdocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func newSetDockerEndpointCommand(dockerCli command.Cli) *cobra.Command {
opts := &dockerEndpointOptions{}
cmd := &cobra.Command{
Use: "set-docker-endpoint [OPTIONS] CONTEXT",
Use: "docker-endpoint [OPTIONS] CONTEXT",
Short: "Reset the docker endpoint of a context",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/command/context/setkubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func newSetKubernetesEndpointCommand(dockerCli command.Cli) *cobra.Command {
opts := &kubernetesEndpointOptions{}
cmd := &cobra.Command{
Use: "set-kubernetes-endpoint <context> [options]",
Use: "kubernetes-endpoint <context> [options]",
Short: "Reset the kubernetes endpoint of a context",
Args: cli.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down
65 changes: 36 additions & 29 deletions cli/command/context/setoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,51 @@ package context
import (
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type contextScopeOptions struct {
description string
defaultStackOrchestrator string
}

func newSetOptionsCommand(dockerCli command.Cli) *cobra.Command {
o := &contextScopeOptions{}
func newSetStackOrchestratorCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "set-options [options] CONTEXT",
Short: "set common options of a context",
Args: cli.ExactArgs(1),
Use: "stack-orchestrator CONTEXT ORCHESTRATOR",
Short: "Set the default stack orchestrator of a context",
Args: cli.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
contextMetadata, err := dockerCli.ContextStore().GetContextMetadata(args[0])
if err != nil {
return err
}
dockerContext, err := command.GetDockerContext(contextMetadata)
orchestrator, err := command.NormalizeOrchestrator(args[1])
if err != nil {
return err
}
if o.defaultStackOrchestrator != "" {
if dockerContext.StackOrchestrator, err = command.NormalizeOrchestrator(o.defaultStackOrchestrator); err != nil {
return errors.Wrap(err, "unable to parse default-stack-orchestrator")
}
}
if o.description != "" {
dockerContext.Description = o.description
}
contextMetadata.Metadata = dockerContext
return dockerCli.ContextStore().CreateOrUpdateContext(args[0], contextMetadata)
return setContextLevelOption(dockerCli, args[0], func(dc *command.DockerContext) {
dc.StackOrchestrator = orchestrator
})
},
}
return cmd
}

func newSetDescriptionCommand(dockerCli command.Cli) *cobra.Command {
cmd := &cobra.Command{
Use: "description CONTEXT DESCRIPTION",
Short: "Set description of a context",
Args: cli.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return setContextLevelOption(dockerCli, args[0], func(dc *command.DockerContext) {
dc.Description = args[1]
})
},
}
flags := cmd.Flags()
flags.StringVar(&o.description, "description", "", "set the description of the context")
flags.StringVar(&o.defaultStackOrchestrator, "default-stack-orchestrator", "", "set the default orchestrator for stack operations if different to the default one, to use with this context (swarm|kubernetes|all)")
return cmd
}

func setContextLevelOption(dockerCli command.Cli, name string, mutator func(*command.DockerContext)) error {
contextMetadata, err := dockerCli.ContextStore().GetContextMetadata(name)
if err != nil {
return err
}
dockerContext, err := command.GetDockerContext(contextMetadata)
if err != nil {
return err
}
mutator(&dockerContext)
contextMetadata.Metadata = dockerContext
return dockerCli.ContextStore().CreateOrUpdateContext(name, contextMetadata)
}

0 comments on commit 3662466

Please sign in to comment.