Skip to content

Commit

Permalink
Move gitref to sub package
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Oct 7, 2020
1 parent 729720f commit adba32a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cmd/tfinstall/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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:]
Expand Down
3 changes: 2 additions & 1 deletion tfexec/internal/testutil/tfcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/hashicorp/terraform-exec/tfinstall"
"github.com/hashicorp/terraform-exec/tfinstall/gitref"
)

const (
Expand Down Expand Up @@ -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)
})
}

Expand Down
24 changes: 17 additions & 7 deletions tfinstall/git_ref.go → tfinstall/gitref/git_ref.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tfinstall
package gitref

import (
"context"
Expand All @@ -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
Expand Down Expand Up @@ -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
}
10 changes: 8 additions & 2 deletions tfinstall/git_ref_test.go → tfinstall/gitref/git_ref_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tfinstall
package gitref_test

import (
"context"
Expand All @@ -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")
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit adba32a

Please sign in to comment.