From 538616cfa3add184601aa28f34abc93a3bb6505c Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Tue, 6 Oct 2020 12:29:46 -0400 Subject: [PATCH] Move gitref to sub package --- cmd/tfinstall/main.go | 3 ++- tfexec/internal/testutil/tfcache.go | 3 ++- tfinstall/{ => gitref}/git_ref.go | 26 ++++++++++++++++++++------ tfinstall/{ => gitref}/git_ref_test.go | 7 +++++-- 4 files changed, 29 insertions(+), 10 deletions(-) rename tfinstall/{ => gitref}/git_ref.go (75%) rename tfinstall/{ => gitref}/git_ref_test.go (89%) diff --git a/cmd/tfinstall/main.go b/cmd/tfinstall/main.go index 1c33f27c..8b7d934e 100644 --- a/cmd/tfinstall/main.go +++ b/cmd/tfinstall/main.go @@ -12,6 +12,7 @@ import ( "github.com/mitchellh/cli" "github.com/hashicorp/terraform-exec/tfinstall" + "github.com/hashicorp/terraform-exec/tfinstall/gitref" ) // TODO: add versioning to this? @@ -110,7 +111,7 @@ func run(ui cli.Ui, args []string) int { finder.UserAgent = userAgentAppend findArgs = append(findArgs, finder) case strings.HasPrefix(tfVersion, "refs/"): - findArgs = append(findArgs, tfinstall.GitRef(tfVersion, "", tfDir)) + findArgs = append(findArgs, gitref.Install(tfVersion, "", tfDir)) default: if strings.HasPrefix(tfVersion, "v") { tfVersion = tfVersion[1:] diff --git a/tfexec/internal/testutil/tfcache.go b/tfexec/internal/testutil/tfcache.go index 3826a4ea..38e03de3 100644 --- a/tfexec/internal/testutil/tfcache.go +++ b/tfexec/internal/testutil/tfcache.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/hashicorp/terraform-exec/tfinstall" + "github.com/hashicorp/terraform-exec/tfinstall/gitref" ) const ( @@ -32,7 +33,7 @@ func NewTFCache(dir string) *TFCache { func (tf *TFCache) GitRef(t *testing.T, ref string) string { t.Helper() return tf.find(t, "gitref:"+ref, func(dir string) tfinstall.ExecPathFinder { - return tfinstall.GitRef(ref, "", dir) + return gitref.Install(ref, "", dir) }) } diff --git a/tfinstall/git_ref.go b/tfinstall/gitref/git_ref.go similarity index 75% rename from tfinstall/git_ref.go rename to tfinstall/gitref/git_ref.go index 71149bd4..2ea931de 100644 --- a/tfinstall/git_ref.go +++ b/tfinstall/gitref/git_ref.go @@ -1,4 +1,4 @@ -package tfinstall +package gitref import ( "context" @@ -12,25 +12,27 @@ import ( "github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5/plumbing" + + "github.com/hashicorp/terraform-exec/tfinstall" ) -type GitRefOption struct { +type Option struct { installDir string repoURL string ref string } -var _ ExecPathFinder = &GitRefOption{} +var _ tfinstall.ExecPathFinder = &Option{} -func GitRef(ref, repo, installDir string) *GitRefOption { - return &GitRefOption{ +func Install(ref, repo, installDir string) *Option { + return &Option{ installDir: installDir, repoURL: repo, ref: ref, } } -func (opt *GitRefOption) ExecPath(ctx context.Context) (string, error) { +func (opt *Option) ExecPath(ctx context.Context) (string, error) { installDir, err := ensureInstallDir(opt.installDir) if err != nil { return "", err @@ -91,3 +93,15 @@ func (opt *GitRefOption) ExecPath(ctx context.Context) (string, error) { return binName, nil } + +func ensureInstallDir(installDir string) (string, error) { + if installDir == "" { + return ioutil.TempDir("", "tfexec") + } + + if _, err := os.Stat(installDir); err != nil { + return "", fmt.Errorf("could not access directory %s for installing Terraform: %w", installDir, err) + } + + return installDir, nil +} diff --git a/tfinstall/git_ref_test.go b/tfinstall/gitref/git_ref_test.go similarity index 89% rename from tfinstall/git_ref_test.go rename to tfinstall/gitref/git_ref_test.go index a49fc975..57d17fbe 100644 --- a/tfinstall/git_ref_test.go +++ b/tfinstall/gitref/git_ref_test.go @@ -1,4 +1,4 @@ -package tfinstall +package gitref_test import ( "context" @@ -7,6 +7,9 @@ import ( "os/exec" "strings" "testing" + + "github.com/hashicorp/terraform-exec/tfinstall" + "github.com/hashicorp/terraform-exec/tfinstall/gitref" ) func TestGitRef(t *testing.T) { @@ -49,7 +52,7 @@ func TestGitRef(t *testing.T) { }) t.Logf("finding / building ref %q...", c.ref) - tfpath, err := Find(ctx, GitRef(c.ref, "", tmpDir)) + tfpath, err := tfinstall.Find(ctx, gitref.Install(c.ref, "", tmpDir)) if err != nil { t.Fatalf("%T %s", err, err) }