From 3232a22bd1ad73fe3dfe34117b8d99ad39ac911c Mon Sep 17 00:00:00 2001 From: GennadySpb Date: Tue, 15 Mar 2022 20:29:45 +0300 Subject: [PATCH] Use Existing Terraform CLI binary, otherwise download latest (#124) 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. --- go.mod | 2 ++ go.sum | 2 ++ internal/cmd/generate.go | 4 +++- internal/provider/generate.go | 38 ++++++++++++++++++++++++++++------- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 833aa372..99bf8ff8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 85b7fb68..d36d807b 100644 --- a/go.sum +++ b/go.sum @@ -50,6 +50,7 @@ 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= @@ -57,6 +58,7 @@ github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/S 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= diff --git a/internal/cmd/generate.go b/internal/cmd/generate.go index a653253c..98b6b32a 100644 --- a/internal/cmd/generate.go +++ b/internal/cmd/generate.go @@ -11,6 +11,7 @@ type generateCmd struct { commonCmd flagLegacySidebar bool + tfVersion string } func (cmd *generateCmd) Synopsis() string { @@ -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 } @@ -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) } diff --git a/internal/provider/generate.go b/internal/provider/generate.go index 07a4b1c8..f7a27396 100644 --- a/internal/provider/generate.go +++ b/internal/provider/generate.go @@ -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" @@ -74,6 +78,7 @@ var ( type generator struct { legacySidebar bool + tfVersion string ui cli.Ui } @@ -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, } @@ -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 }