@@ -6,6 +6,7 @@ package tfexec
6
6
import (
7
7
"context"
8
8
"fmt"
9
+ "io"
9
10
"os/exec"
10
11
)
11
12
@@ -99,6 +100,21 @@ func (opt *VerifyPluginsOption) configureInit(conf *initConfig) {
99
100
conf .verifyPlugins = opt .verifyPlugins
100
101
}
101
102
103
+ func (tf * Terraform ) configureInitOptions (ctx context.Context , c * initConfig , opts ... InitOption ) error {
104
+ for _ , o := range opts {
105
+ switch o .(type ) {
106
+ case * LockOption , * LockTimeoutOption , * VerifyPluginsOption , * GetPluginsOption :
107
+ err := tf .compatible (ctx , nil , tf0_15_0 )
108
+ if err != nil {
109
+ return fmt .Errorf ("-lock, -lock-timeout, -verify-plugins, and -get-plugins options are no longer available as of Terraform 0.15: %w" , err )
110
+ }
111
+ }
112
+
113
+ o .configureInit (c )
114
+ }
115
+ return nil
116
+ }
117
+
102
118
// Init represents the terraform init subcommand.
103
119
func (tf * Terraform ) Init (ctx context.Context , opts ... InitOption ) error {
104
120
cmd , err := tf .initCmd (ctx , opts ... )
@@ -108,21 +124,71 @@ func (tf *Terraform) Init(ctx context.Context, opts ...InitOption) error {
108
124
return tf .runTerraformCmd (ctx , cmd )
109
125
}
110
126
127
+ // InitJSON represents the terraform init subcommand with the `-json` flag.
128
+ // Using the `-json` flag will result in
129
+ // [machine-readable](https://developer.hashicorp.com/terraform/internals/machine-readable-ui)
130
+ // JSON being written to the supplied `io.Writer`.
131
+ func (tf * Terraform ) InitJSON (ctx context.Context , w io.Writer , opts ... InitOption ) error {
132
+ err := tf .compatible (ctx , tf1_9_0 , nil )
133
+ if err != nil {
134
+ return fmt .Errorf ("terraform init -json was added in 1.9.0: %w" , err )
135
+ }
136
+
137
+ tf .SetStdout (w )
138
+
139
+ cmd , err := tf .initJSONCmd (ctx , opts ... )
140
+ if err != nil {
141
+ return err
142
+ }
143
+
144
+ return tf .runTerraformCmd (ctx , cmd )
145
+ }
146
+
111
147
func (tf * Terraform ) initCmd (ctx context.Context , opts ... InitOption ) (* exec.Cmd , error ) {
112
148
c := defaultInitOptions
113
149
114
- for _ , o := range opts {
115
- switch o .(type ) {
116
- case * LockOption , * LockTimeoutOption , * VerifyPluginsOption , * GetPluginsOption :
117
- err := tf .compatible (ctx , nil , tf0_15_0 )
118
- if err != nil {
119
- return nil , fmt .Errorf ("-lock, -lock-timeout, -verify-plugins, and -get-plugins options are no longer available as of Terraform 0.15: %w" , err )
120
- }
121
- }
150
+ err := tf .configureInitOptions (ctx , & c , opts ... )
151
+ if err != nil {
152
+ return nil , err
153
+ }
154
+
155
+ args , err := tf .buildInitArgs (ctx , c )
156
+ if err != nil {
157
+ return nil , err
158
+ }
159
+
160
+ // Optional positional argument; must be last as flags precede positional arguments.
161
+ if c .dir != "" {
162
+ args = append (args , c .dir )
163
+ }
164
+
165
+ return tf .buildInitCmd (ctx , c , args )
166
+ }
167
+
168
+ func (tf * Terraform ) initJSONCmd (ctx context.Context , opts ... InitOption ) (* exec.Cmd , error ) {
169
+ c := defaultInitOptions
170
+
171
+ err := tf .configureInitOptions (ctx , & c , opts ... )
172
+ if err != nil {
173
+ return nil , err
174
+ }
122
175
123
- o .configureInit (& c )
176
+ args , err := tf .buildInitArgs (ctx , c )
177
+ if err != nil {
178
+ return nil , err
179
+ }
180
+
181
+ args = append (args , "-json" )
182
+
183
+ // Optional positional argument; must be last as flags precede positional arguments.
184
+ if c .dir != "" {
185
+ args = append (args , c .dir )
124
186
}
125
187
188
+ return tf .buildInitCmd (ctx , c , args )
189
+ }
190
+
191
+ func (tf * Terraform ) buildInitArgs (ctx context.Context , c initConfig ) ([]string , error ) {
126
192
args := []string {"init" , "-no-color" , "-input=false" }
127
193
128
194
// string opts: only pass if set
@@ -172,11 +238,10 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) (*exec.Cmd
172
238
}
173
239
}
174
240
175
- // optional positional argument
176
- if c .dir != "" {
177
- args = append (args , c .dir )
178
- }
241
+ return args , nil
242
+ }
179
243
244
+ func (tf * Terraform ) buildInitCmd (ctx context.Context , c initConfig , args []string ) (* exec.Cmd , error ) {
180
245
mergeEnv := map [string ]string {}
181
246
if c .reattachInfo != nil {
182
247
reattachStr , err := c .reattachInfo .marshalString ()
0 commit comments