Skip to content

Commit

Permalink
Add support for refs in the tfinstall CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Sep 11, 2020
1 parent 2b6dd05 commit c548698
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
44 changes: 34 additions & 10 deletions cmd/tfinstall/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ import (
"io/ioutil"
"log"
"os"
"strings"

"github.com/hashicorp/logutils"
"github.com/hashicorp/terraform-exec/tfinstall"
"github.com/mitchellh/cli"

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

// TODO: add versioning to this?
const userAgentAppend = "tfinstall-cli"

func main() {
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "WARN", "ERROR"},
Expand All @@ -36,11 +41,17 @@ func main() {
}

func help() string {
return `Usage: tfinstall [--dir=DIR] VERSION
return `Usage: tfinstall [--dir=DIR] VERSION-OR-REF
Downloads, verifies, and installs a terraform binary of version
VERSION from releases.hashicorp.com. VERSION must be a valid version under
semantic versioning, or "latest".
Downloads, verifies, and installs a official releases of the Terraform binary
from releases.hashicorp.com or downloads, compiles, and installs a version of
the Terraform binary from the GitHub repository.
To download an official release, pass "latest" or a valid semantic versioning
version string.
To download and compile a version of the Terraform binary from the GitHub
repository pass a ref in the form "refs/...", some examples are shown below.
If a binary is successfully installed, its path will be printed to stdout.
Expand All @@ -55,6 +66,9 @@ Examples:
tfinstall latest
tfinstall 0.13.0-beta3
tfinstall --dir=/home/kmoe/bin 0.12.28
tfinstall refs/heads/master
tfinstall refs/tags/v0.12.29
tfinstall refs/pull/25633/head
`
}

Expand All @@ -73,7 +87,7 @@ func run(ui cli.Ui, args []string) int {
}

if flags.NArg() != 1 {
ui.Error("Please specify VERSION")
ui.Error("Please specify VERSION-OR-REF")
ui.Output(help())
return 127
}
Expand All @@ -90,10 +104,20 @@ func run(ui cli.Ui, args []string) int {

var findArgs []tfinstall.ExecPathFinder

if tfVersion == "latest" {
findArgs = append(findArgs, tfinstall.LatestVersion(tfDir, false))
} else {
findArgs = append(findArgs, tfinstall.ExactVersion(tfVersion, tfDir))
switch {
case tfVersion == "latest":
finder := tfinstall.LatestVersion(tfDir, false)
finder.UserAgent = userAgentAppend
findArgs = append(findArgs, finder)
case strings.HasPrefix(tfVersion, "refs/"):
findArgs = append(findArgs, tfinstall.GitRef(tfVersion, "", tfDir))
default:
if strings.HasPrefix(tfVersion, "v") {
tfVersion = tfVersion[1:]
}
finder := tfinstall.ExactVersion(tfVersion, tfDir)
finder.UserAgent = userAgentAppend
findArgs = append(findArgs, finder)
}

tfPath, err := tfinstall.Find(ctx, findArgs...)
Expand Down
4 changes: 3 additions & 1 deletion tfinstall/git_ref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ func TestGitRef(t *testing.T) {
}{
"branch v0.12": {"Terraform v0.12", "refs/heads/v0.12"},
"tag v0.12.29": {"Terraform v0.12.29", "refs/tags/v0.12.29"},
// https://github.com/hashicorp/terraform/pull/25633
"PR 25633": {"Terraform v0.12.29-dev", "refs/pull/25633/head"},
//"commit 83630a7": {"Terraform v0.12.29", "83630a7003fb8b868a3bf940798326634c3c6acc"},
"empty": {"Terraform v0.14.", ""}, // should pull master, which is currently 0.13
"empty": {"Terraform v0.14.", ""}, // should pull master, which is currently 0.14 dev
} {
c := c
t.Run(n, func(t *testing.T) {
Expand Down

0 comments on commit c548698

Please sign in to comment.