8 "github.com/hashicorp/hcl2/hcl"
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
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.
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
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.
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() {
36 primary, fDiags := p.loadFiles(primaryPaths, false)
37 diags = append(diags, fDiags...)
38 override, fDiags := p.loadFiles(overridePaths, true)
39 diags = append(diags, fDiags...)
41 mod, modDiags := NewModule(primary, override)
42 diags = append(diags, modDiags...)
49 // ConfigDirFiles returns lists of the primary and override files configuration
50 // files in the given directory.
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)
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
66 func (p *Parser) loadFiles(paths []string, override bool) ([]*File, hcl.Diagnostics) {
68 var diags hcl.Diagnostics
70 for _, path := range paths {
72 var fDiags hcl.Diagnostics
74 f, fDiags = p.LoadConfigFileOverride(path)
76 f, fDiags = p.LoadConfigFile(path)
78 diags = append(diags, fDiags...)
80 files = append(files, f)
87 func (p *Parser) dirFiles(dir string) (primary, override []string, diags hcl.Diagnostics) {
88 infos, err := p.fs.ReadDir(dir)
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),
98 for _, info := range infos {
100 // We only care about files
106 if ext == "" || IsIgnoredFile(name) {
110 baseName := name[:len(name)-len(ext)] // strip extension
111 isOverride := baseName == "override" || strings.HasSuffix(baseName, "_override")
113 fullPath := filepath.Join(dir, name)
115 override = append(override, fullPath)
117 primary = append(primary, fullPath)
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") {
129 } else if strings.HasSuffix(path, ".tf.json") {
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