@@ -6,6 +6,7 @@ package tfexec
66import (
77 "context"
88 "fmt"
9+ "io"
910 "os/exec"
1011)
1112
@@ -99,6 +100,21 @@ func (opt *VerifyPluginsOption) configureInit(conf *initConfig) {
99100 conf .verifyPlugins = opt .verifyPlugins
100101}
101102
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+
102118// Init represents the terraform init subcommand.
103119func (tf * Terraform ) Init (ctx context.Context , opts ... InitOption ) error {
104120 cmd , err := tf .initCmd (ctx , opts ... )
@@ -108,21 +124,71 @@ func (tf *Terraform) Init(ctx context.Context, opts ...InitOption) error {
108124 return tf .runTerraformCmd (ctx , cmd )
109125}
110126
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+
111147func (tf * Terraform ) initCmd (ctx context.Context , opts ... InitOption ) (* exec.Cmd , error ) {
112148 c := defaultInitOptions
113149
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+ }
122175
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 )
124186 }
125187
188+ return tf .buildInitCmd (ctx , c , args )
189+ }
190+
191+ func (tf * Terraform ) buildInitArgs (ctx context.Context , c initConfig ) ([]string , error ) {
126192 args := []string {"init" , "-no-color" , "-input=false" }
127193
128194 // string opts: only pass if set
@@ -172,11 +238,10 @@ func (tf *Terraform) initCmd(ctx context.Context, opts ...InitOption) (*exec.Cmd
172238 }
173239 }
174240
175- // optional positional argument
176- if c .dir != "" {
177- args = append (args , c .dir )
178- }
241+ return args , nil
242+ }
179243
244+ func (tf * Terraform ) buildInitCmd (ctx context.Context , c initConfig , args []string ) (* exec.Cmd , error ) {
180245 mergeEnv := map [string ]string {}
181246 if c .reattachInfo != nil {
182247 reattachStr , err := c .reattachInfo .marshalString ()
0 commit comments