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 754eafb
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
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

0 comments on commit 754eafb

Please sign in to comment.