]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/terraform/configs/parser_config_dir.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform / configs / parser_config_dir.go
1 package configs
2
3 import (
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.
30 func (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.
54 func (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.)
61 func (p *Parser) IsConfigDir(path string) bool {
62 primaryPaths, overridePaths, _ := p.dirFiles(path)
63 return (len(primaryPaths) + len(overridePaths)) > 0
64 }
65
66 func (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
87 func (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.
126 func 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.
138 func 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 }