Documentation
¶
Overview ¶
Package util provides utility functions for Terragrunt.
Index ¶
- Constants
- func AsTerraformEnvVarJSONValue(value any) (string, error)
- func CanonicalPath(path string, basePath string) (string, error)
- func CanonicalResolvedPath(path, basePath string) (string, error)
- func ContainsPath(path, subpath string) bool
- func Copy(ctx context.Context, dst io.Writer, src io.Reader) (int64, error)
- func CopyFile(source string, destination string) error
- func CopyFolderContents(l log.Logger, source, destination, manifestFile string, opts ...CopyOption) error
- func CopyFolderContentsWithFilter(l log.Logger, source, destination, manifestFile string, ...) error
- func CopyFolderToTemp(source string, tempPrefix string, filter func(path string) bool) (string, error)
- func DefaultWorkingAndDownloadDirs(terragruntConfigPath string) (string, string)
- func DirContainsTFFiles(dirPath string) (bool, error)
- func DoWithRetry(ctx context.Context, actionDescription string, maxRetries int, ...) error
- func EncodeBase64Sha1(str string) string
- func EnsureCacheDir() (string, error)
- func EnsureDirectory(path string) error
- func EnsureTempDir() (string, error)
- func EscapeInterpolationInString(s string) string
- func ExcludeFiltersFromFile(baseDir, filename string) ([]string, error)
- func FileExists(path string) bool
- func FileNotExists(path string) bool
- func FileOrData(maybePath string) (string, error)
- func FileSHA256(filePath string) ([]byte, error)
- func FindTFFiles(rootPath string) ([]string, error)
- func FirstNonEmpty[S ~[]E, E comparable](list S) E
- func GenerateRandomSha256() (string, error)
- func GetExitCode(err error) (int, error)
- func GetFiltersFromFile(baseDir, filename string) ([]string, error)
- func GetRandomTime(lowerBound, upperBound time.Duration) time.Duration
- func GrepFilesWithSuffix(fsys vfs.FS, regex *regexp.Regexp, rootDir, suffix string) (bool, error)
- func HasPathPrefix(path, prefix string) bool
- func IsCommandExecutable(e vexec.Exec, ctx context.Context, command string, args ...string) bool
- func IsDir(path string) bool
- func IsDirectoryEmpty(dirPath string) (bool, error)
- func IsFile(path string) bool
- func IsSymLink(path string) bool
- func IsTFFile(path string) bool
- func JoinTerraformModulePath(modulesFolder string, path string) string
- func KindOf(value any) reflect.Kind
- func ListContainsSublist[S ~[]E, E comparable](list, sublist S) bool
- func ListHasPrefix[S ~[]E, E comparable](list, prefix S) bool
- func ListTfFiles(directoryPath string, walkWithSymlinks bool) ([]string, error)
- func MatchSha256Checksum(file, filename []byte) []byte
- func MatchesAny(regExps []string, s string) bool
- func MergeSlices[S ~[]E, E cmp.Ordered](slicesToMerge ...S) S
- func MoveFile(source string, destination string) error
- func MustWalkTerraformOutput(value any, path ...string) any
- func NewFileManifest(manifestFolder string, manifestFile string) *fileManifest
- func NotifyIfSlow(ctx context.Context, l log.Logger, spinnerW io.Writer, timeout time.Duration, ...) error
- func ParseTimestamp(ts string) (time.Time, error)
- func ReadFileAsString(path string) (string, error)
- func RegexFoundInTFFiles(workingDir string, pattern *regexp.Regexp) (bool, error)
- func RelPathForLog(basePath, targetPath string, showAbsPath bool) string
- func RemoveDuplicates[S ~[]E, E cmp.Ordered](list S) S
- func RemoveDuplicatesKeepLast[S ~[]E, E comparable](list S) S
- func ResolvePath(path string) string
- func SanitizePath(baseDir string, file string) (sanitized string, err error)
- func SkipDirIfIgnorable(dir string) error
- func SpinnerWriter() io.Writer
- func SplitUrls(s, sep string) []string
- func TerragruntExcludes(path string) bool
- func UniqueID() string
- func WalkDirWithSymlinks(root string, externalWalkFn fs.WalkDirFunc) error
- func WriteFileWithSamePermissions(source string, destination string, contents io.Reader) error
- func WriterNotifier(writer io.Writer, notifyFn func(p []byte)) io.Writer
- type CmdOutput
- type CopyOption
- type FatalError
- type KeyLocks
- type Lockfile
- type MaxRetriesExceeded
- type PathIsNotDirectory
- type PathIsNotFile
- type ProcessExecutionError
- type SlowNotifyMsg
- type SyncWriter
- type TrapWriter
Constants ¶
const ( TerraformLockFile = ".terraform.lock.hcl" TerragruntCacheDir = ".terragrunt-cache" TerraformCacheDir = ".terraform" GitDir = ".git" DefaultBoilerplateDir = ".boilerplate" ChecksumReadBlock = 8192 )
const Base62Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
const UniqueIDLength = 6 // Should be good for 62^6 = 56+ billion combinations
Variables ¶
This section is empty.
Functions ¶
func AsTerraformEnvVarJSONValue ¶
AsTerraformEnvVarJSONValue converts the given value to a JSON value that can be passed to OpenTofu/Terraform as an environment variable. For the most part, this converts the value directly to JSON using Go's built-in json.Marshal. However, we have special handling for strings, which with normal JSON conversion would be wrapped in quotes, but when passing them to OpenTofu/Terraform via env vars, we need to NOT wrap them in quotes, so this method adds special handling for that case. For complex types (maps, lists, objects), string values containing ${...} patterns are escaped to $${...} to prevent OpenTofu/Terraform's HCL parser from treating them as variable interpolations.
func CanonicalPath ¶
CanonicalPath returns the canonical version of the given path, relative to the given base path. That is, if the given path is a relative path, assume it is relative to the given base path. A canonical path is an absolute path with all relative components (e.g. "../") fully resolved, which makes it safe to compare paths as strings. If the path is relative, basePath must be absolute or an error is returned.
func CanonicalResolvedPath ¶ added in v1.0.3
CanonicalResolvedPath returns the cleaned absolute path with symlinks resolved best-effort.
func ContainsPath ¶
ContainsPath returns true if path contains the given subpath E.g. path="foo/bar/bee", subpath="bar/bee" -> true E.g. path="foo/bar/bee", subpath="bar/be" -> false (because be is not a directory)
func CopyFolderContents ¶
func CopyFolderContents(l log.Logger, source, destination, manifestFile string, opts ...CopyOption) error
CopyFolderContents copies the files and folders within the source folder into the destination folder. Note that hidden files and folders (those starting with a dot) will be skipped. Will create a specified manifest file that contains paths of all copied files.
Optional behavior is configured through CopyOption values such as WithIncludeInCopy, WithExcludeFromCopy, and WithFastCopy.
func CopyFolderContentsWithFilter ¶
func CopyFolderContentsWithFilter(l log.Logger, source, destination, manifestFile string, filter func(absolutePath string) bool) error
CopyFolderContentsWithFilter copies the files and folders within the source folder into the destination folder.
func CopyFolderToTemp ¶ added in v1.0.0
func CopyFolderToTemp(source string, tempPrefix string, filter func(path string) bool) (string, error)
CopyFolderToTemp creates a temp directory with the given prefix, copies the contents of the source folder into it using the provided filter, and returns the path to the temp directory.
func DefaultWorkingAndDownloadDirs ¶ added in v1.0.0
DefaultWorkingAndDownloadDirs gets the default working and download directories for the given Terragrunt config path.
func DirContainsTFFiles ¶
DirContainsTFFiles checks if the given directory contains any Terraform/OpenTofu files (.tf, .tofu, .tf.json, .tofu.json)
func DoWithRetry ¶
func DoWithRetry(ctx context.Context, actionDescription string, maxRetries int, sleepBetweenRetries time.Duration, logger log.Logger, logLevel log.Level, action func(ctx context.Context) error) error
DoWithRetry runs the specified action. If it returns a value, return that value. If it returns an error, sleep for sleepBetweenRetries and try again, up to a maximum of maxRetries retries. If maxRetries is exceeded, return a MaxRetriesExceeded error.
func EncodeBase64Sha1 ¶
EncodeBase64Sha1 Returns the base 64 encoded sha1 hash of the given string
func EnsureCacheDir ¶ added in v1.0.4
EnsureCacheDir returns the global terragrunt cache directory for the current user.
func EnsureDirectory ¶
EnsureDirectory creates a directory at this path if it does not exist, or error if the path exists and is a file.
func EnsureTempDir ¶ added in v1.0.4
EnsureTempDir returns the global terragrunt temp directory.
func EscapeInterpolationInString ¶ added in v1.0.0
EscapeInterpolationInString escapes HCL interpolation patterns (${...}) in a string by doubling the dollar sign (${ → $${). This is idempotent: already-escaped $${...} patterns are not double-escaped.
func ExcludeFiltersFromFile ¶
ExcludeFiltersFromFile returns a list of filters from the given filename, where each filter starts on a new line.
Note that this is a backwards compatibility implementation for the `--queue-excludes-file` flag, so it's going to append the ! prefix to each filter to negate it.
func FileExists ¶
FileExists returns true if the given file exists.
func FileNotExists ¶
FileNotExists returns true if the given file does not exist.
func FileOrData ¶
FileOrData will read the contents of the data of the given arg if it is a file, and otherwise return the contents by itself. This will return an error if the given path is a directory.
func FileSHA256 ¶
FileSHA256 calculates the SHA256 hash of the file at the given path.
func FindTFFiles ¶
FindTFFiles walks through the directory and returns all OpenTofu/Terraform files (.tf, .tofu, .tf.json, .tofu.json)
func FirstNonEmpty ¶
func FirstNonEmpty[S ~[]E, E comparable](list S) E
FirstNonEmpty returns the first non-empty/non-zero element from the slice, or the zero value if none found.
func GenerateRandomSha256 ¶
func GetExitCode ¶
GetExitCode returns the exit code of a command. If the error does not implement errorCode or is not an exec.ExitError or *errors.MultiError type, the error is returned.
func GetFiltersFromFile ¶
GetFiltersFromFile returns a list of filter queries from the given filename, where each filter query starts on a new line.
func GetRandomTime ¶
GetRandomTime gets a random time duration between the lower bound and upper bound. This is useful because some of our automated tests wound up flooding the AWS API all at once, leading to a "Subscriber limit exceeded" error. TODO: Some of the more exotic test cases fail, but it's not worth catching them given the intended use of this function.
func GrepFilesWithSuffix ¶ added in v1.0.3
GrepFilesWithSuffix returns true if regex matches the contents of any file under rootDir whose name ends with suffix. The walk stops as soon as a match is found. A missing rootDir is not an error; the function returns false.
func HasPathPrefix ¶
HasPathPrefix returns true if path starts with the given path prefix E.g. path="/foo/bar/biz", prefix="/foo/bar" -> true E.g. path="/foo/bar/biz", prefix="/foo/ba" -> false (because ba is not a directory path)
func IsCommandExecutable ¶
IsCommandExecutable returns true if the command can be run to completion without error via the given vexec.Exec.
func IsDirectoryEmpty ¶
IsDirectoryEmpty - returns true if the given path exists and is a empty directory.
func IsSymLink ¶
IsSymLink returns true if the given file is a symbolic link Per https://stackoverflow.com/a/18062079/2308858
func IsTFFile ¶
IsTFFile checks if a given file is a Terraform/OpenTofu file (.tf, .tofu, .tf.json, .tofu.json)
func JoinTerraformModulePath ¶
JoinTerraformModulePath joins two paths together with a double-slash between them, as this is what Terraform uses to identify where a "repo" ends and a path within the repo begins. Note: The Terraform docs only mention two forward-slashes, so it's not clear if on Windows those should be two back-slashes? https://www.terraform.io/docs/modules/sources.html
func ListContainsSublist ¶
func ListContainsSublist[S ~[]E, E comparable](list, sublist S) bool
ListContainsSublist returns true if an instance of the sublist can be found in the given list
func ListHasPrefix ¶
func ListHasPrefix[S ~[]E, E comparable](list, prefix S) bool
ListHasPrefix returns true if list starts with the given prefix list
func ListTfFiles ¶
ListTfFiles returns a list of all TF files in the specified directory.
func MatchSha256Checksum ¶
MatchSha256Checksum returns the SHA256 checksum for the given file and filename.
func MatchesAny ¶
func MergeSlices ¶
MergeSlices combines multiple slices and removes duplicates. Note: This function sorts the result, so original order is not preserved.
func MoveFile ¶
MoveFile attempts to rename a file from source to destination, if this fails due to invalid cross-device link it falls back to copying the file contents and deleting the original file.
func MustWalkTerraformOutput ¶
MustWalkTerraformOutput is a helper utility to deeply return a value from a terraform output.
nil will be returned if the path is invalid
Using an example terraform output:
a = {
b = {
c = "foo"
}
"d" = [
1,
2
]
}
path ["a", "b", "c"] will return "foo"
path ["a", "d", "1"] will return 2
path ["a", "foo"] will return nil
func NewFileManifest ¶
func NotifyIfSlow ¶ added in v1.0.1
func NotifyIfSlow(ctx context.Context, l log.Logger, spinnerW io.Writer, timeout time.Duration, msgs SlowNotifyMsg, fn func() error) error
NotifyIfSlow runs fn and, if it takes longer than timeout, shows a spinner on spinnerW. When fn completes successfully, the spinner is replaced by an INFO log with the done message and elapsed time. When fn returns an error, the spinner is cleared but no success message is logged.
func ReadFileAsString ¶
ReadFileAsString returns the contents of the file at the given path as a string.
func RegexFoundInTFFiles ¶
RegexFoundInTFFiles walks through the directory and checks if any OpenTofu/Terraform files (.tf, .tofu, .tf.json, .tofu.json) contain the given regex pattern
func RelPathForLog ¶ added in v0.99.4
RelPathForLog returns a relative path suitable for logging. If the path cannot be made relative, it returns the original path. Paths that don't start with ".." get a "./" prefix for clarity. If showAbsPath is true, the original targetPath is returned unchanged.
func RemoveDuplicates ¶
RemoveDuplicates returns a new slice with duplicates removed. Note: This function sorts the result, so original order is not preserved.
func RemoveDuplicatesKeepLast ¶
func RemoveDuplicatesKeepLast[S ~[]E, E comparable](list S) S
RemoveDuplicatesKeepLast returns a new slice with duplicates removed, keeping the last occurrence. Unlike RemoveDuplicates, this preserves the relative order of elements.
func ResolvePath ¶ added in v0.99.4
ResolvePath resolves symlinks in a path for consistent comparison across platforms. On macOS, /var is a symlink to /private/var, so paths must be resolved. Returns the original path if symlink resolution fails.
func SanitizePath ¶
SanitizePath resolves a file path within a base directory, returning the sanitized path or an error if it attempts to access anything outside the base directory.
func SkipDirIfIgnorable ¶ added in v1.0.0
SkipDirIfIgnorable checks if an entire directory should be skipped based on the fact that it's in a directory that should never have components discovered in it.
func SpinnerWriter ¶ added in v1.0.1
SpinnerWriter returns os.Stderr if it is an interactive terminal, nil otherwise. Use the returned writer as the spinnerW argument to NotifyIfSlow.
func SplitUrls ¶
SplitUrls slices s into all substrings separated by sep and returns a slice of the substrings between those separators. Taking into account that the `=` sign can also be used as a git tag, e.g. `git@github.com/test.git?ref=feature`
func TerragruntExcludes ¶
func UniqueID ¶
func UniqueID() string
UniqueID returns a unique (ish) id we can use to name resources so they don't conflict with each other. Uses base 62 to generate a 6 character string that's unlikely to collide with the handful of tests we run in parallel. Based on code here:
http://stackoverflow.com/a/9543797/483528
func WalkDirWithSymlinks ¶
func WalkDirWithSymlinks(root string, externalWalkFn fs.WalkDirFunc) error
WalkDirWithSymlinks traverses a directory tree using filepath.WalkDir, following symbolic links and calling the provided function for each file or directory encountered. It handles both regular symlinks and circular symlinks without getting into infinite loops.
func WriteFileWithSamePermissions ¶
WriteFileWithSamePermissions writes a file to the given destination with the given contents using the same permissions as the file at source.
Types ¶
type CopyOption ¶ added in v1.0.4
type CopyOption func(*copyConfig)
CopyOption configures a CopyFolderContents call.
func WithExcludeFromCopy ¶ added in v1.0.4
func WithExcludeFromCopy(patterns ...string) CopyOption
WithExcludeFromCopy adds glob patterns whose matches must be skipped during the copy.
func WithFastCopy ¶ added in v1.0.4
func WithFastCopy() CopyOption
WithFastCopy enables the fast-copy path: patterns compile once through glob.Compile and the source tree is walked once via vfs.WalkDirParallel. See the `fast-copy` strict control for the semantic implications.
func WithIncludeInCopy ¶ added in v1.0.4
func WithIncludeInCopy(patterns ...string) CopyOption
WithIncludeInCopy adds glob patterns that must be copied even when TerragruntExcludes would skip them (for example hidden files).
type FatalError ¶
type FatalError struct {
Underlying error
}
FatalError is error interface for cases that should not be retried.
func (FatalError) Error ¶
func (err FatalError) Error() string
func (FatalError) Unwrap ¶
func (err FatalError) Unwrap() error
type KeyLocks ¶
type KeyLocks struct {
// contains filtered or unexported fields
}
KeyLocks manages a map of locks, each associated with a string key.
type Lockfile ¶
func NewLockfile ¶
type MaxRetriesExceeded ¶
MaxRetriesExceeded is an error that occurs when the maximum amount of retries is exceeded.
func (MaxRetriesExceeded) Error ¶
func (err MaxRetriesExceeded) Error() string
type PathIsNotDirectory ¶
type PathIsNotDirectory struct {
// contains filtered or unexported fields
}
PathIsNotDirectory is returned when the given path is unexpectedly not a directory.
func (PathIsNotDirectory) Error ¶
func (err PathIsNotDirectory) Error() string
type PathIsNotFile ¶
type PathIsNotFile struct {
// contains filtered or unexported fields
}
PathIsNotFile is returned when the given path is unexpectedly not a file.
func (PathIsNotFile) Error ¶
func (err PathIsNotFile) Error() string
type ProcessExecutionError ¶
type ProcessExecutionError struct {
Err error
WorkingDir string
RootWorkingDir string
Command string
Args []string
Output CmdOutput
LogShowAbsPaths bool
DisableSummary bool
}
ProcessExecutionError - error returned when a command fails, contains StdOut and StdErr
func (ProcessExecutionError) Error ¶
func (err ProcessExecutionError) Error() string
func (ProcessExecutionError) ExitStatus ¶
func (err ProcessExecutionError) ExitStatus() (int, error)
func (ProcessExecutionError) Unwrap ¶
func (err ProcessExecutionError) Unwrap() error
type SlowNotifyMsg ¶ added in v1.0.1
type SlowNotifyMsg struct {
// Spinner is shown while the operation is in progress (e.g. "Creating Git worktree for ref main...").
Spinner string
// Done is logged as INFO when the operation completes (e.g. "Created Git worktree for ref main").
Done string
}
SlowNotifyMsg holds the messages for NotifyIfSlow.
type SyncWriter ¶ added in v1.0.0
type SyncWriter struct {
// contains filtered or unexported fields
}
SyncWriter wraps an io.Writer with a mutex to make it safe for concurrent use. This is necessary when multiple goroutines write to the same writer, such as when running terraform commands in parallel during "run --all" operations.
func NewSyncWriter ¶ added in v1.0.0
func NewSyncWriter(w io.Writer) *SyncWriter
NewSyncWriter returns a new SyncWriter that wraps the given writer.
type TrapWriter ¶
type TrapWriter struct {
// contains filtered or unexported fields
}
TrapWriter intercepts any messages received from the `writer` output. Used when necessary to filter logs from terraform.
func NewTrapWriter ¶
func NewTrapWriter(writer io.Writer) *TrapWriter
NewTrapWriter returns a new TrapWriter instance.
func (*TrapWriter) Flush ¶
func (trap *TrapWriter) Flush() error
Flush flushes intercepted messages to the writer.