From adba32abfba52292765139aa8151f63e08f46603 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 | 24 +++++++++++++++++------- tfinstall/{ => gitref}/git_ref_test.go | 10 ++++++++-- 4 files changed, 29 insertions(+), 11 deletions(-) rename tfinstall/{ => gitref}/git_ref.go (78%) rename tfinstall/{ => gitref}/git_ref_test.go (85%) 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 78% rename from tfinstall/git_ref.go rename to tfinstall/gitref/git_ref.go index 71149bd4..0dc01170 100644 --- a/tfinstall/git_ref.go +++ b/tfinstall/gitref/git_ref.go @@ -1,4 +1,4 @@ -package tfinstall +package gitref import ( "context" @@ -14,23 +14,21 @@ import ( "github.com/go-git/go-git/v5/plumbing" ) -type GitRefOption struct { +type Option struct { installDir string repoURL string ref string } -var _ ExecPathFinder = &GitRefOption{} - -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 +89,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 85% rename from tfinstall/git_ref_test.go rename to tfinstall/gitref/git_ref_test.go index a49fc975..7e5bf149 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,8 +7,14 @@ import ( "os/exec" "strings" "testing" + + "github.com/hashicorp/terraform-exec/tfinstall" + "github.com/hashicorp/terraform-exec/tfinstall/gitref" ) +// ensure the option satisfies the interface +var _ tfinstall.ExecPathFinder = &gitref.Option{} + func TestGitRef(t *testing.T) { if testing.Short() { t.Skip("skipping git ref tests for short run") @@ -49,7 +55,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) }