-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
settings: Make root modules configurable #198
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Settings | ||
|
||
## Supported Options | ||
|
||
The language server supports the following configuration options: | ||
|
||
## `rootModulePaths` (`[]string`) | ||
|
||
This allows overriding automatic root module discovery by passing a static list | ||
of absolute paths to root modules (i.e. folders with `*.tf` files | ||
which have been `terraform init`-ed). | ||
|
||
## How to pass settings | ||
|
||
The server expects static settings to be passed as part of LSP `initialize` call, | ||
but how settings are requested from on the UI side depends on the client. | ||
|
||
### Sublime Text | ||
|
||
Use `initializationOptions` key under the `clients.terraform` section, e.g. | ||
|
||
```json | ||
{ | ||
"clients": { | ||
"terraform": { | ||
"initializationOptions": { | ||
"rootModulePaths": ["/any/path"] | ||
}, | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### VS Code | ||
|
||
Use `terraform-ls`, e.g. | ||
|
||
```json | ||
{ | ||
"terraform-ls": { | ||
"rootModulePaths": ["/any/path"] | ||
} | ||
} | ||
``` |
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,59 @@ | ||
package settings | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
type Options struct { | ||
// RootModulePaths describes a list of absolute paths to root modules | ||
RootModulePaths []string `mapstructure:"rootModulePaths"` | ||
|
||
// TODO: Need to check for conflict with CLI flags | ||
// TerraformExecPath string | ||
// TerraformExecTimeout time.Duration | ||
// TerraformLogFilePath string | ||
} | ||
|
||
func (o *Options) Validate() error { | ||
var result *multierror.Error | ||
|
||
for _, p := range o.RootModulePaths { | ||
if !filepath.IsAbs(p) { | ||
result = multierror.Append(result, fmt.Errorf("%q is not an absolute path", p)) | ||
} | ||
} | ||
|
||
return result.ErrorOrNil() | ||
} | ||
|
||
type DecodedOptions struct { | ||
Options *Options | ||
UnusedKeys []string | ||
} | ||
|
||
func DecodeOptions(input interface{}) (*DecodedOptions, error) { | ||
var md mapstructure.Metadata | ||
var options Options | ||
|
||
config := &mapstructure.DecoderConfig{ | ||
Metadata: &md, | ||
Result: &options, | ||
} | ||
decoder, err := mapstructure.NewDecoder(config) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := decoder.Decode(input); err != nil { | ||
return nil, err | ||
} | ||
|
||
return &DecodedOptions{ | ||
Options: &options, | ||
UnusedKeys: md.Unused, | ||
}, 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 settings | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
) | ||
|
||
func TestDecodeOptions_nil(t *testing.T) { | ||
out, err := DecodeOptions(nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
opts := out.Options | ||
|
||
if opts.RootModulePaths != nil { | ||
t.Fatalf("expected no options for nil, %#v given", opts.RootModulePaths) | ||
} | ||
} | ||
|
||
func TestDecodeOptions_wrongType(t *testing.T) { | ||
_, err := DecodeOptions(map[string]interface{}{ | ||
"rootModulePaths": "/random/path", | ||
}) | ||
if err == nil { | ||
t.Fatal("expected decoding of wrong type to result in error") | ||
} | ||
} | ||
|
||
func TestDecodeOptions_success(t *testing.T) { | ||
out, err := DecodeOptions(map[string]interface{}{ | ||
"rootModulePaths": []string{"/random/path"}, | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
opts := out.Options | ||
expectedPaths := []string{"/random/path"} | ||
if diff := cmp.Diff(expectedPaths, opts.RootModulePaths); diff != "" { | ||
t.Fatalf("options mismatch: %s", diff) | ||
} | ||
} | ||
|
||
func TestDecodedOptions_Validate(t *testing.T) { | ||
opts := &Options{ | ||
RootModulePaths: []string{ | ||
"./relative/path", | ||
}, | ||
} | ||
err := opts.Validate() | ||
if err == nil { | ||
t.Fatal("expected relative path to fail validation") | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit, make
ctx
first argumentThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I don't disagree, but I think I'd prefer to change all of the
With*
functions at the same time and perhaps do that in a separate PR, if that's ok and follow the same pattern throughout the package for now?