2018-06-26 08:07:26 -04:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"unicode"
|
2019-01-14 12:00:14 -05:00
|
|
|
|
|
|
|
"github.com/docker/cli/cli/command"
|
|
|
|
"github.com/docker/cli/cli/command/stack/kubernetes"
|
|
|
|
"github.com/spf13/pflag"
|
2018-06-26 08:07:26 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// validateStackName checks if the provided string is a valid stack name (namespace).
|
|
|
|
// It currently only does a rudimentary check if the string is empty, or consists
|
|
|
|
// of only whitespace and quoting characters.
|
|
|
|
func validateStackName(namespace string) error {
|
|
|
|
v := strings.TrimFunc(namespace, quotesOrWhitespace)
|
|
|
|
if v == "" {
|
|
|
|
return fmt.Errorf("invalid stack name: %q", namespace)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateStackNames(namespaces []string) error {
|
|
|
|
for _, ns := range namespaces {
|
|
|
|
if err := validateStackName(ns); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func quotesOrWhitespace(r rune) bool {
|
|
|
|
return unicode.IsSpace(r) || r == '"' || r == '\''
|
|
|
|
}
|
2019-01-14 12:00:14 -05:00
|
|
|
|
|
|
|
func runOrchestratedCommand(dockerCli command.Cli, flags *pflag.FlagSet, commonOrchestrator command.Orchestrator, swarmCmd func() error, kubernetesCmd func(*kubernetes.KubeCli) error) error {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|