Skip to content

Commit

Permalink
Add terraform version command
Browse files Browse the repository at this point in the history
This commit adds a new `module.terraform` command which returns the required and discovered version of Terraform in the currrent workspace.
  • Loading branch information
jpogran committed Jul 26, 2022
1 parent c6dcc27 commit 58c564e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
56 changes: 56 additions & 0 deletions internal/langserver/handlers/command/terraform.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package command

import (
"context"
"fmt"

"github.com/creachadair/jrpc2/code"
"github.com/hashicorp/terraform-ls/internal/langserver/cmd"
"github.com/hashicorp/terraform-ls/internal/uri"
)

const terraformVersionRequestVersion = 0

type terraformInfoResponse struct {
FormatVersion int `json:"v"`
RequiredVersion string `json:"required_version,omitempty"`
DiscoveredVersion string `json:"discovered_version,omitempty"`
DiscoveredPath string `json:"discovered_path,omitempty"`
}

func (h *CmdHandler) TerraformVersionRequestHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) {
response := terraformInfoResponse{
FormatVersion: terraformVersionRequestVersion,
}

modUri, ok := args.GetString("uri")
if !ok || modUri == "" {
return response, fmt.Errorf("%w: expected module uri argument to be set", code.InvalidParams.Err())
}

if !uri.IsURIValid(modUri) {
return response, fmt.Errorf("URI %q is not valid", modUri)
}

modPath, err := uri.PathFromURI(modUri)
if err != nil {
return response, err
}

mod, _ := h.StateStore.Modules.ModuleByPath(modPath)
if mod == nil {
return response, nil
}

if mod.TerraformVersion == nil {
return response, nil
}
if mod.Meta.CoreRequirements == nil {
return response, nil
}

response.DiscoveredVersion = mod.TerraformVersion.String()
response.RequiredVersion = mod.Meta.CoreRequirements.String()

return response, nil
}
1 change: 1 addition & 0 deletions internal/langserver/handlers/execute_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func cmdHandlers(svc *service) cmd.Handlers {
cmd.Name("terraform.validate"): cmdHandler.TerraformValidateHandler,
cmd.Name("module.calls"): cmdHandler.ModuleCallsHandler,
cmd.Name("module.providers"): cmdHandler.ModuleProvidersHandler,
cmd.Name("module.terraform"): cmdHandler.TerraformVersionRequestHandler,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/terraform/module/module_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func GetTerraformVersion(ctx context.Context, modStore *state.ModuleStore, modPa
}

ipErr := modStore.UpdateInstalledProviders(modPath, pVersions)
if ipErr != nil {
if ipErr != nil {
return ipErr
}

Expand Down

0 comments on commit 58c564e

Please sign in to comment.