Skip to content

Commit

Permalink
Introduce tfexec.Get() for downloading modules (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko authored Jun 9, 2021
1 parent 0c7451a commit 3211ef7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
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 @@ -290,6 +290,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

0 comments on commit 3211ef7

Please sign in to comment.