diff --git a/tfexec/add.go b/tfexec/add.go deleted file mode 100644 index 0e60034e..00000000 --- a/tfexec/add.go +++ /dev/null @@ -1,101 +0,0 @@ -package tfexec - -import ( - "context" - "fmt" - "os/exec" - "strconv" - "strings" -) - -type addConfig struct { - fromState bool - out string - includeOptional bool - provider string - reattachInfo ReattachInfo -} - -var defaultAddOptions = addConfig{} - -type AddOption interface { - configureAdd(*addConfig) -} - -func (opt *FromStateOption) configureAdd(conf *addConfig) { - conf.fromState = opt.fromState -} - -func (opt *OutOption) configureAdd(conf *addConfig) { - conf.out = opt.path -} - -func (opt *IncludeOptionalOption) configureAdd(conf *addConfig) { - conf.includeOptional = opt.includeOptional -} - -func (opt *ProviderOption) configureAdd(conf *addConfig) { - conf.provider = opt.provider -} - -func (opt *ReattachOption) configureAdd(conf *addConfig) { - conf.reattachInfo = opt.info -} - -// Add represents the `terraform add` subcommand (added in 1.1.0). -// -// Note that this function signature and behaviour is subject -// to breaking changes including removal of that function -// until final 1.1.0 Terraform version (with this command) is released. -func (tf *Terraform) Add(ctx context.Context, address string, opts ...AddOption) (string, error) { - cmd, err := tf.addCmd(ctx, address, opts...) - if err != nil { - return "", err - } - - var outBuf strings.Builder - cmd.Stdout = mergeWriters(cmd.Stdout, &outBuf) - - if err := tf.runTerraformCmd(ctx, cmd); err != nil { - return "", err - } - - return outBuf.String(), nil -} - -func (tf *Terraform) addCmd(ctx context.Context, address string, opts ...AddOption) (*exec.Cmd, error) { - err := tf.compatible(ctx, tf1_1_0, nil) - if err != nil { - return nil, fmt.Errorf("terraform add was added in 1.1.0: %w", err) - } - - c := defaultAddOptions - - for _, o := range opts { - o.configureAdd(&c) - } - - args := []string{"add"} - - args = append(args, "-from-state="+strconv.FormatBool(c.fromState)) - if c.out != "" { - args = append(args, "-out="+c.out) - } - args = append(args, "-optional="+strconv.FormatBool(c.includeOptional)) - if c.provider != "" { - args = append(args, "-provider="+c.provider) - } - - args = append(args, address) - - mergeEnv := map[string]string{} - if c.reattachInfo != nil { - reattachStr, err := c.reattachInfo.marshalString() - if err != nil { - return nil, err - } - mergeEnv[reattachEnvVar] = reattachStr - } - - return tf.buildTerraformCmd(ctx, mergeEnv, args...), nil -} diff --git a/tfexec/add_test.go b/tfexec/add_test.go deleted file mode 100644 index cfe92ae4..00000000 --- a/tfexec/add_test.go +++ /dev/null @@ -1,56 +0,0 @@ -package tfexec - -import ( - "context" - "testing" - - "github.com/hashicorp/terraform-exec/tfexec/internal/testutil" -) - -func TestAddCmd(t *testing.T) { - td := t.TempDir() - - tf, err := NewTerraform(td, tfVersion(t, testutil.Latest_v1_1)) - if err != nil { - t.Fatal(err) - } - - // empty env, to avoid environ mismatch in testing - tf.SetEnv(map[string]string{}) - - t.Run("default", func(t *testing.T) { - addCmd, err := tf.addCmd(context.Background(), "my-addr") - if err != nil { - t.Fatal(err) - } - - assertCmd(t, []string{ - "add", - "-from-state=false", - "-optional=false", - "my-addr", - }, nil, addCmd) - }) - - t.Run("override-default", func(t *testing.T) { - addCmd, err := tf.addCmd(context.Background(), - "my-addr", - FromState(true), - Out("out"), - IncludeOptional(true), - Provider("my-provider"), - ) - if err != nil { - t.Fatal(err) - } - - assertCmd(t, []string{ - "add", - "-from-state=true", - "-out=out", - "-optional=true", - "-provider=my-provider", - "my-addr", - }, nil, addCmd) - }) -} diff --git a/tfexec/options.go b/tfexec/options.go index fb2d5bd7..71638895 100644 --- a/tfexec/options.go +++ b/tfexec/options.go @@ -373,21 +373,3 @@ type VerifyPluginsOption struct { func VerifyPlugins(verifyPlugins bool) *VerifyPluginsOption { return &VerifyPluginsOption{verifyPlugins} } - -// FromStateOption represents the -from-state option of the "terraform add" command. -type FromStateOption struct { - fromState bool -} - -func FromState(fromState bool) *FromStateOption { - return &FromStateOption{fromState} -} - -// IncludeOptionalOption represents the -optional option of the "terraform add" command. -type IncludeOptionalOption struct { - includeOptional bool -} - -func IncludeOptional(includeOptional bool) *IncludeOptionalOption { - return &IncludeOptionalOption{includeOptional} -}