From 50330e7089bae80b6498f41b1d4b5981b98ad6b3 Mon Sep 17 00:00:00 2001 From: Mathieu Champlon Date: Fri, 11 May 2018 17:19:55 +0200 Subject: [PATCH] Make an error for an invalid orchestrator Signed-off-by: Mathieu Champlon --- cli/command/cli.go | 5 ++++- cli/command/orchestrator.go | 34 ++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cli/command/cli.go b/cli/command/cli.go index 55a6c436d4..d514a6a36b 100644 --- a/cli/command/cli.go +++ b/cli/command/cli.go @@ -166,7 +166,10 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions) error { if err != nil { return errors.Wrap(err, "Experimental field") } - orchestrator := GetOrchestrator(hasExperimental, opts.Common.Orchestrator, cli.configFile.Orchestrator) + orchestrator, err := GetOrchestrator(hasExperimental, opts.Common.Orchestrator, cli.configFile.Orchestrator) + if err != nil { + return err + } cli.clientInfo = ClientInfo{ DefaultVersion: cli.client.ClientVersion(), HasExperimental: hasExperimental, diff --git a/cli/command/orchestrator.go b/cli/command/orchestrator.go index d27bd6c1fa..b15c8f41bf 100644 --- a/cli/command/orchestrator.go +++ b/cli/command/orchestrator.go @@ -19,41 +19,39 @@ const ( envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR" ) -func normalize(flag string) Orchestrator { - switch flag { +func normalize(value string) (Orchestrator, error) { + switch value { case "kubernetes": - return OrchestratorKubernetes + return OrchestratorKubernetes, nil case "swarm": - return OrchestratorSwarm + return OrchestratorSwarm, nil + case "": + return orchestratorUnset, nil default: - return orchestratorUnset + return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes or swarm", value) } } // GetOrchestrator checks DOCKER_ORCHESTRATOR environment variable and configuration file // orchestrator value and returns user defined Orchestrator. -func GetOrchestrator(isExperimental bool, flagValue, value string) Orchestrator { +func GetOrchestrator(isExperimental bool, flagValue, value string) (Orchestrator, error) { // Non experimental CLI has kubernetes disabled if !isExperimental { - return defaultOrchestrator + return defaultOrchestrator, nil } // Check flag - if o := normalize(flagValue); o != orchestratorUnset { - return o + if o, err := normalize(flagValue); o != orchestratorUnset { + return o, err } // Check environment variable env := os.Getenv(envVarDockerOrchestrator) - if o := normalize(env); o != orchestratorUnset { - return o + if o, err := normalize(env); o != orchestratorUnset { + return o, err } // Check specified orchestrator - if o := normalize(value); o != orchestratorUnset { - return o - } - - if value != "" { - fmt.Fprintf(os.Stderr, "Specified orchestrator %q is invalid. Please use either kubernetes or swarm\n", value) + if o, err := normalize(value); o != orchestratorUnset { + return o, err } // Nothing set, use default orchestrator - return defaultOrchestrator + return defaultOrchestrator, nil }