|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/creachadair/jrpc2/code" |
| 8 | + "github.com/hashicorp/terraform-ls/internal/langserver/cmd" |
| 9 | + "github.com/hashicorp/terraform-ls/internal/uri" |
| 10 | +) |
| 11 | + |
| 12 | +const terraformVersionRequestVersion = 0 |
| 13 | + |
| 14 | +type terraformInfoResponse struct { |
| 15 | + FormatVersion int `json:"v"` |
| 16 | + RequiredVersion string `json:"required_version,omitempty"` |
| 17 | + DiscoveredVersion string `json:"discovered_version,omitempty"` |
| 18 | + DiscoveredPath string `json:"discovered_path,omitempty"` |
| 19 | +} |
| 20 | + |
| 21 | +func (h *CmdHandler) TerraformVersionRequestHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) { |
| 22 | + response := terraformInfoResponse{ |
| 23 | + FormatVersion: terraformVersionRequestVersion, |
| 24 | + } |
| 25 | + |
| 26 | + modUri, ok := args.GetString("uri") |
| 27 | + if !ok || modUri == "" { |
| 28 | + return response, fmt.Errorf("%w: expected module uri argument to be set", code.InvalidParams.Err()) |
| 29 | + } |
| 30 | + |
| 31 | + if !uri.IsURIValid(modUri) { |
| 32 | + return response, fmt.Errorf("URI %q is not valid", modUri) |
| 33 | + } |
| 34 | + |
| 35 | + modPath, err := uri.PathFromURI(modUri) |
| 36 | + if err != nil { |
| 37 | + return response, err |
| 38 | + } |
| 39 | + |
| 40 | + mod, _ := h.StateStore.Modules.ModuleByPath(modPath) |
| 41 | + if mod == nil { |
| 42 | + return response, nil |
| 43 | + } |
| 44 | + |
| 45 | + if mod.TerraformVersion == nil { |
| 46 | + return response, nil |
| 47 | + } |
| 48 | + if mod.Meta.CoreRequirements == nil { |
| 49 | + return response, nil |
| 50 | + } |
| 51 | + |
| 52 | + response.DiscoveredVersion = mod.TerraformVersion.String() |
| 53 | + response.RequiredVersion = mod.Meta.CoreRequirements.String() |
| 54 | + |
| 55 | + return response, nil |
| 56 | +} |
0 commit comments