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

Support terraform providers mirror subcommand #267

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

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

import (
"context"
"testing"

"github.com/hashicorp/go-version"

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

func TestProvidersMirror(t *testing.T) {
runTestVersions(t, []string{testutil.Latest013, testutil.Latest014, testutil.Latest015, testutil.Latest_v1}, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) {
err := tf.Init(context.Background())
if err != nil {
t.Fatalf("error running Init in test directory: %s", err)
}

err = tf.ProvidersMirror(context.Background(), "targetDir")
if err != nil {
t.Fatalf("error running provider mirror: %s", err)
}
})

}
50 changes: 50 additions & 0 deletions tfexec/providers_mirror.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tfexec

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

type providersMirrorConfig struct {
platforms []string
}

var defaultProviderMirrorOptions = providersMirrorConfig{}

// ProvidersMirrorCmdOption represents options used in the mirror method.
type ProvidersMirrorCmdOption interface {
configureProvidersMirror(*providersMirrorConfig)
}

func (opt *PlatformOption) configureProvidersMirror(conf *providersMirrorConfig) {
conf.platforms = append(conf.platforms, opt.platform)
}

// ProvidersMirror represents the terraform providers mirror subcommand.
func (tf *Terraform) ProvidersMirror(ctx context.Context, targetDir string, opts ...ProvidersMirrorCmdOption) error {
err := tf.compatible(ctx, tf0_13_0, nil)
if err != nil {
return fmt.Errorf("mirror was first introduced in Terraform 0.13.0: %w", err)
}

mirrorCmd := tf.providersMirrorCmd(ctx, targetDir, opts...)

return tf.runTerraformCmd(ctx, mirrorCmd)
}

func (tf *Terraform) providersMirrorCmd(ctx context.Context, targetDir string, opts ...ProvidersMirrorCmdOption) *exec.Cmd {
c := defaultProviderMirrorOptions

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

args := []string{"providers", "mirror", targetDir}

for _, p := range c.platforms {
args = append(args, "-platform="+p)
}

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

import (
"context"
"testing"

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

func TestProvidersMirrorCmd(t *testing.T) {
td := t.TempDir()

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("defaults", func(t *testing.T) {
mirrorCmd := tf.providersMirrorCmd(context.Background(), "foo")

assertCmd(t, []string{
"providers",
"mirror",
"foo",
}, nil, mirrorCmd)
})

t.Run("override all defaults", func(t *testing.T) {
mirrorCmd := tf.providersMirrorCmd(context.Background(), "foo", Platform("linux_amd64"))

assertCmd(t, []string{
"providers",
"mirror",
"foo",
"-platform=linux_amd64",
}, nil, mirrorCmd)
})
}