-
Notifications
You must be signed in to change notification settings - Fork 4
/
compose_add.go
246 lines (206 loc) · 6.44 KB
/
compose_add.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package subcommands
import (
"errors"
"fmt"
"os"
"path"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
func CreateComposeAddCommand() *cobra.Command {
var serviceNameFlag string
var serviceImageFlag string
var servicePortsFlag []string
var serviceNetworksFlags []string
var serviceHostnameFlag string
composeAddCmd := &cobra.Command{
Use: "add",
Short: "Adds a new service into docker-compose.yml",
Run: func(cmd *cobra.Command, args []string) {
envs := viper.GetStringMapString("envs")
if len(serviceNameFlag) == 0 {
prompt := promptui.Prompt{
Label: "Service name",
Validate: validatePrompt,
}
sN, err := prompt.Run()
if err != nil {
os.Exit(1)
}
serviceNameFlag = sN
}
service := Service{
Image: serviceImageFlag,
Hostname: serviceHostnameFlag,
Environment: envs,
Networks: serviceNetworksFlags,
Ports: servicePortsFlag,
}
workingDir, getWdError := os.Getwd()
if getWdError != nil {
fmt.Println(text.FgRed.Sprint("oops! could not get current working directory."))
os.Exit(1)
}
dockerComposePath := path.Join(workingDir, "docker-compose.yml")
dockerComposeFileInfo, statComposeError := os.Stat(dockerComposePath)
if statComposeError != nil {
fmt.Println(text.FgRed.Sprint("for some reason, it failed to read docker-compose.yml file."))
os.Exit(1)
}
// todo fallback to empty composeYml
dockerComposeFileContents, readComposeError := os.ReadFile(dockerComposePath)
if readComposeError != nil {
fmt.Println(text.FgRed.Sprint("for some reason, it failed to read docker-compose.yml file."))
os.Exit(1)
}
var composeYml DockerCompose
yamlParseError := yaml.Unmarshal(dockerComposeFileContents, &composeYml)
if yamlParseError != nil {
fmt.Println(text.FgRed.Sprint("can't manage docker-compose.yml, the contents of the file are invalid."))
os.Exit(1)
}
if composeYml.Version == "" {
prompt := promptui.Select{
Label: "It seems like your docker-compose file does not have a version defined. Would you like to define one?",
Items: []string{"yes", "no"},
}
_, answer, _ := prompt.Run()
if answer == "yes" {
promptVersion := promptui.Prompt{
Label: "Type the disired version",
Validate: validatePrompt,
}
version, _ := promptVersion.Run()
composeYml.Version = version
}
}
newCompose, addServiceError := composeAdd(&composeYml, serviceNameFlag, service)
if addServiceError != nil {
fmt.Println(text.FgRed.Sprint(addServiceError))
return
}
newComposeYmlAsBytes, marshalError := yaml.Marshal(newCompose)
if marshalError != nil {
fmt.Println(text.FgRed.Sprint("can't manage docker-compose.yml, the contents of the file are invalid."))
return
}
if err := os.WriteFile(dockerComposePath, newComposeYmlAsBytes, dockerComposeFileInfo.Mode()); err != nil {
fmt.Println(text.FgRed.Sprint(err))
return
}
fmt.Println(text.FgGreen.Sprint("service added to docker-compose.yml succesfully!"))
},
}
composeAddCmd.Flags().StringVarP(&serviceNameFlag, "service", "s", "", "sets the service name in docker-compose")
composeAddCmd.Flags().StringVarP(&serviceImageFlag, "image", "i", "", "sets the service image in docker-compose")
composeAddCmd.Flags().StringSliceVarP(&servicePortsFlag, "ports", "p", []string{}, "sets the service port in service in docker-compose")
composeAddCmd.Flags().StringToStringP("envs", "e", map[string]string{}, "sets an service environment variable in docker-compose")
if err := viper.BindPFlag("envs", composeAddCmd.Flags().Lookup("envs")); err != nil {
fmt.Println(text.FgRed.Sprint(err))
os.Exit(1)
}
composeAddCmd.Flags().StringSliceVarP(&serviceNetworksFlags, "networks", "n", []string{}, "sets the service network in docker-compose")
composeAddCmd.Flags().StringVarP(&serviceHostnameFlag, "hostname", "o", "", "sets the service hostname in docker-compose")
return composeAddCmd
}
func composeAdd(compose *DockerCompose, serviceName string, service Service) (*DockerCompose, error) {
newCompose := compose
if newCompose.Services == nil {
newCompose.Services = make(map[string]Service)
}
newservice := setServiceSettings(&service)
if _, ok := newCompose.Services[serviceName]; ok {
return nil, fmt.Errorf("%s is conflicting with a service with same name", serviceName)
}
newCompose.Services[serviceName] = *newservice
return newCompose, nil
}
func setServiceSettings(service *Service) *Service {
data := service
if len(data.Image) == 0 {
imagePrompt := promptui.Prompt{
Label: "Image",
Validate: validatePrompt,
}
image, _ := imagePrompt.Run()
data.Image = image
}
if len(data.Hostname) == 0 {
hostnamePrompt := promptui.Prompt{
Label: "Hostname",
Validate: validatePrompt,
}
hostname, _ := hostnamePrompt.Run()
data.Hostname = hostname
}
if len(data.Environment) == 0 {
for {
promptKey := promptui.Prompt{
Label: "Enter a key for the Environment map (or 'stop' to finish)",
Validate: validatePrompt,
}
key, err := promptKey.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return nil
}
if key == "stop" {
break
}
promptValue := promptui.Prompt{
Label: fmt.Sprintf("Enter a value for the key '%s'", key),
Validate: validatePrompt,
}
value, err := promptValue.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return nil
}
data.Environment[key] = value
}
}
if len(data.Ports) == 0 {
for {
promptPort := promptui.Prompt{
Label: "Enter a port for the Ports (or 'stop' to finish)",
Validate: validatePrompt,
}
port, err := promptPort.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return nil
}
if port == "stop" {
break
}
data.Ports = append(data.Ports, port)
}
}
if len(data.Networks) == 0 {
for {
promptNetwork := promptui.Prompt{
Label: "Enter a network for Networks (or 'stop' to finish) ",
Validate: validatePrompt,
}
network, err := promptNetwork.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return nil
}
if network == "stop" {
break
}
data.Networks = append(data.Networks, network)
}
}
return data
}
func validatePrompt(input string) error {
if len(input) < 1 {
return errors.New("This field is required")
}
return nil
}