Skip to content

Commit

Permalink
Display provider versions in completion/hover detail (#329)
Browse files Browse the repository at this point in the history
* Provide provider versions in hover & completion

* Regenerate preloaded provider schemas

* Bump terraform-schema to latest revision
  • Loading branch information
radeksimko authored Dec 4, 2020
1 parent dadc23f commit f1f0a75
Show file tree
Hide file tree
Showing 18 changed files with 161 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/hashicorp/hcl/v2 v2.6.0
github.com/hashicorp/terraform-exec v0.11.1-0.20201007122305-ea2094d52cb5
github.com/hashicorp/terraform-json v0.6.0
github.com/hashicorp/terraform-schema v0.0.0-20201110191417-e2e5d08913c4
github.com/hashicorp/terraform-schema v0.0.0-20201204171308-0c9744a02c65
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
github.com/mitchellh/cli v1.1.1
github.com/mitchellh/go-homedir v1.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ github.com/hashicorp/terraform-json v0.5.0 h1:7TV3/F3y7QVSuN4r9BEXqnWqrAyeOtON8f
github.com/hashicorp/terraform-json v0.5.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-json v0.6.0 h1:nMTj4t9ysC7xJ72rvVsDqhUccvbUINrjhPqafeUeREk=
github.com/hashicorp/terraform-json v0.6.0/go.mod h1:eAbqb4w0pSlRmdvl8fOyHAi/+8jnkVYN28gJkSJrLhU=
github.com/hashicorp/terraform-schema v0.0.0-20201110191417-e2e5d08913c4 h1:yNsRZ4dH6psVLdh9KnjEKGafrv4KAbvhK1cXrT+Ow50=
github.com/hashicorp/terraform-schema v0.0.0-20201110191417-e2e5d08913c4/go.mod h1:tU5zEQwdtUJbMQXh5+s0TnBOFshXSAcdAx8Ki8p2S4w=
github.com/hashicorp/terraform-schema v0.0.0-20201204171308-0c9744a02c65 h1:CFZI/uaZCoAN0SI4A9edORfj+tnq+Hg0v9PH2AZ+1tA=
github.com/hashicorp/terraform-schema v0.0.0-20201204171308-0c9744a02c65/go.mod h1:tU5zEQwdtUJbMQXh5+s0TnBOFshXSAcdAx8Ki8p2S4w=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 h1:HKLsbzeOsfXmKNpr3GiT18XAblV0BjCbzL8KQAMZGa0=
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod h1:kNDNcF7sN4DocDLBkQYz73HGKwN1ANB1blq4lIYLYvg=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
Expand Down
1 change: 1 addition & 0 deletions internal/langserver/handlers/complete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestCompletion_withValidData(t *testing.T) {
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions internal/langserver/handlers/execute_command_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestLangServer_workspaceExecuteCommand_init_basic(t *testing.T) {
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Expand Down Expand Up @@ -150,6 +151,7 @@ func TestLangServer_workspaceExecuteCommand_init_error(t *testing.T) {
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions internal/langserver/handlers/formatting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestLangServer_formatting_basic(t *testing.T) {
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Expand Down Expand Up @@ -132,6 +133,7 @@ func TestLangServer_formatting_oldVersion(t *testing.T) {
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.7.6")),
nil,
nil,
},
},
{
Expand Down
1 change: 1 addition & 0 deletions internal/langserver/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ func validTfMockCalls() exec.ExecutorFactory {
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Expand Down
1 change: 1 addition & 0 deletions internal/langserver/handlers/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func TestHover_withValidData(t *testing.T) {
ReturnArguments: []interface{}{
version.Must(version.NewVersion("0.12.0")),
nil,
nil,
},
},
{
Expand Down
48 changes: 41 additions & 7 deletions internal/schemas/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import (
"os"
"path/filepath"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/hashicorp/terraform-exec/tfinstall"
"github.com/hashicorp/terraform-ls/internal/schemas"
"github.com/shurcooL/vfsgen"
)

const terraformBlock = `terraform {
required_version = "~> 0.13"
required_providers {
{{ range $p := . }}
{{ $p.Name }} = {
Expand Down Expand Up @@ -91,20 +94,42 @@ func gen() error {
return err
}

err = tf.Init(ctx, tfexec.Upgrade(true), tfexec.LockTimeout("120s"))
err = tf.Init(ctx, tfexec.Upgrade(true))
if err != nil {
return err
}

// TODO upstream change to have tfexec write to file directly instead of unmarshal/remarshal
log.Println("running terraform providers schema")
ps, err := tf.ProvidersSchema(ctx)
log.Println("creating schemas/data dir")
err = os.MkdirAll("data", 0755)
if err != nil {
return err
}
fs := http.Dir("data")

log.Println("creating schemas/data dir")
err = os.MkdirAll("data", 0755)
coreVersion, providerVersions, err := tf.Version(ctx, true)
if err != nil {
return err
}

log.Println("creating version file")
versionFile, err := os.Create(filepath.Join("data", "versions.json"))
if err != nil {
return err
}
versionOutput := &schemas.VersionOutput{
CoreVersion: coreVersion.String(),
Providers: stringifyProviderVersions(providerVersions),
}

log.Println("writing versions to file")
err = json.NewEncoder(versionFile).Encode(versionOutput)
if err != nil {
return err
}

// TODO upstream change to have tfexec write to file directly instead of unmarshal/remarshal
log.Println("running terraform providers schema")
ps, err := tf.ProvidersSchema(ctx)
if err != nil {
return err
}
Expand All @@ -122,14 +147,23 @@ func gen() error {
}

log.Println("generating embedded go file")
var fs http.FileSystem = http.Dir("data")
return vfsgen.Generate(fs, vfsgen.Options{
Filename: "schemas.go",
PackageName: "schemas",
VariableName: "Files",
})
}

func stringifyProviderVersions(m map[string]*version.Version) map[string]string {
versions := make(map[string]string, 0)

for addr, ver := range m {
versions[addr] = ver.String()
}

return versions
}

type providerAttributes struct {
Name string `json:"name"`
FullName string `json:"full-name"`
Expand Down
16 changes: 12 additions & 4 deletions internal/schemas/schemas.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions internal/schemas/version_output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package schemas

type VersionOutput struct {
CoreVersion string `json:"core"`
Providers map[string]string `json:"providers"`
}
8 changes: 4 additions & 4 deletions internal/terraform/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,16 @@ func (e *Executor) Format(ctx context.Context, input []byte) ([]byte, error) {
return buf.Bytes(), e.contextfulError(ctx, "Format", err)
}

func (e *Executor) Version(ctx context.Context) (*version.Version, error) {
func (e *Executor) Version(ctx context.Context) (*version.Version, map[string]*version.Version, error) {
ctx, cancel := e.withTimeout(ctx)
defer cancel()
err := e.setLogPath("Version")
if err != nil {
return nil, err
return nil, nil, err
}

ver, _, err := e.tf.Version(ctx, true)
return ver, e.contextfulError(ctx, "Version", err)
ver, pv, err := e.tf.Version(ctx, true)
return ver, pv, e.contextfulError(ctx, "Version", err)
}

func (e *Executor) ProviderSchemas(ctx context.Context) (*tfjson.ProviderSchemas, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestExec_timeout(t *testing.T) {

expectedErr := ExecTimeoutError("Version", timeout)

_, err := e.Version(context.Background())
_, _, err := e.Version(context.Background())
if err != nil {
if errors.Is(err, expectedErr) {
return
Expand All @@ -39,7 +39,7 @@ func TestExec_cancel(t *testing.T) {

expectedErr := ExecCanceledError("Version")

_, err := e.Version(ctx)
_, _, err := e.Version(ctx)
if err != nil {
if errors.Is(err, expectedErr) {
return
Expand Down
19 changes: 14 additions & 5 deletions internal/terraform/exec/mock/executor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/terraform/exec/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ type TerraformExecutor interface {
GetExecPath() string
Init(ctx context.Context, opts ...tfexec.InitOption) error
Format(ctx context.Context, input []byte) ([]byte, error)
Version(ctx context.Context) (*version.Version, error)
Version(ctx context.Context) (*version.Version, map[string]*version.Version, error)
ProviderSchemas(ctx context.Context) (*tfjson.ProviderSchemas, error)
}
Loading

0 comments on commit f1f0a75

Please sign in to comment.