-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for finding Terraform by git ref
- Loading branch information
Showing
4 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package tfinstall | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
"path/filepath" | ||
"runtime" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing" | ||
) | ||
|
||
type GitRefOption struct { | ||
installDir string | ||
repoURL string | ||
ref string | ||
} | ||
|
||
var _ ExecPathFinder = &GitRefOption{} | ||
|
||
func GitRef(installDir, ref, repo string) *GitRefOption { | ||
return &GitRefOption{ | ||
installDir: installDir, | ||
repoURL: repo, | ||
ref: ref, | ||
} | ||
} | ||
|
||
func (opt *GitRefOption) ExecPath(ctx context.Context) (string, error) { | ||
installDir, err := ensureInstallDir(opt.installDir) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
ref := plumbing.ReferenceName(opt.ref) | ||
if opt.ref == "" { | ||
ref = plumbing.ReferenceName("refs/heads/master") | ||
} | ||
|
||
repoURL := opt.repoURL | ||
if repoURL == "" { | ||
repoURL = "https://github.com/hashicorp/terraform.git" | ||
} | ||
|
||
_, err = git.PlainClone(installDir, false, &git.CloneOptions{ | ||
URL: repoURL, | ||
ReferenceName: ref, | ||
|
||
Depth: 1, | ||
Tags: git.NoTags, | ||
}) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to clone %q: %w", repoURL, err) | ||
} | ||
|
||
binName := "terraform" | ||
if runtime.GOOS == "windows" { | ||
binName = "terraform.exe" | ||
} | ||
|
||
cmd := exec.CommandContext(ctx, "go", "build", "-mod", "vendor", "-o", binName) | ||
cmd.Dir = installDir | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
return "", fmt.Errorf("unable to build Terraform: %w\n%s", err, out) | ||
} | ||
|
||
return filepath.Join(installDir, binName), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package tfinstall | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGitRef(t *testing.T) { | ||
for n, c := range map[string]struct { | ||
expectedVersion string | ||
ref string | ||
}{ | ||
"branch v0.12": {"Terraform v0.12", "refs/heads/v0.12"}, | ||
"tag v0.12.29": {"Terraform v0.12.29", "refs/tags/v0.12.29"}, | ||
//"commit 83630a7": {"Terraform v0.12.29", "83630a7003fb8b868a3bf940798326634c3c6acc"}, | ||
"empty": {"Terraform v0.13.", ""}, // should pull master, which is currently 0.13 | ||
} { | ||
c := c | ||
t.Run(n, func(t *testing.T) { | ||
// these are really long running due to the compilation, run them in parallel | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
|
||
tmpDir, err := ioutil.TempDir("", "tfinstall-test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Cleanup(func() { | ||
os.RemoveAll(tmpDir) | ||
}) | ||
|
||
t.Logf("finding / building ref %q...", c.ref) | ||
tfpath, err := Find(ctx, GitRef(tmpDir, c.ref, "")) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Logf("testing version cmd...") | ||
cmd := exec.Command(tfpath, "version") | ||
|
||
out, err := cmd.Output() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
actual := string(out) | ||
if !strings.Contains(actual, c.expectedVersion) { | ||
t.Fatalf("ran terraform version, expected %s, but got %s", c.expectedVersion, actual) | ||
} | ||
}) | ||
} | ||
} |