From 715e7f4d68262e205b7eef20af39c9c2877288a7 Mon Sep 17 00:00:00 2001 From: Kazuma Watanabe Date: Sun, 23 Jun 2024 12:58:27 +0000 Subject: [PATCH] Add linterOptions to settings --- internal/langserver/handlers/initialize.go | 8 ++++ internal/settings/settings.go | 47 ++++++++++++++++------ internal/settings/settings_test.go | 18 +++++++++ 3 files changed, 61 insertions(+), 12 deletions(-) diff --git a/internal/langserver/handlers/initialize.go b/internal/langserver/handlers/initialize.go index 947dac91..93e40d0d 100644 --- a/internal/langserver/handlers/initialize.go +++ b/internal/langserver/handlers/initialize.go @@ -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": "", } @@ -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 } diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 08b3c533..ec922937 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -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"` @@ -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"` @@ -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 { @@ -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 diff --git a/internal/settings/settings_test.go b/internal/settings/settings_test.go index 50032454..b6f6cdf5 100644 --- a/internal/settings/settings_test.go +++ b/internal/settings/settings_test.go @@ -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") + } +}