-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce tfexec.Get() for downloading modules (#176)
- Loading branch information
1 parent
0c7451a
commit 3211ef7
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters