fmt

package
v0.6.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 17, 2024 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package fmt contains functions for formatting hcl config.

Index

Constants

View Source
const ErrHCLSyntax errors.Kind = "HCL syntax error"

ErrHCLSyntax is the error kind for syntax errors.

View Source
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

func Format(src, filename string) (string, error)

Format will format the given source code using hcl.Format. It returns an error if the given source is invalid HCL.

func FormatAttributes

func FormatAttributes(attrs map[string]cty.Value) string

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

func FormatMultiline(src, filename string) (string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL