aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go
diff options
context:
space:
mode:
authorNathan Dench <ndenc2@gmail.com>2019-05-24 15:16:44 +1000
committerNathan Dench <ndenc2@gmail.com>2019-05-24 15:16:44 +1000
commit107c1cdb09c575aa2f61d97f48d8587eb6bada4c (patch)
treeca7d008643efc555c388baeaf1d986e0b6b3e28c /vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go
parent844b5a68d8af4791755b8f0ad293cc99f5959183 (diff)
downloadterraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.gz
terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.zst
terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.zip
Upgrade to 0.12
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go')
-rw-r--r--vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go142
1 files changed, 142 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go b/vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go
new file mode 100644
index 0000000..3014cb4
--- /dev/null
+++ b/vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go
@@ -0,0 +1,142 @@
1package configs
2
3import (
4 "fmt"
5 "path/filepath"
6 "strings"
7
8 "github.com/hashicorp/hcl2/hcl"
9)
10
11// LoadConfigDir reads the .tf and .tf.json files in the given directory
12// as config files (using LoadConfigFile) and then combines these files into
13// a single Module.
14//
15// If this method returns nil, that indicates that the given directory does not
16// exist at all or could not be opened for some reason. Callers may wish to
17// detect this case and ignore the returned diagnostics so that they can
18// produce a more context-aware error message in that case.
19//
20// If this method returns a non-nil module while error diagnostics are returned
21// then the module may be incomplete but can be used carefully for static
22// analysis.
23//
24// This file does not consider a directory with no files to be an error, and
25// will simply return an empty module in that case. Callers should first call
26// Parser.IsConfigDir if they wish to recognize that situation.
27//
28// .tf files are parsed using the HCL native syntax while .tf.json files are
29// parsed using the HCL JSON syntax.
30func (p *Parser) LoadConfigDir(path string) (*Module, hcl.Diagnostics) {
31 primaryPaths, overridePaths, diags := p.dirFiles(path)
32 if diags.HasErrors() {
33 return nil, diags
34 }
35
36 primary, fDiags := p.loadFiles(primaryPaths, false)
37 diags = append(diags, fDiags...)
38 override, fDiags := p.loadFiles(overridePaths, true)
39 diags = append(diags, fDiags...)
40
41 mod, modDiags := NewModule(primary, override)
42 diags = append(diags, modDiags...)
43
44 mod.SourceDir = path
45
46 return mod, diags
47}
48
49// ConfigDirFiles returns lists of the primary and override files configuration
50// files in the given directory.
51//
52// If the given directory does not exist or cannot be read, error diagnostics
53// are returned. If errors are returned, the resulting lists may be incomplete.
54func (p Parser) ConfigDirFiles(dir string) (primary, override []string, diags hcl.Diagnostics) {
55 return p.dirFiles(dir)
56}
57
58// IsConfigDir determines whether the given path refers to a directory that
59// exists and contains at least one Terraform config file (with a .tf or
60// .tf.json extension.)
61func (p *Parser) IsConfigDir(path string) bool {
62 primaryPaths, overridePaths, _ := p.dirFiles(path)
63 return (len(primaryPaths) + len(overridePaths)) > 0
64}
65
66func (p *Parser) loadFiles(paths []string, override bool) ([]*File, hcl.Diagnostics) {
67 var files []*File
68 var diags hcl.Diagnostics
69
70 for _, path := range paths {
71 var f *File
72 var fDiags hcl.Diagnostics
73 if override {
74 f, fDiags = p.LoadConfigFileOverride(path)
75 } else {
76 f, fDiags = p.LoadConfigFile(path)
77 }
78 diags = append(diags, fDiags...)
79 if f != nil {
80 files = append(files, f)
81 }
82 }
83
84 return files, diags
85}
86
87func (p *Parser) dirFiles(dir string) (primary, override []string, diags hcl.Diagnostics) {
88 infos, err := p.fs.ReadDir(dir)
89 if err != nil {
90 diags = append(diags, &hcl.Diagnostic{
91 Severity: hcl.DiagError,
92 Summary: "Failed to read module directory",
93 Detail: fmt.Sprintf("Module directory %s does not exist or cannot be read.", dir),
94 })
95 return
96 }
97
98 for _, info := range infos {
99 if info.IsDir() {
100 // We only care about files
101 continue
102 }
103
104 name := info.Name()
105 ext := fileExt(name)
106 if ext == "" || IsIgnoredFile(name) {
107 continue
108 }
109
110 baseName := name[:len(name)-len(ext)] // strip extension
111 isOverride := baseName == "override" || strings.HasSuffix(baseName, "_override")
112
113 fullPath := filepath.Join(dir, name)
114 if isOverride {
115 override = append(override, fullPath)
116 } else {
117 primary = append(primary, fullPath)
118 }
119 }
120
121 return
122}
123
124// fileExt returns the Terraform configuration extension of the given
125// path, or a blank string if it is not a recognized extension.
126func fileExt(path string) string {
127 if strings.HasSuffix(path, ".tf") {
128 return ".tf"
129 } else if strings.HasSuffix(path, ".tf.json") {
130 return ".tf.json"
131 } else {
132 return ""
133 }
134}
135
136// IsIgnoredFile returns true if the given filename (which must not have a
137// directory path ahead of it) should be ignored as e.g. an editor swap file.
138func IsIgnoredFile(name string) bool {
139 return strings.HasPrefix(name, ".") || // Unix-like hidden files
140 strings.HasSuffix(name, "~") || // vim
141 strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs
142}