/
configure.go
185 lines (155 loc) · 3.37 KB
/
configure.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package commands
import (
"fmt"
"os"
"reflect"
"strings"
"github.com/AlecAivazis/survey/v2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type ConfigureOptions struct {
}
func NewConfigFlags() *ConfigureOptions {
return &ConfigureOptions{}
}
func NewCmdConfig() *cobra.Command {
o := NewConfigFlags()
cmd := &cobra.Command{
Use: "configure",
Short: "Generate global configuration file",
RunE: func(cmd *cobra.Command, args []string) error {
err := o.Run()
if err != nil {
return err
}
return nil
},
}
return cmd
}
func (o *ConfigureOptions) Run() error {
home, err := os.UserHomeDir()
if err != nil {
return err
}
gc := map[string]map[string]map[string]string{}
viper.SetConfigFile(fmt.Sprintf("%s/.ize/config.toml", home))
err = viper.ReadInConfig()
if err != nil {
return err
}
err = viper.Unmarshal(&gc)
if err != nil {
return err
}
env, exist := os.LookupEnv("IZE_ENV")
if !exist {
env = os.Getenv("ENV")
}
region, exist := os.LookupEnv("IZE_AWS_REGION")
if !exist {
region = os.Getenv("AWS_REGION")
}
profile, exist := os.LookupEnv("IZE_AWS_PROFILE")
if !exist {
profile = os.Getenv("AWS_PROFILE")
}
namespace, exist := os.LookupEnv("IZE_NAMESPACE")
if !exist {
namespace = os.Getenv("NAMESPACE")
}
err = survey.AskOne(&survey.Input{
Message: " namespace:",
Default: namespace,
}, &namespace, survey.WithIcons(func(is *survey.IconSet) {
is.Question.Text = " ??"
is.Question.Format = "black:green"
is.Error.Format = "black:red"
}))
if err != nil {
return err
}
err = survey.AskOne(&survey.Input{
Message: " env:",
Default: env,
}, &env, survey.WithIcons(func(is *survey.IconSet) {
is.Question.Text = " ??"
is.Question.Format = "black:green"
is.Error.Format = "black:red"
}))
if err != nil {
return err
}
var qs = []*survey.Question{
{
Prompt: &survey.Input{
Message: " aws region:",
Default: region,
},
Validate: survey.Required,
Name: "aws_region",
},
{
Prompt: &survey.Input{
Message: " aws profile:",
Default: profile,
},
Validate: survey.Required,
Name: "aws_profile",
},
{
Prompt: &survey.Input{
Message: " terraform version:",
},
Validate: survey.Required,
Name: "terraform_version",
},
}
opts := Config{}
err = survey.Ask(qs, &opts, survey.WithIcons(func(is *survey.IconSet) {
is.Question.Text = " ??"
is.Question.Format = "black:green"
is.Error.Format = "black:red"
}))
if err != nil {
return err
}
v := reflect.ValueOf(opts)
typeOfOpts := v.Type()
viper.Reset()
if gc[namespace] == nil {
gc[namespace] = make(map[string]map[string]string)
}
if gc[namespace][env] == nil {
gc[namespace][env] = make(map[string]string)
}
for i := 0; i < v.NumField(); i++ {
gc[namespace][env][strings.ToLower(typeOfOpts.Field(i).Name)] = v.Field(i).String()
}
raw := make(map[string]interface{}, len(gc))
for k, v := range gc {
raw[k] = v
}
err = viper.MergeConfigMap(raw)
if err != nil {
return err
}
err = os.MkdirAll(fmt.Sprintf("%s/.ize", home), 0755)
if err != nil {
return err
}
err = viper.WriteConfigAs(fmt.Sprintf("%s/.ize/config.toml", home))
if err != nil {
return fmt.Errorf("can't write config: %w", err)
}
return nil
}
type Namespace struct {
Env map[string]Config
}
type Config struct {
AwsProfile string
AwsRegion string
TerraformVersion string
}