-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add terraform version command #1016
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
754eafb
Add terraform version command
jpogran b15f21e
Add progress notification
jpogran 425f343
Remove discovered path until we can actually discover it
jpogran 084674c
add version info if info is present
jpogran 3715ac6
add test
jpogran 246d7fb
add terraform version change notification
jpogran a1ef321
Docs
jpogran 81e7b96
update intialization tests
jpogran 1c8b9f6
Apply suggestions from code review
jpogran File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/creachadair/jrpc2/code" | ||
"github.com/hashicorp/terraform-ls/internal/langserver/cmd" | ||
"github.com/hashicorp/terraform-ls/internal/langserver/progress" | ||
"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"` | ||
} | ||
|
||
func (h *CmdHandler) TerraformVersionRequestHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) { | ||
progress.Begin(ctx, "Initializing") | ||
defer func() { | ||
progress.End(ctx, "Finished") | ||
}() | ||
|
||
response := terraformInfoResponse{ | ||
FormatVersion: terraformVersionRequestVersion, | ||
} | ||
|
||
progress.Report(ctx, "Finding current module info ...") | ||
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 | ||
} | ||
|
||
progress.Report(ctx, "Recording terraform version info ...") | ||
if mod.TerraformVersion != nil { | ||
response.DiscoveredVersion = mod.TerraformVersion.String() | ||
} | ||
if mod.Meta.CoreRequirements != nil { | ||
response.RequiredVersion = mod.Meta.CoreRequirements.String() | ||
} | ||
|
||
progress.Report(ctx, "Sending response ...") | ||
|
||
return response, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
internal/langserver/handlers/execute_command_terraform_version_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package handlers | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/terraform-ls/internal/document" | ||
"github.com/hashicorp/terraform-ls/internal/langserver" | ||
"github.com/hashicorp/terraform-ls/internal/langserver/cmd" | ||
"github.com/hashicorp/terraform-ls/internal/state" | ||
"github.com/hashicorp/terraform-ls/internal/terraform/exec" | ||
"github.com/hashicorp/terraform-ls/internal/uri" | ||
"github.com/hashicorp/terraform-ls/internal/walker" | ||
tfaddr "github.com/hashicorp/terraform-registry-address" | ||
tfmod "github.com/hashicorp/terraform-schema/module" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestLangServer_workspaceExecuteCommand_terraformVersion_basic(t *testing.T) { | ||
modDir := t.TempDir() | ||
modUri := uri.FromPath(modDir) | ||
|
||
s, err := state.NewStateStore() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = s.Modules.Add(modDir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
metadata := &tfmod.Meta{ | ||
Path: modDir, | ||
CoreRequirements: testConstraint(t, "~> 0.15"), | ||
} | ||
|
||
err = s.Modules.UpdateMetadata(modDir, metadata, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ver, err := version.NewVersion("1.1.0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = s.Modules.UpdateTerraformVersion(modDir, ver, map[tfaddr.Provider]*version.Version{}, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
wc := walker.NewWalkerCollector() | ||
|
||
ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{ | ||
TerraformCalls: &exec.TerraformMockCalls{ | ||
PerWorkDir: map[string][]*mock.Call{ | ||
modDir: validTfMockCalls(), | ||
}, | ||
}, | ||
StateStore: s, | ||
WalkerCollector: wc, | ||
})) | ||
stop := ls.Start(t) | ||
defer stop() | ||
|
||
ls.Call(t, &langserver.CallRequest{ | ||
Method: "initialize", | ||
ReqParams: fmt.Sprintf(`{ | ||
"capabilities": {}, | ||
"rootUri": %q, | ||
"processId": 12345 | ||
}`, modUri)}) | ||
waitForWalkerPath(t, s, wc, document.DirHandleFromURI(modUri)) | ||
ls.Notify(t, &langserver.CallRequest{ | ||
Method: "initialized", | ||
ReqParams: "{}", | ||
}) | ||
|
||
ls.CallAndExpectResponse(t, &langserver.CallRequest{ | ||
Method: "workspace/executeCommand", | ||
ReqParams: fmt.Sprintf(`{ | ||
"command": %q, | ||
"arguments": ["uri=%s"] | ||
}`, cmd.Name("module.terraform"), modUri)}, `{ | ||
"jsonrpc": "2.0", | ||
"id": 2, | ||
"result": { | ||
"v": 0, | ||
"required_version": "~\u003e 0.15", | ||
"discovered_version": "1.1.0" | ||
} | ||
}`) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is getting the data from memory, it should be pretty quick (definitely sub-second, low milliseconds in most cases), so I'm not sure there's much value in the progress reporting for this particular command?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method is a no-op if a token isn't provided in the request, so this technically doesn't do anything yet.
My thinking along this lines is that we enable progress information everywhere as we go, instead of having to add when there's a problem. Then we can dial back if it's too much.