Skip to content

Commit

Permalink
rootmodule: find executor for unmatched root dir
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jun 24, 2020
1 parent 6fc8b19 commit 7b26e8e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
8 changes: 8 additions & 0 deletions internal/terraform/rootmodule/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ func (e *RootModuleNotFoundErr) Error() string {
}
return "root module not found"
}

func IsRootModuleNotFound(err error) bool {
if err == nil {
return false
}
_, ok := err.(*RootModuleNotFoundErr)
return ok
}
33 changes: 30 additions & 3 deletions internal/terraform/rootmodule/root_module_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,42 @@ func (rmm *rootModuleManager) ParserForDir(path string) (lang.Parser, error) {
return rm.Parser(), nil
}

func (rmm *rootModuleManager) TerraformExecutorForDir(path string) (*exec.Executor, error) {
func (rmm *rootModuleManager) TerraformExecutorForDir(ctx context.Context, path string) (*exec.Executor, error) {
rm, err := rmm.RootModuleByPath(path)
if err != nil {
return nil, err
if err != nil && IsRootModuleNotFound(err) {
return rmm.terraformExecutorForDir(ctx, path)
}

return rm.TerraformExecutor(), nil
}

func (rmm *rootModuleManager) terraformExecutorForDir(ctx context.Context, dir string) (*exec.Executor, error) {
tfPath := rmm.tfExecPath
if tfPath == "" {
var err error
d := &discovery.Discovery{}
tfPath, err = d.LookPath()
if err != nil {
return nil, err
}
}

tf := exec.NewExecutor(ctx, tfPath)

tf.SetWorkdir(dir)
tf.SetLogger(rmm.logger)

if rmm.tfExecLogPath != "" {
tf.SetExecLogPath(rmm.tfExecLogPath)
}

if rmm.tfExecTimeout != 0 {
tf.SetTimeout(rmm.tfExecTimeout)
}

return tf, nil
}

// rootModuleDirFromPath strips known lock file paths and filenames
// to get the directory path of the relevant rootModule
func rootModuleDirFromFilePath(filePath string) string {
Expand Down
4 changes: 2 additions & 2 deletions internal/terraform/rootmodule/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ParserFinder interface {
}

type TerraformExecFinder interface {
TerraformExecutorForDir(path string) (*exec.Executor, error)
TerraformExecutorForDir(ctx context.Context, path string) (*exec.Executor, error)
}

type RootModuleManager interface {
Expand All @@ -30,7 +30,7 @@ type RootModuleManager interface {
PathsToWatch() []string
RootModuleByPath(path string) (RootModule, error)
ParserForDir(path string) (lang.Parser, error)
TerraformExecutorForDir(path string) (*exec.Executor, error)
TerraformExecutorForDir(ctx context.Context, path string) (*exec.Executor, error)
}

type RootModule interface {
Expand Down
2 changes: 1 addition & 1 deletion langserver/handlers/formatting.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (h *logHandler) TextDocumentFormatting(ctx context.Context, params lsp.Docu
return edits, err
}

tf, err := tff.TerraformExecutorForDir(fh.Dir())
tf, err := tff.TerraformExecutorForDir(ctx, fh.Dir())
if err != nil {
return edits, err
}
Expand Down

0 comments on commit 7b26e8e

Please sign in to comment.