hclutils

package
v0.0.0-...-841f565 Latest Latest
Warning

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

Go to latest
Published: Sep 28, 2022 License: Apache-2.0 Imports: 28 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Glob = function.New(&function.Spec{
	Params: []function.Parameter{
		{
			Name: "pattern",
			Type: cty.String,
		},
	},
	Type: function.StaticReturnType(cty.List(cty.String)),
	Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
		pattern := args[0].AsString()
		var paths []cty.Value

		glob, err := filepath.Glob(pattern)
		if err != nil {
			return cty.NilVal, fmt.Errorf("failed to build file list from pattern: %s", err)
		}

		for _, i := range glob {
			paths = append(paths, cty.StringVal(i))
		}

		return cty.ListVal(paths), nil
	},
})

Functions

func BuildStdLibFunctions

func BuildStdLibFunctions(baseDir string) map[string]function.Function

BuildStdLibFunctions returns a combined map of stdlib functions and feeds scope into filesystem functions

func DecodeExpressionFromReflection

func DecodeExpressionFromReflection(exp hcl.Expression, field StructField, eval *hcl.EvalContext) hcl.Diagnostics

DecodeExpressionFromReflection takes a reflected value and decodes an hcl expression into it with an eval context.

func DecodeExpressions

func DecodeExpressions(src interface{}, dest interface{}, eval *hcl.EvalContext) error

DecodeExpressions takes a set of struct pointers (src, dest) extracts src and dest struct fields by tag of HCL checks each hcl field for type of hcl.Expression and decodes the value of that expression into the matching dest struct field.

func DeepMergeFunc

func DeepMergeFunc() function.Function

DeepMergeFunc works the same as the regular merge() but is able to do deeply nested data structures

func DiagToErrWrap

func DiagToErrWrap(diag hcl.Diagnostics) (err error)

DiagToErrWrap combines all diag errors into a single error value If diag.HasErrors() returns false this function returns a nil error

func FileFromPath

func FileFromPath(filename string) (*hcl.File, hcl.Diagnostics)

FileFromPath reads data from a file, parses it as HCL and returns a pointer to an HCLFile struct

func FileFromString

func FileFromString(rawHCL string) (*hcl.File, hcl.Diagnostics)

FileFromString uses a new parser and attempts to load HCL from a string we generate a hash of the string and use it as the filename as the ParseHCL function requires a filename to avoid parsing the same file

func HashFile

func HashFile(file *hclwrite.File, hasher hash.Hash) (hash.Hash, error)

HashFile a convenience method for writing bytes to a hasher. if a hasher isn't provided it defaults to sha256

func IsolateBlocks

func IsolateBlocks(sourceBytes []byte, matcher BlockMatcher, fileName string) (*hclwrite.File, error)

IsolateBlocks takes source HCL bytes and a blockMatcher to isolate matching blocks to a single file (useful for hashing)

func MapStringInterfaceToCty

func MapStringInterfaceToCty(v map[string]interface{}) (variables map[string]cty.Value)

MapStringInterfaceToCty attempts to case a map[string]interface{} to a map[string]cty.Value This will panic if it cannot convert a specified interface

func MapStringStringToCtyObject

func MapStringStringToCtyObject(v map[string]string) cty.Value

MapStringStringToCtyObject returns a cty.Object from map[string]string

func MergeMapStringCtyValue

func MergeMapStringCtyValue(maps ...map[string]cty.Value) map[string]cty.Value

MergeMapStringCtyValue merges n number of map[string]string

func PathRelTo

func PathRelTo(baseDir string) function.Function

PathRelTo constructs a function that takes a dir path, cleans, mkdirs, and returns it

func StringSliceToCtyTuple

func StringSliceToCtyTuple(v []string) cty.Value

StringSliceToCtyTuple returns a slice of strings as a slice of cty.Value

func StructFields

func StructFields(v interface{}) (map[string]StructField, error)

StructFields extracts all HCL tagged struct fields and returns a map of reflections by name If a tagged field does not have a name (hcl:",label") it is excluded

func TraversalToKeys

func TraversalToKeys(traversal hcl.Traversal) []string

TraversalToKeys translates an hcl.Traversal to a slice of string keys.

func TraversalToString

func TraversalToString(traversal hcl.Traversal) string

TraversalToString the same as TraversalToKeys but returns a string representation

Types

type BlockMatcher

type BlockMatcher func(file *hclwrite.File) *hclwrite.Block

BlockMatcher an interface that accepts an HCL file and queries it for matching blocks

type FormatOpts

type FormatOpts struct {
	List      bool
	Write     bool
	Diff      bool
	Check     bool
	Recursive bool
	Input     io.Reader
}

FormatOpts aggregates all of the flags that are available for working with the HCL formatter

func (*FormatOpts) Run

func (f *FormatOpts) Run(path string, matcher *pattern.Matcher) hcl.Diagnostics

Run executes the formatting workflow

type Isolator

type Isolator interface {
	IsolateHCLBlocks() (*hclwrite.File, error)
}

Isolator an interface describing an object that can isolate itself

type SortedAttrs

type SortedAttrs []*hcl.Attribute

SortedAttrs is a slice of HCL attributes that implements sort.Interface

func SortAttributes

func SortAttributes(attributes hcl.Attributes) SortedAttrs

SortAttributes returns a sorted slice of attributes so variable decoding happens in the correct order

func (SortedAttrs) Len

func (s SortedAttrs) Len() int

Len returns the length of a data set

func (SortedAttrs) Less

func (s SortedAttrs) Less(i, j int) bool

Less returns a boolean for which item is less than a comparison item

func (SortedAttrs) Swap

func (s SortedAttrs) Swap(i, j int)

Swap exchanges the values between two indexes

type StructField

type StructField struct {
	Value     reflect.Value
	Field     reflect.StructField
	Tag       reflect.StructTag
	TagString string
	TagValues []string
}

StructField a reflected struct field, and it's parsed tags.

Jump to

Keyboard shortcuts

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