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

Move gitref to sub package #98

Merged
merged 1 commit into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
radeksimko marked this conversation as resolved.
Show resolved Hide resolved

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"
radeksimko marked this conversation as resolved.
Show resolved Hide resolved
)

// 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