@@ -15,25 +15,31 @@ import (
15
15
)
16
16
17
17
func main () {
18
+ // Define subcommands
18
19
generateCmd := flag .NewFlagSet ("generate" , flag .ExitOnError )
20
+ terraformCmd := flag .NewFlagSet ("terraform" , flag .ExitOnError )
21
+
22
+ // Define flags for 'generate' subcommand
19
23
company := generateCmd .String ("company" , "" , "Company name (required)" )
20
24
product := generateCmd .String ("product" , "" , "Product name (required)" )
21
25
provider := generateCmd .String ("provider" , "" , "Provider name (required)" )
22
26
modules := generateCmd .String ("modules" , "" , "Comma-separated list of modules" )
23
27
customers := generateCmd .String ("customers" , "" , "Comma-separated list of customers" )
24
28
25
- terraformCmd := flag . NewFlagSet ( " terraform" , flag . ExitOnError )
26
- tfCommand := terraformCmd .String ("command" , "" , "Terraform command to execute (init, plan, apply, build, print)" )
29
+ // Define flags for ' terraform' subcommand
30
+ tfCommand := terraformCmd .String ("command" , "" , "Terraform command to execute (init, validate, plan, apply, build, destroy , print)" )
27
31
tfCompany := terraformCmd .String ("company" , "" , "Company name (required)" )
28
32
tfProduct := terraformCmd .String ("product" , "" , "Product name (required)" )
29
33
tfProvider := terraformCmd .String ("provider" , "" , "Provider name (required)" )
30
34
tfInfratype := terraformCmd .String ("infratype" , "" , "Infrastructure type (prod or nonprod)" )
31
35
36
+ // Ensure a subcommand is provided
32
37
if len (os .Args ) < 2 {
33
38
fmt .Println ("Expected 'generate' or 'terraform' subcommands" )
34
39
os .Exit (1 )
35
40
}
36
41
42
+ // Parse subcommands
37
43
switch os .Args [1 ] {
38
44
case "generate" :
39
45
generateCmd .Parse (os .Args [2 :])
@@ -53,12 +59,15 @@ func main() {
53
59
}
54
60
}
55
61
62
+ // handleGenerateCommand processes the 'generate' subcommand
56
63
func handleGenerateCommand (company , product , provider , modules , customers string ) {
64
+ // Validate required flags
57
65
if company == "" || product == "" || provider == "" {
58
66
fmt .Println ("Error: --company, --product, and --provider are required" )
59
67
os .Exit (1 )
60
68
}
61
69
70
+ // Create a GenerateRequest
62
71
req := models.GenerateRequest {
63
72
OrganisationName : company ,
64
73
ProductName : product ,
@@ -82,6 +91,7 @@ func handleGenerateCommand(company, product, provider, modules, customers string
82
91
}
83
92
}
84
93
94
+ // Generate Terraform code
85
95
if err := services .GenerateTerraform (& req ); err != nil {
86
96
fmt .Printf ("Error generating Terraform code: %v\n " , err )
87
97
os .Exit (1 )
@@ -90,41 +100,51 @@ func handleGenerateCommand(company, product, provider, modules, customers string
90
100
fmt .Println ("Terraform code generated successfully" )
91
101
}
92
102
103
+ // handleTerraformCommand processes the 'terraform' subcommand
93
104
func handleTerraformCommand (command , company , product , provider , infratype string ) {
105
+ // Validate required flags
94
106
if command == "" || company == "" || product == "" || provider == "" {
95
107
fmt .Println ("Error: --command, --company, --product, and --provider are required" )
96
108
os .Exit (1 )
97
109
}
98
110
99
- // 'init', 'build', and 'print' commands require '-- infratype'
100
- if (command == "init" || command == "build" || command == "print" ) && infratype == "" {
101
- fmt .Println ("Error: --infratype is required for 'init', 'build', and 'print ' commands" )
111
+ // Certain commands require 'infratype'
112
+ if (command == "init" || command == "build" || command == "print" || command == "validate" ) && infratype == "" {
113
+ fmt .Println ("Error: --infratype is required for 'init', 'build', 'print', and 'validate ' commands" )
102
114
os .Exit (1 )
103
115
}
104
116
117
+ // Handle 'print' command separately
105
118
if command == "print" {
106
119
printTerraformCommands (command , company , product , provider , infratype )
107
120
} else {
121
+ // Execute other Terraform commands
108
122
if err := runTerraformCommand (command , company , product , provider , infratype ); err != nil {
109
123
log .Fatalf ("Error executing Terraform command: %v\n " , err )
110
124
}
111
125
}
112
126
}
113
127
128
+ // runTerraformCommand executes the specified Terraform command
114
129
func runTerraformCommand (command , company , product , provider , infratype string ) error {
115
130
terraformDir := filepath .Join ("output" , "terraform" , company , product )
116
131
if _ , err := os .Stat (terraformDir ); os .IsNotExist (err ) {
117
132
return fmt .Errorf ("Terraform directory %s does not exist" , terraformDir )
118
133
}
119
134
135
+ // Change to the Terraform directory
120
136
if err := os .Chdir (terraformDir ); err != nil {
121
137
return fmt .Errorf ("error changing directory to %s: %v" , terraformDir , err )
122
138
}
123
139
124
140
switch command {
125
141
case "init" :
126
- backendConfig := fmt .Sprintf ("backend/%s_%s.tfvars" , product , strings .ToLower (infratype ))
127
- args := []string {"init" , "-no-color" , "-get=true" , "-force-copy" , fmt .Sprintf ("-backend-config=%s" , backendConfig )}
142
+ // Removed -backend-config
143
+ args := []string {"init" , "-no-color" , "-get=true" , "-force-copy" }
144
+ return executeCommand ("terraform" , args )
145
+
146
+ case "validate" :
147
+ args := []string {"validate" , "-no-color" }
128
148
return executeCommand ("terraform" , args )
129
149
130
150
case "plan" :
@@ -135,10 +155,15 @@ func runTerraformCommand(command, company, product, provider, infratype string)
135
155
args := []string {"apply" , "-no-color" , "-input=false" , "-auto-approve=true" , "-lock=true" , "-lock-timeout=7200s" , "-refresh=true" , "-var-file=./vars.tfvars" }
136
156
return executeCommand ("terraform" , args )
137
157
158
+ case "destroy" :
159
+ args := []string {"destroy" , "-no-color" , "-auto-approve=true" , "-var-file=./vars.tfvars" }
160
+ return executeCommand ("terraform" , args )
161
+
138
162
case "build" :
139
- backendConfig := fmt . Sprintf ( "backend/%s_%s.tfvars" , product , strings . ToLower ( infratype ))
163
+ // Sequentially execute init, validate, plan, and apply
140
164
buildCommands := [][]string {
141
- {"init" , "-no-color" , "-get=true" , "-force-copy" , fmt .Sprintf ("-backend-config=%s" , backendConfig )},
165
+ {"init" , "-no-color" , "-get=true" , "-force-copy" },
166
+ {"validate" , "-no-color" },
142
167
{"plan" , "-no-color" , "-input=false" , "-lock=true" , "-refresh=true" , "-var-file=./vars.tfvars" },
143
168
{"apply" , "-no-color" , "-input=false" , "-auto-approve=true" , "-lock=true" , "-lock-timeout=7200s" , "-refresh=true" , "-var-file=./vars.tfvars" },
144
169
}
@@ -156,16 +181,33 @@ func runTerraformCommand(command, company, product, provider, infratype string)
156
181
return nil
157
182
}
158
183
184
+ // printTerraformCommands prints the Terraform commands without executing them
159
185
func printTerraformCommands (command , company , product , provider , infratype string ) {
160
186
terraformDir := filepath .Join ("output" , "terraform" , company , product )
161
187
fmt .Printf ("Working directory: %s\n " , terraformDir )
162
188
163
- backendConfig := fmt .Sprintf ("backend/%s_%s.tfvars" , product , strings .ToLower (infratype ))
164
- fmt .Printf ("terraform init -no-color -get=true -force-copy -backend-config=%s\n " , backendConfig )
165
- fmt .Println ("terraform plan -no-color -input=false -lock=true -refresh=true -var-file=./vars.tfvars" )
166
- fmt .Println ("terraform apply -no-color -input=false -auto-approve=true -lock=true -lock-timeout=7200s -refresh=true -var-file=./vars.tfvars" )
189
+ switch command {
190
+ case "init" :
191
+ fmt .Println ("terraform init -no-color -get=true -force-copy" )
192
+ case "validate" :
193
+ fmt .Println ("terraform validate -no-color" )
194
+ case "plan" :
195
+ fmt .Println ("terraform plan -no-color -input=false -lock=true -refresh=true -var-file=./vars.tfvars" )
196
+ case "apply" :
197
+ fmt .Println ("terraform apply -no-color -input=false -auto-approve=true -lock=true -lock-timeout=7200s -refresh=true -var-file=./vars.tfvars" )
198
+ case "destroy" :
199
+ fmt .Println ("terraform destroy -no-color -auto-approve=true -var-file=./vars.tfvars" )
200
+ case "build" :
201
+ fmt .Println ("terraform init -no-color -get=true -force-copy" )
202
+ fmt .Println ("terraform validate -no-color" )
203
+ fmt .Println ("terraform plan -no-color -input=false -lock=true -refresh=true -var-file=./vars.tfvars" )
204
+ fmt .Println ("terraform apply -no-color -input=false -auto-approve=true -lock=true -lock-timeout=7200s -refresh=true -var-file=./vars.tfvars" )
205
+ default :
206
+ fmt .Printf ("Unsupported command: %s\n " , command )
207
+ }
167
208
}
168
209
210
+ // executeCommand runs a shell command and streams its output
169
211
func executeCommand (command string , args []string ) error {
170
212
cmd := exec .Command (command , args ... )
171
213
cmd .Stdout = os .Stdout
0 commit comments