2017-12-04 03:53:03 -05:00
|
|
|
package command
|
2017-11-20 09:30:52 -05:00
|
|
|
|
|
|
|
import (
|
2017-12-05 05:13:38 -05:00
|
|
|
"fmt"
|
2017-11-20 09:30:52 -05:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Orchestrator type acts as an enum describing supported orchestrators.
|
|
|
|
type Orchestrator string
|
|
|
|
|
|
|
|
const (
|
2017-12-04 06:30:39 -05:00
|
|
|
// OrchestratorKubernetes orchestrator
|
2017-12-04 03:53:03 -05:00
|
|
|
OrchestratorKubernetes = Orchestrator("kubernetes")
|
2017-12-04 06:30:39 -05:00
|
|
|
// OrchestratorSwarm orchestrator
|
2017-12-04 03:53:03 -05:00
|
|
|
OrchestratorSwarm = Orchestrator("swarm")
|
2018-04-25 08:24:46 -04:00
|
|
|
// OrchestratorAll orchestrator
|
|
|
|
OrchestratorAll = Orchestrator("all")
|
2017-12-04 03:53:03 -05:00
|
|
|
orchestratorUnset = Orchestrator("unset")
|
2017-11-20 09:30:52 -05:00
|
|
|
|
2018-01-02 17:56:07 -05:00
|
|
|
defaultOrchestrator = OrchestratorSwarm
|
|
|
|
envVarDockerOrchestrator = "DOCKER_ORCHESTRATOR"
|
2017-11-20 09:30:52 -05:00
|
|
|
)
|
|
|
|
|
2018-05-11 11:19:55 -04:00
|
|
|
func normalize(value string) (Orchestrator, error) {
|
|
|
|
switch value {
|
2018-01-11 15:16:37 -05:00
|
|
|
case "kubernetes":
|
2018-05-11 11:19:55 -04:00
|
|
|
return OrchestratorKubernetes, nil
|
2018-01-11 15:16:37 -05:00
|
|
|
case "swarm":
|
2018-05-11 11:19:55 -04:00
|
|
|
return OrchestratorSwarm, nil
|
|
|
|
case "":
|
|
|
|
return orchestratorUnset, nil
|
2018-04-25 08:24:46 -04:00
|
|
|
case "all":
|
|
|
|
return OrchestratorAll, nil
|
2017-11-20 09:30:52 -05:00
|
|
|
default:
|
2018-04-25 08:24:46 -04:00
|
|
|
return defaultOrchestrator, fmt.Errorf("specified orchestrator %q is invalid, please use either kubernetes, swarm or all", value)
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrchestrator checks DOCKER_ORCHESTRATOR environment variable and configuration file
|
|
|
|
// orchestrator value and returns user defined Orchestrator.
|
2018-05-18 11:54:18 -04:00
|
|
|
func GetOrchestrator(flagValue, value string) (Orchestrator, error) {
|
2017-12-20 13:06:15 -05:00
|
|
|
// Check flag
|
2018-05-11 11:19:55 -04:00
|
|
|
if o, err := normalize(flagValue); o != orchestratorUnset {
|
|
|
|
return o, err
|
2017-12-20 13:06:15 -05:00
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
// Check environment variable
|
2018-01-02 17:56:07 -05:00
|
|
|
env := os.Getenv(envVarDockerOrchestrator)
|
2018-05-11 11:19:55 -04:00
|
|
|
if o, err := normalize(env); o != orchestratorUnset {
|
|
|
|
return o, err
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|
2017-12-04 06:30:39 -05:00
|
|
|
// Check specified orchestrator
|
2018-05-11 11:19:55 -04:00
|
|
|
if o, err := normalize(value); o != orchestratorUnset {
|
|
|
|
return o, err
|
2017-12-05 05:13:38 -05:00
|
|
|
}
|
2017-11-20 09:30:52 -05:00
|
|
|
// Nothing set, use default orchestrator
|
2018-05-11 11:19:55 -04:00
|
|
|
return defaultOrchestrator, nil
|
2017-11-20 09:30:52 -05:00
|
|
|
}
|