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

Create a constant for the binary file name #162

Closed
wants to merge 1 commit into from

Conversation

sundowndev
Copy link

@sundowndev sundowndev commented Apr 14, 2021

As a user, I'd like to predicate the binary file name tfexec will try to execute. This can be useful when you need to install multiple versions of Terraform at the same time in different directories but install each version only once. If that binary file name might change in the future, I don't want my code to fail because of that. Is that something relevant to you? May be I missed a method to achieve that differently, let me know!

References

@hashicorp-cla
Copy link

hashicorp-cla commented Apr 14, 2021

CLA assistant check
All committers have signed the CLA.

@radeksimko
Copy link
Member

Hi @sundowndev
As described in #109, the tfinstall package will be removed in the next release. We recommend that https://github.com/hashicorp/hc-install be used instead. Let us know what you think of the API.

It does expose the binary name for two products currently (including Terraform), but I would encourage you to perhaps look at the Remove method of the installer instead, which can do the cleanup for you. That way you don't have to deal with any particular binary name in your application and treat it as an implementation detail.

https://pkg.go.dev/github.com/hashicorp/hc-install#Installer.Remove

Here is an example snippet which demonstrates how you can install multiple versions:

package main

import (
	"context"
	"log"
	"os"

	"github.com/hashicorp/go-version"
	"github.com/hashicorp/hc-install"
	"github.com/hashicorp/hc-install/product"
	"github.com/hashicorp/hc-install/releases"
	"github.com/hashicorp/hc-install/src"
	"github.com/hashicorp/terraform-exec/tfexec"
)

func main() {
	tmpDir := os.TempDir()
	err := run(tmpDir)
	if err != nil {
		log.Fatal(err)
	}
}

func run(workDir string) error {
	ctx := context.Background()

	cons, _ := version.NewConstraint(">= 0.12")
	rv := &releases.Versions{
		Product:     product.Terraform,
		Constraints: cons,
	}

	versions, err := rv.List(ctx)
	if err != nil {
		return err
	}

	i := install.NewInstaller()
	for _, installableVersion := range versions {
		ver := installableVersion.(*releases.ExactVersion)
		log.Printf("installing %s...", ver.Version)

		execPath, err := i.Ensure(context.Background(), []src.Source{
			installableVersion,
		})
		if err != nil {
			return err
		}

		log.Printf("version %s installed", ver.Version)

		// Do some testing here
		tf, err := tfexec.NewTerraform(workDir, execPath)
		if err != nil {
			return err
		}
		tfVersion, _, err := tf.Version(ctx, true)
		if err != nil {
			return err
		}
		log.Printf("TF Version: %q", tfVersion)
		// tf.Destroy(ctx)
		// etc.
	}
	i.Remove(ctx)

	return nil
}

Just keep in mind that the installation and removal are not (yet) thread-safe, so each installation should happen in a single thread and removal afterwards. See also hashicorp/hc-install#35 for more on that topic.

@radeksimko radeksimko closed this Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants