| 1 |
// Copyright (c) HashiCorp, Inc. |
| 2 |
// SPDX-License-Identifier: MPL-2.0 |
| 3 |
|
| 4 |
package tfconfig |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"path/filepath" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"github.com/hashicorp/hcl/v2" |
| 12 |
) |
| 13 |
|
| 14 |
// LoadModule reads the directory at the given path and attempts to interpret |
| 15 |
// it as a Terraform module. |
| 16 |
func LoadModule(dir string) (*Module, Diagnostics) { |
| 17 |
return LoadModuleFromFilesystem(NewOsFs(), dir) |
| 18 |
} |
| 19 |
|
| 20 |
// LoadModuleFromFilesystem reads the directory at the given path |
| 21 |
// in the given FS and attempts to interpret it as a Terraform module |
| 22 |
func LoadModuleFromFilesystem(fs FS, dir string) (*Module, Diagnostics) { |
| 23 |
// For broad compatibility here we actually have two separate loader |
| 24 |
// codepaths. The main one uses the new HCL parser and API and is intended |
| 25 |
// for configurations from Terraform 0.12 onwards (though will work for |
| 26 |
// many older configurations too), but we'll also fall back on one that |
| 27 |
// uses the _old_ HCL implementation so we can deal with some edge-cases |
| 28 |
// that are not valid in new HCL. |
| 29 |
|
| 30 |
module, diags := loadModule(fs, dir) |
| 31 |
if diags.HasErrors() { |
| 32 |
// Try using the legacy HCL parser and see if we fare better. |
| 33 |
legacyModule, legacyDiags := loadModuleLegacyHCL(fs, dir) |
| 34 |
if !legacyDiags.HasErrors() { |
| 35 |
legacyModule.init(legacyDiags) |
| 36 |
return legacyModule, legacyDiags |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
module.init(diags) |
| 41 |
return module, diags |
| 42 |
} |
| 43 |
|
| 44 |
// IsModuleDir checks if the given path contains terraform configuration files. |
| 45 |
// This allows the caller to decide how to handle directories that do not have tf files. |
| 46 |
func IsModuleDir(dir string) bool { |
| 47 |
return IsModuleDirOnFilesystem(NewOsFs(), dir) |
| 48 |
} |
| 49 |
|
| 50 |
// IsModuleDirOnFilesystem checks if the given path in the given FS contains |
| 51 |
// Terraform configuration files. This allows the caller to decide |
| 52 |
// how to handle directories that do not have tf files. |
| 53 |
func IsModuleDirOnFilesystem(fs FS, dir string) bool { |
| 54 |
primaryPaths, _ := dirFiles(fs, dir) |
| 55 |
if len(primaryPaths) == 0 { |
| 56 |
return false |
| 57 |
} |
| 58 |
return true |
| 59 |
} |
| 60 |
|
| 61 |
func (m *Module) init(diags Diagnostics) { |
| 62 |
// Fill in any additional provider requirements that are implied by |
| 63 |
// resource configurations, to avoid the caller from needing to apply |
| 64 |
// this logic itself. Implied requirements don't have version constraints, |
| 65 |
// but we'll make sure the requirement value is still non-nil in this |
| 66 |
// case so callers can easily recognize it. |
| 67 |
for _, r := range m.ManagedResources { |
| 68 |
if _, exists := m.RequiredProviders[r.Provider.Name]; !exists { |
| 69 |
m.RequiredProviders[r.Provider.Name] = &ProviderRequirement{} |
| 70 |
} |
| 71 |
} |
| 72 |
for _, r := range m.DataResources { |
| 73 |
if _, exists := m.RequiredProviders[r.Provider.Name]; !exists { |
| 74 |
m.RequiredProviders[r.Provider.Name] = &ProviderRequirement{} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
// We redundantly also reference the diagnostics from inside the module |
| 79 |
// object, primarily so that we can easily included in JSON-serialized |
| 80 |
// versions of the module object. |
| 81 |
m.Diagnostics = diags |
| 82 |
} |
| 83 |
|
| 84 |
func dirFiles(fs FS, dir string) (primary []string, diags hcl.Diagnostics) { |
| 85 |
infos, err := fs.ReadDir(dir) |
| 86 |
if err != nil { |
| 87 |
diags = append(diags, &hcl.Diagnostic{ |
| 88 |
Severity: hcl.DiagError, |
| 89 |
Summary: "Failed to read module directory", |
| 90 |
Detail: fmt.Sprintf("Module directory %s does not exist or cannot be read.", dir), |
| 91 |
}) |
| 92 |
return |
| 93 |
} |
| 94 |
|
| 95 |
var override []string |
| 96 |
for _, info := range infos { |
| 97 |
if info.IsDir() { |
| 98 |
// We only care about files |
| 99 |
continue |
| 100 |
} |
| 101 |
|
| 102 |
name := info.Name() |
| 103 |
ext := fileExt(name) |
| 104 |
if ext == "" || isIgnoredFile(name) { |
| 105 |
continue |
| 106 |
} |
| 107 |
|
| 108 |
baseName := name[:len(name)-len(ext)] // strip extension |
| 109 |
isOverride := baseName == "override" || strings.HasSuffix(baseName, "_override") |
| 110 |
|
| 111 |
fullPath := filepath.Join(dir, name) |
| 112 |
if isOverride { |
| 113 |
override = append(override, fullPath) |
| 114 |
} else { |
| 115 |
primary = append(primary, fullPath) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
// We are assuming that any _override files will be logically named, |
| 120 |
// and processing the files in alphabetical order. Primaries first, then overrides. |
| 121 |
primary = append(primary, override...) |
| 122 |
|
| 123 |
return |
| 124 |
} |
| 125 |
|
| 126 |
// fileExt returns the Terraform configuration extension of the given |
| 127 |
// path, or a blank string if it is not a recognized extension. |
| 128 |
func fileExt(path string) string { |
| 129 |
if strings.HasSuffix(path, ".tf") { |
| 130 |
return ".tf" |
| 131 |
} else if strings.HasSuffix(path, ".tf.json") { |
| 132 |
return ".tf.json" |
| 133 |
} else { |
| 134 |
return "" |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
// isIgnoredFile returns true if the given filename (which must not have a |
| 139 |
// directory path ahead of it) should be ignored as e.g. an editor swap file. |
| 140 |
func isIgnoredFile(name string) bool { |
| 141 |
return strings.HasPrefix(name, ".") || // Unix-like hidden files |
| 142 |
strings.HasSuffix(name, "~") || // vim |
| 143 |
strings.HasPrefix(name, "#") && strings.HasSuffix(name, "#") // emacs |
| 144 |
} |