/
config.go
160 lines (150 loc) · 4.12 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package cli
import (
"errors"
"fmt"
"math/rand"
"strings"
"time"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func init() {
rootCmd.AddCommand(configCmd)
}
var configCmd = &cobra.Command{
Use: "config",
Short: "Setup config file for rattlesnakeos-stack",
Run: func(cmd *cobra.Command, args []string) {
color.Cyan(fmt.Sprintln("Device is the device codename (e.g. sailfish). Supported devices:", supportDevicesOutput))
validate := func(input string) error {
if len(input) < 1 {
return errors.New("Device name is too short")
}
found := false
for _, d := range supportedDevicesCodename {
if input == d {
found = true
break
}
}
if !found {
return errors.New("Invalid device")
}
return nil
}
devicePrompt := promptui.Prompt{
Label: "Device ",
Default: viper.GetString("device"),
Validate: validate,
}
result, err := devicePrompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
viper.Set("device", result)
defaultName := fmt.Sprintf("rattlesnakeos-%v-%v", result, randomString(10))
if viper.GetString("name") != "" {
defaultName = viper.GetString("name")
}
color.Cyan(fmt.Sprintln("Stack name is used as an identifier for all the AWS components that get deployed. THIS NAME MUST BE UNIQUE OR DEPLOYMENT WILL FAIL."))
validate = func(input string) error {
if len(input) < 1 {
return errors.New("Stack name is too short")
}
return nil
}
namePrompt := promptui.Prompt{
Label: "Stack name ",
Validate: validate,
Default: defaultName,
}
result, err = namePrompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
viper.Set("name", result)
color.Cyan(fmt.Sprintf("Stack region is the AWS region where you would like to deploy your stack. Valid options: %v\n",
strings.Join(supportedRegions, ", ")))
validate = func(input string) error {
if len(input) < 1 {
return errors.New("Stack region is too short")
}
found := false
for _, region := range supportedRegions {
if input == region {
found = true
break
}
}
if !found {
return errors.New("Invalid region")
}
return nil
}
regionPrompt := promptui.Prompt{
Label: "Stack region ",
Default: viper.GetString("region"),
Validate: validate,
}
result, err = regionPrompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
viper.Set("region", result)
color.Cyan(fmt.Sprintln("Email address you would like to send build notifications to."))
validate = func(input string) error {
if !strings.Contains(input, "@") {
return errors.New("Must provide valid email")
}
return nil
}
emailPrompt := promptui.Prompt{
Label: "Email ",
Validate: validate,
Default: viper.GetString("email"),
}
result, err = emailPrompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
viper.Set("email", result)
defaultKeypairName := "rattlesnakeos"
if viper.GetString("ssh-key") != "" {
defaultKeypairName = viper.GetString("ssh-key")
}
color.Cyan(fmt.Sprintln("SSH keypair name is the name of your EC2 keypair that was imported into AWS."))
validate = func(input string) error {
if len(input) < 1 {
return errors.New("SSH keypair name is too short")
}
return nil
}
keypairPrompt := promptui.Prompt{
Label: "SSH Keypair Name ",
Default: defaultKeypairName,
Validate: validate,
}
result, err = keypairPrompt.Run()
if err != nil {
log.Fatalf("Prompt failed %v\n", err)
}
viper.Set("ssh-key", result)
err = viper.WriteConfigAs(configFileFullPath)
if err != nil {
log.WithError(err).Fatalf("failed to write config file %s", configFileFullPath)
}
log.Infof("rattlesnakeos-stack config file has been written to %v", configFileFullPath)
},
}
func randomString(strlen int) string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
const chars = "abcdefghijklmnopqrstuvwxyz0123456789"
result := make([]byte, strlen)
for i := range result {
result[i] = chars[r.Intn(len(chars))]
}
return string(result)
}