Skip to content

Commit

Permalink
* Export stack commands
Browse files Browse the repository at this point in the history
* Factorize common orchestrator switch code between stack named commands (deploy/ps/remove/services)

Signed-off-by: Silvin Lubecki <[email protected]>
  • Loading branch information
silvin-lubecki committed Nov 14, 2018
1 parent 632230f commit bbf87ac
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 53 deletions.
24 changes: 24 additions & 0 deletions cli/command/stack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"fmt"
"strings"
"unicode"

"github.com/docker/cli/cli/command"
"github.com/docker/cli/cli/command/stack/kubernetes"
"github.com/spf13/pflag"
)

// validateStackName checks if the provided string is a valid stack name (namespace).
Expand All @@ -29,3 +33,23 @@ func validateStackNames(namespaces []string) error {
func quotesOrWhitespace(r rune) bool {
return unicode.IsSpace(r) || r == '"' || r == '\''
}

func runOrchestratedCommand(dockerCli command.Cli, flags *pflag.FlagSet, common *commonOptions, swarmCmd func() error, kubernetesCmd func(*kubernetes.KubeCli) error) error {
commonOrchestrator := command.OrchestratorSwarm // default for top-level deploy command
if common != nil {
commonOrchestrator = common.orchestrator
}
//return RunPs(dockerCli, cmd.Flags(), commonOrchestrator, opts)
switch {
case commonOrchestrator.HasAll():
return errUnsupportedAllOrchestrator
case commonOrchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(flags, commonOrchestrator))
if err != nil {
return err
}
return kubernetesCmd(kli)
default:
return swarmCmd()
}
}
19 changes: 5 additions & 14 deletions cli/command/stack/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func newDeployCommand(dockerCli command.Cli, common *commonOptions) *cobra.Comma
if err != nil {
return err
}
return RunDeploy(dockerCli, cmd.Flags(), config, commonOrchestrator, opts)
return RunDeploy(dockerCli, cmd.Flags(), config, common, opts)
},
}

Expand All @@ -74,17 +74,8 @@ func newDeployCommand(dockerCli command.Cli, common *commonOptions) *cobra.Comma
}

// RunDeploy performs a stack deploy against the specified orchestrator
func RunDeploy(dockerCli command.Cli, flags *pflag.FlagSet, config *composetypes.Config, commonOrchestrator command.Orchestrator, opts options.Deploy) error {
switch {
case commonOrchestrator.HasAll():
return errUnsupportedAllOrchestrator
case commonOrchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(flags, commonOrchestrator))
if err != nil {
return errors.Wrap(err, "unable to deploy to Kubernetes")
}
return kubernetes.RunDeploy(kli, opts, config)
default:
return swarm.RunDeploy(dockerCli, opts, config)
}
func RunDeploy(dockerCli command.Cli, flags *pflag.FlagSet, config *composetypes.Config, common *commonOptions, opts options.Deploy) error {
return runOrchestratedCommand(dockerCli, flags, common,
func() error { return swarm.RunDeploy(dockerCli, opts, config) },
func(kli *kubernetes.KubeCli) error { return kubernetes.RunDeploy(kli, opts, config) })
}
22 changes: 9 additions & 13 deletions cli/command/stack/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/cli/cli/command/stack/swarm"
cliopts "github.com/docker/cli/opts"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func newPsCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
Expand All @@ -22,19 +23,7 @@ func newPsCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
if err := validateStackName(opts.Namespace); err != nil {
return err
}

switch {
case common.orchestrator.HasAll():
return errUnsupportedAllOrchestrator
case common.orchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), common.orchestrator))
if err != nil {
return err
}
return kubernetes.RunPS(kli, opts)
default:
return swarm.RunPS(dockerCli, opts)
}
return RunPs(dockerCli, cmd.Flags(), common, opts)
},
}
flags := cmd.Flags()
Expand All @@ -46,3 +35,10 @@ func newPsCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
kubernetes.AddNamespaceFlag(flags)
return cmd
}

// RunPs performs a stack ps against the specified orchestrator
func RunPs(dockerCli command.Cli, flags *pflag.FlagSet, common *commonOptions, opts options.PS) error {
return runOrchestratedCommand(dockerCli, flags, common,
func() error { return swarm.RunPS(dockerCli, opts) },
func(kli *kubernetes.KubeCli) error { return kubernetes.RunPS(kli, opts) })
}
22 changes: 9 additions & 13 deletions cli/command/stack/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/docker/cli/cli/command/stack/options"
"github.com/docker/cli/cli/command/stack/swarm"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func newRemoveCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
Expand All @@ -22,22 +23,17 @@ func newRemoveCommand(dockerCli command.Cli, common *commonOptions) *cobra.Comma
if err := validateStackNames(opts.Namespaces); err != nil {
return err
}

switch {
case common.orchestrator.HasAll():
return errUnsupportedAllOrchestrator
case common.orchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), common.orchestrator))
if err != nil {
return err
}
return kubernetes.RunRemove(kli, opts)
default:
return swarm.RunRemove(dockerCli, opts)
}
return RunRemove(dockerCli, cmd.Flags(), common, opts)
},
}
flags := cmd.Flags()
kubernetes.AddNamespaceFlag(flags)
return cmd
}

// RunRemove performs a stack remove against the specified orchestrator
func RunRemove(dockerCli command.Cli, flags *pflag.FlagSet, common *commonOptions, opts options.Remove) error {
return runOrchestratedCommand(dockerCli, flags, common,
func() error { return swarm.RunRemove(dockerCli, opts) },
func(kli *kubernetes.KubeCli) error { return kubernetes.RunRemove(kli, opts) })
}
22 changes: 9 additions & 13 deletions cli/command/stack/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/cli/cli/command/stack/swarm"
cliopts "github.com/docker/cli/opts"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func newServicesCommand(dockerCli command.Cli, common *commonOptions) *cobra.Command {
Expand All @@ -22,19 +23,7 @@ func newServicesCommand(dockerCli command.Cli, common *commonOptions) *cobra.Com
if err := validateStackName(opts.Namespace); err != nil {
return err
}

switch {
case common.orchestrator.HasAll():
return errUnsupportedAllOrchestrator
case common.orchestrator.HasKubernetes():
kli, err := kubernetes.WrapCli(dockerCli, kubernetes.NewOptions(cmd.Flags(), common.orchestrator))
if err != nil {
return err
}
return kubernetes.RunServices(kli, opts)
default:
return swarm.RunServices(dockerCli, opts)
}
return RunServices(dockerCli, cmd.Flags(), common, opts)
},
}
flags := cmd.Flags()
Expand All @@ -44,3 +33,10 @@ func newServicesCommand(dockerCli command.Cli, common *commonOptions) *cobra.Com
kubernetes.AddNamespaceFlag(flags)
return cmd
}

// RunServices performs a stack services against the specified orchestrator
func RunServices(dockerCli command.Cli, flags *pflag.FlagSet, common *commonOptions, opts options.Services) error {
return runOrchestratedCommand(dockerCli, flags, common,
func() error { return swarm.RunServices(dockerCli, opts) },
func(kli *kubernetes.KubeCli) error { return kubernetes.RunServices(kli, opts) })
}

0 comments on commit bbf87ac

Please sign in to comment.