-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Terraform Validate Command (#323)
* Implement module validation on save * go mod tidy * gofmt * Adapt validation to command * Use tfjson diagnostic type Bump tfjson and terraform-schema * update ref * Add tests regarding diag source cache * Apply suggestions from code review Co-authored-by: Radek Simko <radek.simko@gmail.com> * fix var * bump terraform-schema * clean up internals with typedefs, surface WasInitialized error Co-authored-by: Radek Simko <radek.simko@gmail.com>
- Loading branch information
1 parent
f02b7fa
commit 87c4e22
Showing
14 changed files
with
353 additions
and
36 deletions.
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
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
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
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
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,52 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/creachadair/jrpc2/code" | ||
lsctx "github.com/hashicorp/terraform-ls/internal/context" | ||
"github.com/hashicorp/terraform-ls/internal/langserver/cmd" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
lsp "github.com/hashicorp/terraform-ls/internal/protocol" | ||
) | ||
|
||
func TerraformValidateHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) { | ||
dirUri, ok := args.GetString("uri") | ||
if !ok || dirUri == "" { | ||
return nil, fmt.Errorf("%w: expected dir uri argument to be set", code.InvalidParams.Err()) | ||
} | ||
|
||
dh := ilsp.FileHandlerFromDirURI(lsp.DocumentURI(dirUri)) | ||
|
||
cf, err := lsctx.RootModuleFinder(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rm, err := cf.RootModuleByPath(dh.Dir()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
wasInit, err := rm.WasInitialized() | ||
if err != nil { | ||
return nil, fmt.Errorf("error checking if %s was initialized: %s", dirUri, err) | ||
} | ||
if !wasInit { | ||
return nil, fmt.Errorf("%s is not an initialized module, terraform validate cannot be called", dirUri) | ||
} | ||
|
||
diags, err := lsctx.Diagnostics(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hclDiags, err := rm.ExecuteTerraformValidate(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
diags.PublishHCLDiags(ctx, rm.Path(), hclDiags, "terraform validate") | ||
|
||
return nil, 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
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
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
Oops, something went wrong.