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.
terraform: Check and constrain TF version to >=0.12.0
The (currently simple) discovery *and* checking currently happens during textDocument/completion, which is suboptimal from performance and responsibility perspective, but it is an acceptable technical debt as #16 will address that. Closes #7
- Loading branch information
1 parent
91e330a
commit b57790b
Showing
16 changed files
with
301 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package discovery | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
func LookPath() (string, error) { | ||
path, err := exec.LookPath("terraform") | ||
if err != nil { | ||
return "", fmt.Errorf("unable to find terraform: %s", err) | ||
} | ||
return path, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package exec | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"reflect" | ||
"time" | ||
) | ||
|
||
type Mock struct { | ||
Args []string `json:"args"` | ||
Stdout string `json:"stdout"` | ||
Stderr string `json:"stderr"` | ||
SleepDuration time.Duration `json:"sleep"` | ||
ExitCode int `json:"exit_code"` | ||
} | ||
|
||
func (m *Mock) MarshalJSON() ([]byte, error) { | ||
type t Mock | ||
return json.Marshal((*t)(m)) | ||
} | ||
|
||
func (m *Mock) UnmarshalJSON(b []byte) error { | ||
type t Mock | ||
return json.Unmarshal(b, (*t)(m)) | ||
} | ||
|
||
func MockExecutor(m *Mock) *Executor { | ||
if m == nil { | ||
m = &Mock{} | ||
} | ||
|
||
path, ctxFunc := mockCommandCtxFunc(m) | ||
executor := NewExecutor(context.Background(), path) | ||
executor.cmdCtxFunc = ctxFunc | ||
return executor | ||
} | ||
|
||
func mockCommandCtxFunc(e *Mock) (string, cmdCtxFunc) { | ||
return os.Args[0], func(ctx context.Context, path string, arg ...string) *exec.Cmd { | ||
cmd := exec.CommandContext(ctx, os.Args[0], os.Args[1:]...) | ||
|
||
expectedJson, _ := e.MarshalJSON() | ||
cmd.Env = []string{"TF_LS_MOCK=" + string(expectedJson)} | ||
|
||
return cmd | ||
} | ||
} | ||
|
||
func ExecuteMock(rawMockData string) int { | ||
e := &Mock{} | ||
err := e.UnmarshalJSON([]byte(rawMockData)) | ||
if err != nil { | ||
fmt.Fprint(os.Stderr, "unable to unmarshal mock response") | ||
return 1 | ||
} | ||
|
||
givenArgs := os.Args[1:] | ||
if !reflect.DeepEqual(e.Args, givenArgs) { | ||
fmt.Fprintf(os.Stderr, "arguments don't match.\nexpected: %q\ngiven: %q\n", | ||
e.Args, givenArgs) | ||
return 1 | ||
} | ||
|
||
if e.SleepDuration > 0 { | ||
time.Sleep(e.SleepDuration) | ||
} | ||
|
||
fmt.Fprint(os.Stdout, e.Stdout) | ||
fmt.Fprint(os.Stderr, e.Stderr) | ||
|
||
return e.ExitCode | ||
} |
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.