-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
settings: Make root modules configurable
- Loading branch information
1 parent
042d5d0
commit 3805145
Showing
16 changed files
with
2,004 additions
and
1 deletion.
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
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
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.
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.