Skip to content

Commit

Permalink
Add linterOptions to settings
Browse files Browse the repository at this point in the history
  • Loading branch information
wata727 committed Jun 23, 2024
1 parent e903528 commit 715e7f4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
8 changes: 8 additions & 0 deletions internal/langserver/handlers/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ func getTelemetryProperties(out *settings.DecodedOptions) map[string]interface{}
"options.terraform.timeout": "",
"options.terraform.logFilePath": false,
"options.validation.earlyValidation": false,
"options.linters.tflint.path": false,
"options.linters.tflint.configPath": false,
"options.linters.tflint.lintOnSave": false,
"options.linters.tflint.timeout": "",
"root_uri": "dir",
"lsVersion": "",
}
Expand All @@ -221,6 +225,10 @@ func getTelemetryProperties(out *settings.DecodedOptions) map[string]interface{}
properties["options.terraform.timeout"] = out.Options.Terraform.Timeout
properties["options.terraform.logFilePath"] = len(out.Options.Terraform.LogFilePath) > 0
properties["options.validation.earlyValidation"] = out.Options.Validation.EnableEnhancedValidation
properties["options.linters.tflint.path"] = len(out.Options.Linters.TFLint.Path) > 0
properties["options.linters.tflint.configPath"] = len(out.Options.Linters.TFLint.ConfigPath) > 0
properties["options.linters.tflint.lintOnSave"] = out.Options.Linters.TFLint.LintOnSave
properties["options.linters.tflint.timeout"] = out.Options.Linters.TFLint.Timeout

return properties
}
Expand Down
47 changes: 35 additions & 12 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ type Terraform struct {
LogFilePath string `mapstructure:"logFilePath"`
}

type LinterOptions struct {
TFLint TFLint `mapstructure:"tflint"`
}

type TFLint struct {
Path string `mapstructure:"path"`
ConfigPath string `mapstructure:"configPath"`
LintOnSave bool `mapstructure:"lintOnSave"`
Timeout string `mapstructure:"timeout"`
}

type Options struct {
CommandPrefix string `mapstructure:"commandPrefix"`
Indexing Indexing `mapstructure:"indexing"`
Expand All @@ -47,6 +58,8 @@ type Options struct {

Terraform Terraform `mapstructure:"terraform"`

Linters LinterOptions `mapstructure:"linters"`

XLegacyModulePaths []string `mapstructure:"rootModulePaths"`
XLegacyExcludeModulePaths []string `mapstructure:"excludeModulePaths"`
XLegacyIgnoreDirectoryNames []string `mapstructure:"ignoreDirectoryNames"`
Expand All @@ -56,18 +69,11 @@ type Options struct {
}

func (o *Options) Validate() error {
if o.Terraform.Path != "" {
path := o.Terraform.Path
if !filepath.IsAbs(path) {
return fmt.Errorf("Expected absolute path for Terraform binary, got %q", path)
}
stat, err := os.Stat(path)
if err != nil {
return fmt.Errorf("Unable to find Terraform binary: %s", err)
}
if stat.IsDir() {
return fmt.Errorf("Expected a Terraform binary, got a directory: %q", path)
}
if err := validateBinaryPath("Terraform", o.Terraform.Path); err != nil {
return err
}
if err := validateBinaryPath("TFLint", o.Linters.TFLint.Path); err != nil {
return err
}

if len(o.Indexing.IgnoreDirectoryNames) > 0 {
Expand All @@ -85,6 +91,23 @@ func (o *Options) Validate() error {
return nil
}

func validateBinaryPath(name string, path string) error {
if path == "" {
return nil
}
if !filepath.IsAbs(path) {
return fmt.Errorf("Expected absolute path for %s binary, got %q", name, path)
}
stat, err := os.Stat(path)
if err != nil {
return fmt.Errorf("Unable to find %s binary: %s", name, err)
}
if stat.IsDir() {
return fmt.Errorf("Expected a %s binary, got a directory: %q", name, path)
}
return nil
}

type DecodedOptions struct {
Options *Options
UnusedKeys []string
Expand Down
18 changes: 18 additions & 0 deletions internal/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,21 @@ func TestValidate_relativePath(t *testing.T) {
t.Fatal("expected decoding of relative path to result in error")
}
}

func TestValidate_linterOptions(t *testing.T) {
out, err := DecodeOptions(map[string]interface{}{
"linters": map[string]interface{}{
"tflint": map[string]interface{}{
"path": "relative/path",
},
},
})
if err != nil {
t.Fatal(err)
}

result := out.Options.Validate()
if result == nil {
t.Fatal("expected decoding of relative path to result in error")
}
}

0 comments on commit 715e7f4

Please sign in to comment.