-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathsettings.go
92 lines (75 loc) · 2.4 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package settings
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/terraform-ls/internal/terraform/datadir"
"github.com/mitchellh/mapstructure"
)
type ExperimentalFeatures struct {
ValidateOnSave bool `mapstructure:"validateOnSave"`
PrefillRequiredFields bool `mapstructure:"prefillRequiredFields"`
}
type Options struct {
CommandPrefix string `mapstructure:"commandPrefix"`
IgnoreDirectoryNames []string `mapstructure:"ignoreDirectoryNames"`
IgnorePaths []string `mapstructure:"ignorePaths"`
// ExperimentalFeatures encapsulates experimental features users can opt into.
ExperimentalFeatures ExperimentalFeatures `mapstructure:"experimentalFeatures"`
IgnoreSingleFileWarning bool `mapstructure:"ignoreSingleFileWarning"`
TerraformExecPath string `mapstructure:"terraformExecPath"`
TerraformExecTimeout string `mapstructure:"terraformExecTimeout"`
TerraformLogFilePath string `mapstructure:"terraformLogFilePath"`
XLegacyModulePaths []string `mapstructure:"rootModulePaths"`
XLegacyExcludeModulePaths []string `mapstructure:"excludeModulePaths"`
}
func (o *Options) Validate() error {
if o.TerraformExecPath != "" {
path := o.TerraformExecPath
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 len(o.IgnoreDirectoryNames) > 0 {
for _, directory := range o.IgnoreDirectoryNames {
if directory == datadir.DataDirName {
return fmt.Errorf("cannot ignore directory %q", datadir.DataDirName)
}
if strings.Contains(directory, string(filepath.Separator)) {
return fmt.Errorf("expected directory name, got a path: %q", directory)
}
}
}
return nil
}
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
}