Documentation ¶
Overview ¶
Package fmt contains functions for formatting hcl config.
Index ¶
Constants ¶
const ErrHCLSyntax errors.Kind = "HCL syntax error"
ErrHCLSyntax is the error kind for syntax errors.
const ErrReadFile errors.Kind = "failed to read file"
ErrReadFile is the error kind for any error related to reading the file content.
Variables ¶
This section is empty.
Functions ¶
func Format ¶
Format will format the given source code using hcl.Format. It returns an error if the given source is invalid HCL.
func FormatAttributes ¶
FormatAttributes will format a given attribute map to a string. Name of the attributes are the keys and the corresponding value is mapped. The formatted output will always be lexicographically sorted by the attribute name, so calling this function with the same map multiple times produces the same result.
func FormatMultiline ¶
FormatMultiline will format the given source code. It enforces lists to be formatted as multiline, where each element on the list resides on its own line followed by a comma.
It returns an error if the given source is invalid HCL.
Types ¶
type FormatResult ¶
type FormatResult struct {
// contains filtered or unexported fields
}
FormatResult represents the result of a formatting operation.
func FormatFiles ¶ added in v0.5.0
func FormatFiles(basedir string, files []string) ([]FormatResult, error)
FormatFiles will format all the provided Terramate paths. Only Terramate configuration files can be reliably formatted with this function. If HCL files for a different tool is provided, the result is unpredictable.
Note: The provided file paths can be absolute or relative. If relative, ensure working directory is corrected adjusted. The special `-` filename is treated as a normal filename, then if it needs to be interpreted as `stdin` this needs to be handled separately by the caller.
Files that are already formatted are ignored. If all files are formatted this function returns an empty result.
All files will be left untouched. To save the formatted result on disk you can use FormatResult.Save for each FormatResult.
func FormatTree ¶
func FormatTree(dir string) ([]FormatResult, error)
FormatTree will format all Terramate configuration files in the given tree starting at the given dir. It will recursively navigate on sub directories. Directories starting with "." are ignored.
Only Terramate configuration files will be formatted.
Files that are already formatted are ignored. If all files are formatted this function returns an empty result.
All files will be left untouched. To save the formatted result on disk you can use FormatResult.Save for each FormatResult.
func (FormatResult) Formatted ¶
func (f FormatResult) Formatted() string
Formatted is the contents of the original file after formatting.
func (FormatResult) Path ¶
func (f FormatResult) Path() string
Path is the absolute path of the original file.
func (FormatResult) Save ¶
func (f FormatResult) Save() error
Save will save the formatted result on the original file, replacing its original contents.