-
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
212 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,84 @@ | ||
package tfinstall | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os/exec" | ||
"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(ref, repo, installDir 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) | ||
} | ||
|
||
var binName string | ||
{ | ||
// TODO: maybe there is a better way to make sure this filename is available? | ||
// I guess we could locate it in a different dir, or nest the git underneath | ||
// the root tmp dir, etc. | ||
binPattern := "terraform" | ||
if runtime.GOOS == "windows" { | ||
binPattern = "terraform*.exe" | ||
} | ||
binFile, err := ioutil.TempFile(installDir, binPattern) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to create bin file: %w", err) | ||
} | ||
binName = binFile.Name() | ||
binFile.Close() | ||
} | ||
|
||
cmd := exec.CommandContext(ctx, "go", "build", "-mod", "vendor", "-o", binName) | ||
cmd.Dir = installDir | ||
out, err := cmd.CombinedOutput() | ||
log.Print(string(out)) | ||
if err != nil { | ||
return "", fmt.Errorf("unable to build Terraform: %w\n%s", err, out) | ||
} | ||
|
||
return 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,65 @@ | ||
package tfinstall | ||
|
||
import ( | ||
"context" | ||
"io/ioutil" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestGitRef(t *testing.T) { | ||
cmd := exec.Command("go", "version") | ||
out, err := cmd.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("error with Go installation: %s\n%s", err, string(out)) | ||
} | ||
t.Logf("go version\n%s", string(out)) | ||
|
||
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.14.", ""}, // 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() | ||
|
||
// hacking this tmpdir to local dir due to circle perms, should be env var | ||
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(c.ref, "", tmpDir)) | ||
if err != nil { | ||
t.Fatalf("%T %s", err, err) | ||
} | ||
|
||
t.Logf("testing version cmd...") | ||
cmd := exec.Command(tfpath, "version") | ||
|
||
out, err := cmd.Output() | ||
if err != nil { | ||
t.Fatalf("%s\n\n%s", err, out) | ||
} | ||
|
||
actual := string(out) | ||
if !strings.Contains(actual, c.expectedVersion) { | ||
t.Fatalf("ran terraform version, expected %s, but got %s", c.expectedVersion, actual) | ||
} | ||
}) | ||
} | ||
} |