Skip to content

Commit

Permalink
basic draft to fix #106
Browse files Browse the repository at this point in the history
  • Loading branch information
njuCZ committed Aug 11, 2020
1 parent 6ac17ab commit d9b4b44
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions internal/terraform/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ func writeAndClose(w io.WriteCloser, input []byte) (int, error) {
return n, nil
}

func (e *Executor) Init(ctx context.Context) error {
_, err := e.run(ctx, "init")
return err
}

func (e *Executor) Version(ctx context.Context) (string, error) {
out, err := e.run(ctx, "version")
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions internal/terraform/rootmodule/root_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,17 @@ func (rm *rootModule) discoverTerraformExecutor(ctx context.Context) error {
return nil
}

func (rm *rootModule) ExecuteTerraformInit(ctx context.Context) error {
if err := rm.discoverTerraformExecutor(ctx); err != nil {
return err
}
if rm.tfExec == nil {
return errors.New("no terraform executor - unable to init workspace")
}

return rm.tfExec.Init(ctx)
}

func (rm *rootModule) discoverTerraformVersion(ctx context.Context) error {
if rm.tfExec == nil {
return errors.New("no terraform executor - unable to read version")
Expand Down
4 changes: 4 additions & 0 deletions internal/terraform/rootmodule/root_module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ func (rmm *rootModuleManager) SetLogger(logger *log.Logger) {
rmm.logger = logger
}

func (rmm *rootModuleManager) NewPotentialRootModule(dir string) (RootModule, error) {
return rmm.newRootModule(context.Background(), filepath.Clean(dir))
}

func (rmm *rootModuleManager) AddAndStartLoadingRootModule(ctx context.Context, dir string) (RootModule, error) {
dir = filepath.Clean(dir)

Expand Down
2 changes: 2 additions & 0 deletions internal/terraform/rootmodule/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type RootModuleManager interface {
SetTerraformExecLogPath(logPath string)
SetTerraformExecTimeout(timeout time.Duration)

NewPotentialRootModule(dir string) (RootModule, error)
AddAndStartLoadingRootModule(ctx context.Context, dir string) (RootModule, error)
WorkerPoolSize() int
WorkerQueueSize() int
Expand Down Expand Up @@ -78,6 +79,7 @@ type RootModule interface {
IsParserLoaded() bool
TerraformFormatter() (exec.Formatter, error)
IsTerraformLoaded() bool
ExecuteTerraformInit(ctx context.Context) error
Modules() []ModuleRecord
}

Expand Down
26 changes: 25 additions & 1 deletion langserver/handlers/did_open.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,34 @@ func (lh *logHandler) TextDocumentDidOpen(ctx context.Context, params lsp.DidOpe
// because we don't gather "init-able folders" in any way
" You may need to run terraform init"+
" and reload your editor.", readableDir)
return jrpc2.PushNotify(ctx, "window/showMessage", lsp.ShowMessageParams{
resp, _ := jrpc2.PushCall(ctx, "window/showMessageRequest", lsp.ShowMessageRequestParams{
Type: lsp.MTWarning,
Message: msg,
Actions: []lsp.MessageActionItem{
{
Title: "run `terraform init`",
},
},
})
var action lsp.MessageActionItem
if err := resp.UnmarshalResult(&action); err != nil {
return err
}
if action.Title == "run `terraform init`" {
rmm, err := lsctx.RootModuleManager(ctx)
if err != nil {
return err
}
rm, err := rmm.NewPotentialRootModule(f.Dir())
if err != nil {
return err
}
go func() {
if err := rm.ExecuteTerraformInit(ctx); err != nil {
lh.logger.Printf("failed to execute `terraform init`: %+v", err)
}
}()
}
}
if len(candidates) > 1 {
candidateDir := humanReadablePath(rootDir, candidates[0].Path())
Expand Down
1 change: 1 addition & 0 deletions langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
ctx = lsctx.WithDocumentStorage(ctx, fs)
ctx = lsctx.WithRootDirectory(ctx, &rootDir)
ctx = lsctx.WithRootModuleCandidateFinder(ctx, svc.modMgr)
ctx = lsctx.WithRootModuleManager(ctx, svc.modMgr)
ctx = lsctx.WithRootModuleWalker(ctx, svc.walker)
return handle(ctx, req, lh.TextDocumentDidOpen)
},
Expand Down

0 comments on commit d9b4b44

Please sign in to comment.