@@ -23,10 +23,11 @@ func main() {
23
23
customers := generateCmd .String ("customers" , "" , "Comma-separated list of customers" )
24
24
25
25
terraformCmd := flag .NewFlagSet ("terraform" , flag .ExitOnError )
26
- tfCommand := terraformCmd .String ("command" , "" , "Terraform command to execute (init, plan, apply, build)" )
26
+ tfCommand := terraformCmd .String ("command" , "" , "Terraform command to execute (init, plan, apply, build, print )" )
27
27
tfCompany := terraformCmd .String ("company" , "" , "Company name (required)" )
28
28
tfProduct := terraformCmd .String ("product" , "" , "Product name (required)" )
29
29
tfProvider := terraformCmd .String ("provider" , "" , "Provider name (required)" )
30
+ tfInfratype := terraformCmd .String ("infratype" , "" , "Infrastructure type (prod or nonprod)" )
30
31
31
32
if len (os .Args ) < 2 {
32
33
fmt .Println ("Expected 'generate' or 'terraform' subcommands" )
@@ -37,61 +38,13 @@ func main() {
37
38
case "generate" :
38
39
generateCmd .Parse (os .Args [2 :])
39
40
if generateCmd .Parsed () {
40
- if * company == "" || * product == "" || * provider == "" {
41
- fmt .Println ("Error: --company, --product, and --provider are required" )
42
- generateCmd .Usage ()
43
- os .Exit (1 )
44
- }
45
-
46
- // Prepare the request data
47
- req := models.GenerateRequest {
48
- OrganisationName : * company ,
49
- ProductName : * product ,
50
- Provider : * provider ,
51
- Modules : []string {},
52
- }
53
-
54
- // Handle modules
55
- if * modules != "" {
56
- moduleNames := strings .Split (* modules , "," )
57
- for _ , moduleName := range moduleNames {
58
- moduleName = strings .TrimSpace (moduleName )
59
- req .Modules = append (req .Modules , moduleName )
60
- }
61
- }
62
-
63
- // Handle customers
64
- if * customers != "" {
65
- req .Customers = strings .Split (* customers , "," )
66
- for i , customer := range req .Customers {
67
- req .Customers [i ] = strings .TrimSpace (customer )
68
- }
69
- }
70
-
71
- // Call the generation logic
72
- err := handlers .GenerateTerraform (& req )
73
- if err != nil {
74
- fmt .Printf ("Error generating Terraform code: %v\n " , err )
75
- os .Exit (1 )
76
- }
77
-
78
- fmt .Println ("Terraform code generated successfully" )
41
+ handleGenerateCommand (* company , * product , * provider , * modules , * customers )
79
42
}
80
43
81
44
case "terraform" :
82
45
terraformCmd .Parse (os .Args [2 :])
83
46
if terraformCmd .Parsed () {
84
- if * tfCommand == "" || * tfCompany == "" || * tfProduct == "" || * tfProvider == "" {
85
- fmt .Println ("Error: --command, --company, --product, and --provider are required" )
86
- terraformCmd .Usage ()
87
- os .Exit (1 )
88
- }
89
-
90
- // Run the Terraform command
91
- err := runTerraformCommand (* tfCommand , * tfCompany , * tfProduct , * tfProvider )
92
- if err != nil {
93
- log .Fatalf ("Error executing Terraform command: %v\n " , err )
94
- }
47
+ handleTerraformCommand (* tfCommand , * tfCompany , * tfProduct , * tfProvider , * tfInfratype )
95
48
}
96
49
97
50
default :
@@ -100,45 +53,123 @@ func main() {
100
53
}
101
54
}
102
55
103
- // runTerraformCommand executes Terraform commands (init, plan, apply, build)
104
- func runTerraformCommand (command , company , product , provider string ) error {
105
- // Determine the working directory
56
+ func handleGenerateCommand (company , product , provider , modules , customers string ) {
57
+ if company == "" || product == "" || provider == "" {
58
+ fmt .Println ("Error: --company, --product, and --provider are required" )
59
+ os .Exit (1 )
60
+ }
61
+
62
+ req := models.GenerateRequest {
63
+ OrganisationName : company ,
64
+ ProductName : product ,
65
+ Provider : provider ,
66
+ Modules : []string {},
67
+ }
68
+
69
+ // Handle modules
70
+ if modules != "" {
71
+ moduleNames := strings .Split (modules , "," )
72
+ for _ , moduleName := range moduleNames {
73
+ req .Modules = append (req .Modules , strings .TrimSpace (moduleName ))
74
+ }
75
+ }
76
+
77
+ // Handle customers
78
+ if customers != "" {
79
+ req .Customers = strings .Split (customers , "," )
80
+ for i := range req .Customers {
81
+ req .Customers [i ] = strings .TrimSpace (req .Customers [i ])
82
+ }
83
+ }
84
+
85
+ if err := handlers .GenerateTerraform (& req ); err != nil {
86
+ fmt .Printf ("Error generating Terraform code: %v\n " , err )
87
+ os .Exit (1 )
88
+ }
89
+
90
+ fmt .Println ("Terraform code generated successfully" )
91
+ }
92
+
93
+ func handleTerraformCommand (command , company , product , provider , infratype string ) {
94
+ if command == "" || company == "" || product == "" || provider == "" {
95
+ fmt .Println ("Error: --command, --company, --product, and --provider are required" )
96
+ os .Exit (1 )
97
+ }
98
+
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" )
102
+ os .Exit (1 )
103
+ }
104
+
105
+ if command == "print" {
106
+ printTerraformCommands (command , company , product , provider , infratype )
107
+ } else {
108
+ if err := runTerraformCommand (command , company , product , provider , infratype ); err != nil {
109
+ log .Fatalf ("Error executing Terraform command: %v\n " , err )
110
+ }
111
+ }
112
+ }
113
+
114
+ func runTerraformCommand (command , company , product , provider , infratype string ) error {
106
115
terraformDir := filepath .Join ("output" , "terraform" , company , product )
107
116
if _ , err := os .Stat (terraformDir ); os .IsNotExist (err ) {
108
117
return fmt .Errorf ("Terraform directory %s does not exist" , terraformDir )
109
118
}
110
119
111
- // Change to the Terraform directory
112
120
if err := os .Chdir (terraformDir ); err != nil {
113
121
return fmt .Errorf ("error changing directory to %s: %v" , terraformDir , err )
114
122
}
115
123
116
- // Map of commands to Terraform actions
117
- tfCommands := map [string ][]string {
118
- "init" : {"init" },
119
- "plan" : {"plan" },
120
- "apply" : {"apply" , "-auto-approve" },
121
- "build" : {"init" , "plan" , "apply" , "-auto-approve" },
122
- }
124
+ switch command {
125
+ 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 )}
128
+ return executeCommand ("terraform" , args )
129
+
130
+ case "plan" :
131
+ args := []string {"plan" , "-no-color" , "-input=false" , "-lock=true" , "-refresh=true" , "-var-file=./vars.tfvars" }
132
+ return executeCommand ("terraform" , args )
133
+
134
+ case "apply" :
135
+ args := []string {"apply" , "-no-color" , "-input=false" , "-auto-approve=true" , "-lock=true" , "-lock-timeout=7200s" , "-refresh=true" , "-var-file=./vars.tfvars" }
136
+ return executeCommand ("terraform" , args )
137
+
138
+ case "build" :
139
+ backendConfig := fmt .Sprintf ("backend/%s_%s.tfvars" , product , strings .ToLower (infratype ))
140
+ buildCommands := [][]string {
141
+ {"init" , "-no-color" , "-get=true" , "-force-copy" , fmt .Sprintf ("-backend-config=%s" , backendConfig )},
142
+ {"plan" , "-no-color" , "-input=false" , "-lock=true" , "-refresh=true" , "-var-file=./vars.tfvars" },
143
+ {"apply" , "-no-color" , "-input=false" , "-auto-approve=true" , "-lock=true" , "-lock-timeout=7200s" , "-refresh=true" , "-var-file=./vars.tfvars" },
144
+ }
123
145
124
- // Get the commands for the specified action
125
- actions , ok := tfCommands [command ]
126
- if ! ok {
146
+ for _ , args := range buildCommands {
147
+ if err := executeCommand ("terraform" , args ); err != nil {
148
+ return err
149
+ }
150
+ }
151
+
152
+ default :
127
153
return fmt .Errorf ("unsupported Terraform command: %s" , command )
128
154
}
129
155
130
- // Execute the commands in sequence
131
- for i := 0 ; i < len (actions ); i ++ {
132
- cmd := exec .Command ("terraform" , actions [i ])
133
- cmd .Stdout = os .Stdout
134
- cmd .Stderr = os .Stderr
156
+ return nil
157
+ }
135
158
136
- fmt .Printf ("Running Terraform command: terraform %s\n " , actions [i ])
159
+ func printTerraformCommands (command , company , product , provider , infratype string ) {
160
+ terraformDir := filepath .Join ("output" , "terraform" , company , product )
161
+ fmt .Printf ("Working directory: %s\n " , terraformDir )
137
162
138
- if err := cmd .Run (); err != nil {
139
- return fmt .Errorf ("error running Terraform command '%s': %v" , actions [i ], err )
140
- }
141
- }
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" )
167
+ }
142
168
143
- return nil
169
+ func executeCommand (command string , args []string ) error {
170
+ cmd := exec .Command (command , args ... )
171
+ cmd .Stdout = os .Stdout
172
+ cmd .Stderr = os .Stderr
173
+ fmt .Printf ("Running command: %s %s\n " , command , strings .Join (args , " " ))
174
+ return cmd .Run ()
144
175
}
0 commit comments