Documentation
¶
Overview ¶
Package hclwrite deals with the problem of generating HCL configuration and of making specific surgical changes to existing HCL configurations.
It operates at a different level of abstraction than the main HCL parser and AST, since details such as the placement of comments and newlines are preserved when unchanged.
Index ¶
- Variables
- func Format(src []byte) []byte
- type Attribute
- type Block
- type Body
- func (n *Body) AppendItem(node Node)
- func (n *Body) AppendUnstructuredTokens(seq *TokenSeq)
- func (n *Body) FindAttribute(name string) *Attribute
- func (n *Body) SetAttributeTraversal(name string, traversal hcl.Traversal) *Attribute
- func (n *Body) SetAttributeValue(name string, val cty.Value) *Attribute
- func (n *Body) Tokens() *TokenSeq
- type Expression
- type File
- type Node
- type Token
- type TokenCallback
- type TokenGen
- type TokenSeq
- type Tokens
- type VarRef
Constants ¶
This section is empty.
Variables ¶
var TokenSeqEmpty = TokenSeq([]TokenGen(nil))
TokenSeqEmpty is a TokenSeq that contains no tokens. It can be used anywhere, but its primary purpose is to be assigned as a replacement for a non-empty TokenSeq when eliminating a section of an input file.
Functions ¶
func Format ¶
Format takes source code and performs simple whitespace changes to transform it to a canonical layout style.
Format skips constructing an AST and works directly with tokens, so it is less expensive than formatting via the AST for situations where no other changes will be made. It also ignores syntax errors and can thus be applied to partial source code, although the result in that case may not be desirable.
Types ¶
type Attribute ¶
type Block ¶
type Body ¶
type Body struct { // Items may contain Attribute, Block and Unstructured instances. // Items and AllTokens should be updated only by methods of this type, // since they must be kept synchronized for correct operation. Items []Node AllTokens *TokenSeq // IndentLevel is the number of spaces that should appear at the start // of lines added within this body. IndentLevel int }
func (*Body) AppendItem ¶
func (*Body) AppendUnstructuredTokens ¶
func (*Body) FindAttribute ¶
FindAttribute returns the first attribute item from the body that has the given name, or returns nil if there is currently no matching attribute.
A valid AST has only one definition of each attribute, but that constraint is not enforced in the hclwrite AST, so a tree that has been mutated by other calls may contain additional matching attributes that cannot be seen by this method.
func (*Body) SetAttributeTraversal ¶
SetAttributeTraversal either replaces the expression of an existing attribute of the given name or adds a new attribute definition to the end of the block.
The new expression is given as a hcl.Traversal, which must be an absolute traversal. To set a literal value, use SetAttributeValue.
The return value is the attribute that was either modified in-place or created.
func (*Body) SetAttributeValue ¶
SetAttributeValue either replaces the expression of an existing attribute of the given name or adds a new attribute definition to the end of the block.
The value is given as a cty.Value, and must therefore be a literal. To set a variable reference or other traversal, use SetAttributeTraversal.
The return value is the attribute that was either modified in-place or created.
type Expression ¶
func (*Expression) Tokens ¶
func (n *Expression) Tokens() *TokenSeq
type File ¶
func ParseConfig ¶
ParseConfig interprets the given source bytes into a *hclwrite.File. The resulting AST can be used to perform surgical edits on the source code before turning it back into bytes again.
func (*File) Bytes ¶
Bytes returns a buffer containing the source code resulting from the tokens underlying the receiving file. If any updates have been made via the AST API, these will be reflected in the result.
type Token ¶
type Token struct { Type hclsyntax.TokenType Bytes []byte // We record the number of spaces before each token so that we can // reproduce the exact layout of the original file when we're making // surgical changes in-place. When _new_ code is created it will always // be in the canonical style, but we preserve layout of existing code. SpacesBefore int }
Token is a single sequence of bytes annotated with a type. It is similar in purpose to hclsyntax.Token, but discards the source position information since that is not useful in code generation.
func (*Token) EachToken ¶
func (t *Token) EachToken(cb TokenCallback)
type TokenCallback ¶
type TokenCallback func(*Token)
TokenCallback is used with TokenGen implementations to specify the action that is to be taken for each token in the flattened token sequence.
type TokenGen ¶
type TokenGen interface {
EachToken(TokenCallback)
}
TokenGen is an abstract type that can append tokens to a list. It is the low-level foundation underlying the hclwrite AST; the AST provides a convenient abstraction over raw token sequences to facilitate common tasks, but it's also possible to directly manipulate the tree of token generators to make changes that the AST API doesn't directly allow.
type TokenSeq ¶
type TokenSeq []TokenGen
TokenSeq combines zero or more TokenGens together to produce a flat sequence of tokens from a tree of TokenGens.
func (*TokenSeq) EachToken ¶
func (ts *TokenSeq) EachToken(cb TokenCallback)
func (*TokenSeq) IsIdent ¶
IsIdent returns true if and only if the token sequence represents a single ident token whose name matches the given string.
func (*TokenSeq) SoloToken ¶
SoloToken returns the single token represented by the receiving sequence, or nil if the sequence does not represent exactly one token.
type Tokens ¶
type Tokens []*Token
Tokens is a flat list of tokens.
func (Tokens) Columns ¶
Columns returns the number of columns (grapheme clusters) the token sequence occupies. The result is not meaningful if there are newline or single-line comment tokens in the sequence.
func (Tokens) EachToken ¶
func (ts Tokens) EachToken(cb TokenCallback)