Skip to content

Commit d4f95bf

Browse files
authored
Merge pull request #142 from commitdev/prompt-user-for-apply-environments
#133 : Prompt the user for which environments to apply against
2 parents 672019a + 7f6b8b5 commit d4f95bf

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

cmd/apply.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
"log"
6+
47
"github.com/commitdev/zero/configs"
8+
"github.com/commitdev/zero/pkg/util/exit"
9+
"github.com/manifoldco/promptui"
510
"github.com/spf13/cobra"
611
)
712

813
var applyConfigPath string
14+
var applyEnvironments []string
915

1016
func init() {
1117
applyCmd.PersistentFlags().StringVarP(&applyConfigPath, "config", "c", configs.ZeroProjectYml, "config path")
18+
applyCmd.PersistentFlags().StringSliceVarP(&applyEnvironments, "env", "e", []string{}, "environments to set up (staging, production) - specify multiple times for multiple")
1219

1320
rootCmd.AddCommand(applyCmd)
1421
}
@@ -18,5 +25,45 @@ var applyCmd = &cobra.Command{
1825
Short: "Execute modules to create projects, infrastructure, etc.",
1926
Run: func(cmd *cobra.Command, args []string) {
2027

28+
if len(applyEnvironments) == 0 {
29+
fmt.Println(`Choose the environments to apply. This will create infrastructure, CI pipelines, etc.
30+
At this point, real things will be generated that may cost money!
31+
Only a single environment may be suitable for an initial test, but for a real system we suggest setting up both staging and production environments.`)
32+
applyEnvironments = promptEnvironments()
33+
}
34+
35+
// Strict for now, we can brainstorm how much we want to support custom environments later
36+
for _, env := range applyEnvironments {
37+
if env != "staging" && env != "production" {
38+
exit.Fatal("The currently supported environments are \"staging\" and \"production\"")
39+
}
40+
}
41+
42+
// @TODO : Pass environments to make commands
2143
},
2244
}
45+
46+
// promptEnvironments Prompts the user for the environments to apply against and returns a slice of strings representing the environments
47+
func promptEnvironments() []string {
48+
items := map[string][]string{
49+
"Staging ": []string{"staging"},
50+
"Production": []string{"production"},
51+
"Both Staging and Production": []string{"staging", "production"},
52+
}
53+
54+
var labels []string
55+
for label := range items {
56+
labels = append(labels, label)
57+
}
58+
59+
providerPrompt := promptui.Select{
60+
Label: "Environments",
61+
Items: labels,
62+
}
63+
_, providerResult, err := providerPrompt.Run()
64+
if err != nil {
65+
log.Fatalf("Prompt failed %v\n", err)
66+
panic(err)
67+
}
68+
return items[providerResult]
69+
}

0 commit comments

Comments
 (0)