Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce tfexec.Get() for downloading modules #176

Merged
merged 1 commit into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions tfexec/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tfexec

import (
"context"
"fmt"
"os/exec"
)

type getCmdConfig struct {
dir string
update bool
}

// GetCmdOption represents options used in the Get method.
type GetCmdOption interface {
configureGet(*getCmdConfig)
}

func (opt *DirOption) configureGet(conf *getCmdConfig) {
conf.dir = opt.path
}

func (opt *UpdateOption) configureGet(conf *getCmdConfig) {
conf.update = opt.update
}

// Get represents the terraform get subcommand.
func (tf *Terraform) Get(ctx context.Context, opts ...GetCmdOption) error {
cmd, err := tf.getCmd(ctx, opts...)
if err != nil {
return err
}
return tf.runTerraformCmd(ctx, cmd)
}

func (tf *Terraform) getCmd(ctx context.Context, opts ...GetCmdOption) (*exec.Cmd, error) {
c := getCmdConfig{}

for _, o := range opts {
o.configureGet(&c)
}

args := []string{"get", "-no-color"}

args = append(args, "-update="+fmt.Sprint(c.update))

if c.dir != "" {
args = append(args, c.dir)
}

return tf.buildTerraformCmd(ctx, nil, args...), nil
}
33 changes: 33 additions & 0 deletions tfexec/get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tfexec

import (
"context"
"testing"

"github.com/hashicorp/terraform-exec/tfexec/internal/testutil"
)

func TestGetCmd(t *testing.T) {
td := testTempDir(t)

tf, err := NewTerraform(td, tfVersion(t, testutil.Latest012))
if err != nil {
t.Fatal(err)
}

// empty env, to avoid environ mismatch in testing
tf.SetEnv(map[string]string{})

t.Run("basic", func(t *testing.T) {
getCmd, err := tf.getCmd(context.Background())
if err != nil {
t.Fatal(err)
}

assertCmd(t, []string{
"get",
"-no-color",
"-update=false",
}, nil, getCmd)
})
}
8 changes: 8 additions & 0 deletions tfexec/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,14 @@ func Target(resource string) *TargetOption {
return &TargetOption{resource}
}

type UpdateOption struct {
update bool
}

func Update(update bool) *UpdateOption {
return &UpdateOption{update}
}

type UpgradeOption struct {
upgrade bool
}
Expand Down