forked from hashicorp/terraform-ls
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose terraform.init as a command + implement progress reporting (ha…
- Loading branch information
1 parent
e9f301d
commit 586ff89
Showing
16 changed files
with
424 additions
and
30 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,56 @@ | ||
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 TerraformInitHandler(ctx context.Context, args cmd.CommandArgs) (interface{}, error) { | ||
fileUri, ok := args.GetString("uri") | ||
if !ok || fileUri == "" { | ||
return nil, fmt.Errorf("%w: expected uri argument to be set", code.InvalidParams.Err()) | ||
} | ||
|
||
fh := ilsp.FileHandlerFromDocumentURI(lsp.DocumentURI(fileUri)) | ||
|
||
cf, err := lsctx.RootModuleFinder(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rm, err := cf.RootModuleByPath(fh.Dir()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
progressBegin(ctx, "Initializing") | ||
defer func() { | ||
progressEnd(ctx, "Finished") | ||
}() | ||
|
||
progressReport(ctx, "Running terraform init ...") | ||
err = rm.ExecuteTerraformInit(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
progressReport(ctx, "Detecting paths to watch ...") | ||
paths := rm.PathsToWatch() | ||
|
||
w, err := lsctx.Watcher(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = w.AddPaths(paths) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to add watch for dir (%s): %+v", fh.Dir(), err) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package command | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/creachadair/jrpc2" | ||
lsctx "github.com/hashicorp/terraform-ls/internal/context" | ||
lsp "github.com/hashicorp/terraform-ls/internal/protocol" | ||
) | ||
|
||
func progressBegin(ctx context.Context, title string) error { | ||
token, ok := lsctx.ProgressToken(ctx) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return jrpc2.PushNotify(ctx, "$/progress", lsp.ProgressParams{ | ||
Token: token, | ||
Value: lsp.WorkDoneProgressBegin{ | ||
Kind: "begin", | ||
Title: title, | ||
}, | ||
}) | ||
} | ||
|
||
func progressReport(ctx context.Context, message string) error { | ||
token, ok := lsctx.ProgressToken(ctx) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return jrpc2.PushNotify(ctx, "$/progress", lsp.ProgressParams{ | ||
Token: token, | ||
Value: lsp.WorkDoneProgressReport{ | ||
Kind: "report", | ||
Message: message, | ||
}, | ||
}) | ||
} | ||
|
||
func progressEnd(ctx context.Context, message string) error { | ||
token, ok := lsctx.ProgressToken(ctx) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return jrpc2.PushNotify(ctx, "$/progress", lsp.ProgressParams{ | ||
Token: token, | ||
Value: lsp.WorkDoneProgressEnd{ | ||
Kind: "end", | ||
Message: message, | ||
}, | ||
}) | ||
} |
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.