Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'offline mode': do not download terraform binary #124

Merged
merged 5 commits into from Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/cmd/generate.go
Expand Up @@ -11,6 +11,7 @@ type generateCmd struct {
commonCmd

flagLegacySidebar bool
offline bool
}

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.BoolVar(&cmd.offline, "offline", false, "do not download terraform binary and find one in PATH")
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.offline)
if err != nil {
return fmt.Errorf("unable to generate website: %w", err)
}
Expand Down
32 changes: 25 additions & 7 deletions internal/provider/generate.go
Expand Up @@ -11,13 +11,18 @@ import (
"strings"

"github.com/hashicorp/go-version"
install "github.com/hashicorp/hc-install"
"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"
)

const defaultTerraformVersion = "1.0.5"

GennadySpb marked this conversation as resolved.
Show resolved Hide resolved
// TODO: convert these to flags?
var (
providerName string
Expand Down Expand Up @@ -74,6 +79,7 @@ var (

type generator struct {
legacySidebar bool
offlineMode bool

ui cli.Ui
}
Expand All @@ -86,9 +92,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, offline bool) error {
g := &generator{
legacySidebar: legacySidebar,
offlineMode: offline,

ui: ui,
}
Expand Down Expand Up @@ -498,13 +505,24 @@ 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()
src := []src.Source{}

if g.offlineMode {
g.infof("use Terraform binary from PATH")
src = append(src, &fs.AnyVersion{
Product: &product.Terraform,
})
} else {
g.infof("getting Terraform binary")
src = append(src, &releases.ExactVersion{
Product: product.Terraform,
Version: version.Must(version.NewVersion(defaultTerraformVersion)),
InstallDir: tmpDir,
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recommend switching this to checkpoint.LatestVersion, instead of keeping the hardcoded Terraform version.

}
tfBin, err := installer.Install(context.Background())

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