Skip to content

Commit

Permalink
Use Existing Terraform CLI binary, otherwise download latest (#124)
Browse files Browse the repository at this point in the history
Check for a Terraform binary first, then fall back to downloading the latest version; also support set exact version to download via new `tf-version` command flag.
  • Loading branch information
GennadySpb committed Mar 15, 2022
1 parent 7e2146c commit 3232a22
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
2 changes: 2 additions & 0 deletions go.mod
Expand Up @@ -23,8 +23,10 @@ require (
github.com/fatih/color v1.7.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.0 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -50,13 +50,15 @@ github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-checkpoint v0.5.0 h1:MFYpPZCnQqQTE18jFwSII6eUQrD/oxMFp3mlgcqk5mU=
github.com/hashicorp/go-checkpoint v0.5.0/go.mod h1:7nfLNL10NsxqO4iWuW6tWW0HjZuDrwkBuEQsVcpCOgg=
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-uuid v1.0.0 h1:RS8zrF7PhGwyNPOtxSClXXj9HA8feRnJzgnI1RJCSnM=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-version v1.3.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/go-version v1.4.0 h1:aAQzgqIrRKRa7w75CKpbBxYsmUoPjzVm1W59ca1L0J4=
Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/generate.go
Expand Up @@ -11,6 +11,7 @@ type generateCmd struct {
commonCmd

flagLegacySidebar bool
tfVersion string
}

func (cmd *generateCmd) Synopsis() string {
Expand All @@ -24,6 +25,7 @@ func (cmd *generateCmd) Help() string {
func (cmd *generateCmd) Flags() *flag.FlagSet {
fs := flag.NewFlagSet("generate", flag.ExitOnError)
fs.BoolVar(&cmd.flagLegacySidebar, "legacy-sidebar", false, "generate the legacy .erb sidebar file")
fs.StringVar(&cmd.tfVersion, "tf-version", "", "terraform binary version to download")
return fs
}

Expand All @@ -39,7 +41,7 @@ func (cmd *generateCmd) Run(args []string) int {
}

func (cmd *generateCmd) runInternal() error {
err := provider.Generate(cmd.ui, cmd.flagLegacySidebar)
err := provider.Generate(cmd.ui, cmd.flagLegacySidebar, cmd.tfVersion)
if err != nil {
return fmt.Errorf("unable to generate website: %w", err)
}
Expand Down
38 changes: 31 additions & 7 deletions internal/provider/generate.go
Expand Up @@ -11,8 +11,12 @@ import (
"strings"

"github.com/hashicorp/go-version"
install "github.com/hashicorp/hc-install"
"github.com/hashicorp/hc-install/checkpoint"
"github.com/hashicorp/hc-install/fs"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hc-install/src"
"github.com/hashicorp/terraform-exec/tfexec"
tfjson "github.com/hashicorp/terraform-json"
"github.com/mitchellh/cli"
Expand Down Expand Up @@ -74,6 +78,7 @@ var (

type generator struct {
legacySidebar bool
tfVersion string

ui cli.Ui
}
Expand All @@ -86,9 +91,10 @@ func (g *generator) warnf(format string, a ...interface{}) {
g.ui.Warn(fmt.Sprintf(format, a...))
}

func Generate(ui cli.Ui, legacySidebar bool) error {
func Generate(ui cli.Ui, legacySidebar bool, tfVersion string) error {
g := &generator{
legacySidebar: legacySidebar,
tfVersion: tfVersion,

ui: ui,
}
Expand Down Expand Up @@ -498,13 +504,31 @@ provider %[1]q {
return nil, err
}

g.infof("getting Terraform binary")
installer := &releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion("1.0.5")),
InstallDir: tmpDir,
i := install.NewInstaller()
sources := []src.Source{}
if g.tfVersion != "" {
g.infof("downloading Terraform CLI binary version from releases.hashicorp.com: %s", g.tfVersion)
sources = []src.Source{
&releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion(g.tfVersion)),
InstallDir: tmpDir,
},
}
} else {
g.infof("using Terraform CLI binary from PATH if available, otherwise downloading latest Terraform CLI binary")
sources = []src.Source{
&fs.AnyVersion{
Product: &product.Terraform,
},
&checkpoint.LatestVersion{
InstallDir: tmpDir,
Product: product.Terraform,
},
}
}
tfBin, err := installer.Install(context.Background())

tfBin, err := i.Ensure(context.Background(), sources)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 3232a22

Please sign in to comment.