forked from gruntwork-io/terragrunt
/
config_helpers.go
118 lines (97 loc) · 4.68 KB
/
config_helpers.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package config
import (
"regexp"
"fmt"
"github.com/gruntwork-io/terragrunt/errors"
"github.com/gruntwork-io/terragrunt/options"
"path/filepath"
"github.com/gruntwork-io/terragrunt/util"
)
var INTERPOLATION_SYNTAX_REGEX = regexp.MustCompile("\\$\\{.*?\\}")
var HELPER_FUNCTION_SYNTAX_REGEX = regexp.MustCompile("\\$\\{(.*?)\\(\\)\\}")
var MAX_PARENT_FOLDERS_TO_CHECK = 100
// Given a string value from a .terragrunt config file, parse the string, resolve any calls to helper functions using
// the syntax ${...}, and return the final value.
func ResolveTerragruntConfigString(terragruntConfigString string, include *IncludeConfig, terragruntOptions *options.TerragruntOptions) (resolved string, finalErr error) {
// The function we pass to ReplaceAllStringFunc cannot return an error, so we have to use named error
// parameters to capture such errors.
resolved = INTERPOLATION_SYNTAX_REGEX.ReplaceAllStringFunc(terragruntConfigString, func(str string) string {
out, err := resolveTerragruntInterpolation(str, include, terragruntOptions)
if err != nil {
finalErr = err
}
return out
})
return
}
// Resolve a single call to an interpolation function of the format ${some_function()} in a .terragrunt file
func resolveTerragruntInterpolation(str string, include *IncludeConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
matches := HELPER_FUNCTION_SYNTAX_REGEX.FindStringSubmatch(str)
if len(matches) == 2 {
return executeTerragruntHelperFunction(matches[1], include, terragruntOptions)
} else {
return "", errors.WithStackTrace(InvalidInterpolationSyntax(str))
}
}
// Execute a single Terragrunt helper function and return its value as a string
func executeTerragruntHelperFunction(functionName string, include *IncludeConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
switch functionName {
case "find_in_parent_folders": return findInParentFolders(terragruntOptions)
case "path_relative_to_include": return pathRelativeToInclude(include, terragruntOptions)
default: return "", errors.WithStackTrace(UnknownHelperFunction(functionName))
}
}
// Find a parent .terragrunt file in the parent folders above the current .terragrunt file and return its path
func findInParentFolders(terragruntOptions *options.TerragruntOptions) (string, error) {
previousDir, err := filepath.Abs(filepath.Dir(terragruntOptions.TerragruntConfigPath))
if err != nil {
return "", errors.WithStackTrace(err)
}
// To avoid getting into an accidental infinite loop (e.g. do to cyclical symlinks), set a max on the number of
// parent folders we'll check
for i := 0; i < MAX_PARENT_FOLDERS_TO_CHECK; i++ {
currentDir := filepath.Dir(previousDir)
if currentDir == previousDir {
return "", errors.WithStackTrace(ParentTerragruntConfigNotFound(terragruntOptions.TerragruntConfigPath))
}
configPath := filepath.Join(currentDir, DefaultTerragruntConfigPath)
if util.FileExists(configPath) {
return util.GetPathRelativeTo(configPath, filepath.Dir(terragruntOptions.TerragruntConfigPath))
}
previousDir = currentDir
}
return "", errors.WithStackTrace(CheckedTooManyParentFolders(terragruntOptions.TerragruntConfigPath))
}
// Return the relative path between the included .terragrunt file and the current .terragrunt file
func pathRelativeToInclude(include *IncludeConfig, terragruntOptions *options.TerragruntOptions) (string, error) {
if include == nil {
return ".", nil
}
includedConfigPath, err := ResolveTerragruntConfigString(include.Path, include, terragruntOptions)
if err != nil {
return "", err
}
includePath := filepath.Dir(includedConfigPath)
currentPath := filepath.Dir(terragruntOptions.TerragruntConfigPath)
if !filepath.IsAbs(includePath) {
includePath = filepath.Join(currentPath, includePath)
}
return util.GetPathRelativeTo(currentPath, includePath)
}
// Custom error types
type InvalidInterpolationSyntax string
func (err InvalidInterpolationSyntax) Error() string {
return fmt.Sprintf("Invalid interpolation syntax. Expected syntax of the form '${function_name()}', but got '%s'", string(err))
}
type UnknownHelperFunction string
func (err UnknownHelperFunction) Error() string {
return fmt.Sprintf("Unknown helper function: %s", string(err))
}
type ParentTerragruntConfigNotFound string
func (err ParentTerragruntConfigNotFound) Error() string {
return fmt.Sprintf("Could not find a %s config file in any of the parent folders of %s", DefaultTerragruntConfigPath, string(err))
}
type CheckedTooManyParentFolders string
func (err CheckedTooManyParentFolders) Error() string {
return fmt.Sprintf("Could not find a %s config file in a parent folder of %s after checking %d parent folders", DefaultTerragruntConfigPath, string(err), MAX_PARENT_FOLDERS_TO_CHECK)
}