Skip to content

Commit

Permalink
Switch to hc-install from tfinstall (#737)
Browse files Browse the repository at this point in the history
* Use hcinstall in handlers

* use hcinstall in exec

* replace tfinstall in gen.go

* go mod tidy

* use latest version in exec

* add error handling in handlers/complete_test

* add cleanup functions

* revert installation path to t.TempDir()

Co-authored-by: Radek Simko <radek.simko@gmail.com>
  • Loading branch information
rtim75 and radeksimko authored Jan 11, 2022
1 parent 1221630 commit 7dd9d89
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-uuid v1.0.2
github.com/hashicorp/go-version v1.4.0
github.com/hashicorp/hc-install v0.3.1
github.com/hashicorp/hcl-lang v0.0.0-20211123142056-191cd51dec5b
github.com/hashicorp/hcl/v2 v2.11.1
github.com/hashicorp/terraform-exec v0.15.0
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/hc-install v0.3.1 h1:VIjllE6KyAI1A244G8kTaHXy+TL5/XYzvrtFi8po/Yk=
github.com/hashicorp/hc-install v0.3.1/go.mod h1:3LCdWcCDS1gaHC9mhHCGbkYfoY6vdsKohGjugbZdZak=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/hcl-lang v0.0.0-20211118124824-da3a292c5d7a/go.mod h1:0W3+VP07azoS+fCX5hWk1KxwHnqf1s9J7oBg2cFXm1c=
Expand Down Expand Up @@ -498,6 +500,7 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa h1:idItI2DDfCokpg0N51B2VtiLdJ4vAuXC9fnCb2gACo4=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down
42 changes: 35 additions & 7 deletions internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import (
"time"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-exec/tfinstall"
hcinstall "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"
tfjson "github.com/hashicorp/terraform-json"
"github.com/hashicorp/terraform-ls/internal/langserver"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
Expand Down Expand Up @@ -603,7 +607,7 @@ func TestVarsCompletion_withValidData(t *testing.T) {
"detail": "required, string",
"insertTextFormat":1,
"textEdit": {
"range": {"start":{"line":0,"character":0}, "end":{"line":0,"character":0}},
"range": {"start":{"line":0,"character":0}, "end":{"line":0,"character":0}},
"newText":"test"
}
}
Expand Down Expand Up @@ -844,18 +848,42 @@ output "test" {
}`)
}

func tfExecutor(t *testing.T, workdir, version string) exec.TerraformExecutor {
func tfExecutor(t *testing.T, workdir, tfVersion string) exec.TerraformExecutor {
ctx := context.Background()
installPath := filepath.Join(t.TempDir(), "tfinstall")
err := os.MkdirAll(installPath, 0o755)
if err != nil {
installDir := filepath.Join(t.TempDir(), "hcinstall")
if err := os.MkdirAll(installDir, 0o755); err != nil {
t.Fatal(err)
}
execPath, err := tfinstall.Find(ctx, tfinstall.ExactVersion(version, installPath))
t.Cleanup(func() {
if err := os.Remove(installDir); err != nil {
t.Fatal(err)
}
})

i := hcinstall.NewInstaller()
v := version.Must(version.NewVersion(tfVersion))

execPath, err := i.Ensure(ctx, []src.Source{
&fs.ExactVersion{
Product: product.Terraform,
Version: v,
},
&releases.ExactVersion{
Product: product.Terraform,
Version: v,
InstallDir: installDir,
},
})
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
if err := i.Remove(ctx); err != nil {
t.Fatal(err)
}
})

tfExec, err := exec.NewExecutor(workdir, execPath)
if err != nil {
t.Fatal(err)
Expand Down
25 changes: 20 additions & 5 deletions internal/schemas/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ import (
"path/filepath"

"github.com/hashicorp/go-version"
hcinstall "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"
"github.com/hashicorp/terraform-exec/tfinstall"
"github.com/hashicorp/terraform-ls/internal/schemas"
"github.com/shurcooL/vfsgen"
)
Expand Down Expand Up @@ -82,17 +86,28 @@ func gen() error {

log.Println("ensuring terraform is installed")

tmpDir, err := ioutil.TempDir("", "tfinstall")
installDir, err := ioutil.TempDir("", "hcinstall")
if err != nil {
return err
}
defer os.RemoveAll(tmpDir)

execPath, err := tfinstall.Find(ctx, tfinstall.LookPath(), tfinstall.LatestVersion(tmpDir, false))
defer os.RemoveAll(installDir)

i := hcinstall.NewInstaller()
execPath, err := i.Ensure(ctx, []src.Source{
&fs.AnyVersion{
Product: &product.Terraform,
},
&releases.LatestVersion{
Product: product.Terraform,
InstallDir: installDir,
},
})
if err != nil {
return err
}

defer i.Remove(ctx)

log.Println("running terraform init")

cwd, err := os.Getwd()
Expand Down
39 changes: 27 additions & 12 deletions internal/terraform/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"testing"
"time"

"github.com/hashicorp/terraform-exec/tfinstall"
hcinstall "github.com/hashicorp/hc-install"
"github.com/hashicorp/hc-install/product"
"github.com/hashicorp/hc-install/releases"
"github.com/hashicorp/hc-install/src"
)

func TestExec_timeout(t *testing.T) {
Expand Down Expand Up @@ -58,24 +61,36 @@ func TestExec_cancel(t *testing.T) {
}

func newExecutor(t *testing.T) TerraformExecutor {
tmpDir := TempDir(t)
workDir := filepath.Join(tmpDir, "workdir")
err := os.MkdirAll(workDir, 0755)
if err != nil {
t.Fatal(err)
}
installDir := filepath.Join(tmpDir, "installdir")
err = os.MkdirAll(installDir, 0755)
if err != nil {
ctx := context.Background()
workDir := TempDir(t)
installDir := filepath.Join(workDir, "hcinstall")
if err := os.MkdirAll(installDir, 0755); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
if err := os.Remove(installDir); err != nil {
t.Fatal(err)
}
})

opts := tfinstall.ExactVersion("1.1.0", installDir)
execPath, err := opts.ExecPath(context.Background())
i := hcinstall.NewInstaller()

execPath, err := i.Ensure(ctx, []src.Source{
&releases.LatestVersion{
Product: product.Terraform,
InstallDir: installDir,
},
})
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
if err := i.Remove(ctx); err != nil {
t.Fatal(err)
}
})

e, err := NewExecutor(workDir, execPath)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 7dd9d89

Please sign in to comment.