diff --git a/stim/prompt.go b/stim/prompt.go index c3ec003..e355056 100644 --- a/stim/prompt.go +++ b/stim/prompt.go @@ -39,11 +39,14 @@ func (stim *Stim) PromptBool(label string, override bool, defaultvalue bool) (bo return true, nil } - defaultstring := "n" + y := "y" + n := "n" if defaultvalue { - defaultstring = "y" + y = strings.ToUpper(y) + } else { + n = strings.ToUpper(n) } - label = label + " (y/n) [" + defaultstring + "]" + label = label + " (" + y + "/" + n + ")" prompt := promptui.Prompt{ Label: label, @@ -54,7 +57,11 @@ func (stim *Stim) PromptBool(label string, override bool, defaultvalue bool) (bo return false, err } - if result == "" || strings.ToLower(strings.TrimSpace(result))[0:1] == "y" { + if result == "" { + return defaultvalue, nil + } + + if strings.ToLower(strings.TrimSpace(result))[0:1] == "y" { return true, nil } diff --git a/stimpacks/deploy/config.go b/stimpacks/deploy/config.go index 379dbcb..d080607 100644 --- a/stimpacks/deploy/config.go +++ b/stimpacks/deploy/config.go @@ -51,9 +51,10 @@ type Global struct { // Spec contains the spec of a given environment/instance type Spec struct { - Kubernetes Kubernetes `yaml:"kubernetes"` - Secrets []*v2e.SecretItem `yaml:"secrets"` - EnvironmentVars []*EnvironmentVar `yaml:"env"` + Kubernetes Kubernetes `yaml:"kubernetes"` + Secrets []*v2e.SecretItem `yaml:"secrets"` + EnvironmentVars []*EnvironmentVar `yaml:"env"` + AddConfermationPrompt bool `yaml:"addConfermationPrompt"` } // Kubernetes describes the Kubernetes configuration to use @@ -64,10 +65,11 @@ type Kubernetes struct { // Environment describes a deployment environment (i.e. dev, stage, prod, etc.) type Environment struct { - Name string `yaml:"name"` - Spec *Spec `yaml:"spec"` - Instances []*Instance `yaml:"instances"` - instanceMap map[string]int + Name string `yaml:"name"` + Spec *Spec `yaml:"spec"` + Instances []*Instance `yaml:"instances"` + RemoveAllPrompt bool `yaml:"removeAllPrompt"` + instanceMap map[string]int } // Instance describes an instance of a deployment within an environment (i.e. us-west-2 for env prod) diff --git a/stimpacks/deploy/deploy.go b/stimpacks/deploy/deploy.go index 85ac01a..3701710 100644 --- a/stimpacks/deploy/deploy.go +++ b/stimpacks/deploy/deploy.go @@ -1,6 +1,7 @@ package deploy import ( + "os" "strings" log "github.com/PremiereGlobal/stim/pkg/stimlog" @@ -54,16 +55,28 @@ func (d *Deploy) Run() { environmentList[i] = e.Name } selectedEnvironmentName, _ = d.stim.PromptList("Which environment?", environmentList, d.stim.ConfigGetString("deploy.environment")) + if selectedEnvironmentName == "" { + d.log.Info("No environment selected! exiting") + os.Exit(0) + } } selectedEnvironment := d.config.Environments[d.config.environmentMap[selectedEnvironmentName]] // Determine the selected instance (via cli param) or prompt the user - instanceList := make([]string, len(selectedEnvironment.Instances)+1) - instanceList[0] = allOptionPrompt - for i, inst := range selectedEnvironment.Instances { - instanceList[i+1] = inst.Name + instanceList := make([]string, 0) + + //Check if we should remove all prompt or not + if !selectedEnvironment.RemoveAllPrompt { + instanceList = append(instanceList, allOptionPrompt) + } + for _, inst := range selectedEnvironment.Instances { + instanceList = append(instanceList, inst.Name) } selectedInstanceName, _ := d.stim.PromptList("Which instance?", instanceList, d.stim.ConfigGetString("deploy.instance")) + if selectedInstanceName == "" { + d.log.Info("No instance selected! exiting") + os.Exit(0) + } if strings.ToLower(selectedInstanceName) == strings.ToLower(allOptionPrompt) || strings.ToLower(selectedInstanceName) == strings.ToLower(allOptionCli) { selectedInstanceName = allOptionCli } else if _, ok := selectedEnvironment.instanceMap[selectedInstanceName]; !ok { @@ -73,11 +86,34 @@ func (d *Deploy) Run() { // Run the deployment(s) if selectedInstanceName == allOptionCli { d.log.Info("Deploying to all clusters in environment: {}", selectedEnvironment.Name) + //Check if confermation prompt is required + if selectedEnvironment.Spec.AddConfermationPrompt { + //Do AddConfermationPrompt, only if the instance is not passed on the cli + proceed, _ := d.stim.PromptBool("Proceed?", d.stim.ConfigGetString("deploy.instance") != "", false) + if !proceed { + os.Exit(1) + } + } for _, inst := range selectedEnvironment.Instances { + if inst.Spec.AddConfermationPrompt { + //Do AddConfermationPrompt, only if the instance is not passed on the cli + proceed, _ := d.stim.PromptBool("Proceed?", d.stim.ConfigGetString("deploy.instance") != "", false) + if !proceed { + os.Exit(1) + } + } d.Deploy(selectedEnvironment, inst) } } else { - d.Deploy(selectedEnvironment, selectedEnvironment.Instances[selectedEnvironment.instanceMap[selectedInstanceName]]) + d.log.Info("Deploying to environment: {} and instance: {}", selectedInstanceName) + inst := selectedEnvironment.Instances[selectedEnvironment.instanceMap[selectedInstanceName]] + if selectedEnvironment.Spec.AddConfermationPrompt || inst.Spec.AddConfermationPrompt { + proceed, _ := d.stim.PromptBool("Proceed?", d.stim.ConfigGetString("deploy.instance") != "", false) + if !proceed { + os.Exit(1) + } + } + d.Deploy(selectedEnvironment, inst) } }