Categorygithub.com/mashiike/hclutil
modulepackage
0.5.3
Repository: https://github.com/mashiike/hclutil.git
Documentation: pkg.go.dev

# README

hclutil

HCL utility for Golang

GoDoc Go Report Card License

Overview

This package provides middleware and utility functions for easy logging management. It enables color-coded display for different log levels and automatically collects attributes set in the context. This allows developers to have flexible logging recording and analysis capabilities.

Installation

go get github.com/mashiike/hclutil

Usage

sample code

package main

import (
	"fmt"
	"log"

	"github.com/hashicorp/hcl/v2/gohcl"
	"github.com/mashiike/hclutil"
	"github.com/zclconf/go-cty/cty"
)

type Config struct {
	App struct {
		RequiredVresion cty.Value `hcl:"required_version"`
		Name            string    `hcl:"name"`
		Description     string    `hcl:"description"`
	} `hcl:"app,block"`
}

func main() {
	var v Config
	body, writer, diags := hclutil.Parse("./")
	if diags.HasErrors() {
		writer.WriteDiagnostics(diags)
		log.Fatal("parse failed")
	}
	evalCtx := hclutil.NewEvalContext()
	body, evalCtx, diags = hclutil.DecodeLocals(body, evalCtx)
	if diags.HasErrors() {
		writer.WriteDiagnostics(diags)
		log.Fatal("parse failed")
	}
	diags = gohcl.DecodeBody(body, evalCtx, &v)
	if diags.HasErrors() {
		writer.WriteDiagnostics(diags)
		log.Fatal("parse failed")
	}
	fmt.Println("name:", v.App.Name)
	fmt.Println("description:", v.App.Description)

	var vc hclutil.VersionConstraints
	if err := hclutil.UnmarshalCTYValue(v.App.RequiredVresion, &vc); err != nil {
		log.Fatal(err)
	}
	fmt.Println("required_version:", vc.String())
	fmt.Println("v1.2.3 is satisfied:", vc.ValidateVersion("v1.2.3") == nil)
}

NewEvalContext

this function is create new EvalContext with helpful functions.

DecodeLocals

this function is decode locals block and return new body and EvalContext.

UnmarshalCTYValue

this function is unmarshal cty.Value to Any.

License

This project is licensed under the MIT License - see the LICENSE(./LICENCE) file for details.

Contribution

Contributions, bug reports, and feature requests are welcome. Pull requests are also highly appreciated. For more details, please

# Functions

No description provided by the author
DecodeLocals is a helper function to decode locals block.
DumpCTYValue は cty.Value をJSON文字列に変換します。 これは、ログ出力等を行うときのデバッグ用途を想定しています。.
ExtractAttributes interprets the contents of the HCL body as attributes, allowing for the contents to be accessed without prior knowledge of the structure.
MakeFileFunc は file 関数を作成して返します。これは、指定されたパスのファイルを読み込んで返すHCLの関数です。 HCL中での使用例としては以下となります。 ``` text = file("path/to/file") ``` MakeFileFunc return a function that reads the file at the specified path and returns it.
MakeTemplateFileFunc は templatefile 関数を作成して返します。これは、指定されたパスのファイルを読み込んでテンプレートとして処理し、結果を返すHCLの関数です。 HCL中での使用例としては以下となります。 ``` text = templatefile("path/to/file", {key = "value"}) ``` MakeTemplateFileFunc returns a function that creates the templatefile function.
No description provided by the author
MergeVariables merges multiple variables into one.
MustDumpCtyValue は cty.Value をJSON文字列に変換します。 DumpCTYValue と異なり、エラーが発生した場合は panic します.
NewEvalContext は よく使う基本的な関数を登録したEvalContextを作成します。 NewEvalContext creates an EvalContext with basic functions.
Parse は与えられたPathをHCLとして解析します。 Parse parses the given Path as HCL.
ParseExpression は HCLの式をパースします。.
ParseFS は与えられたfs.ReadDirFSをHCLとして解析します。.
RestrictBlock implements the restriction that block.
Strftime は指定されたタイムゾーンでの時間をフォーマットします。 Strftime formats the time in the specified time zone.
StrftimeInZone は指定されたタイムゾーンでの時間をフォーマットします。 StrftimeInZone formats the time in the specified time zone.
No description provided by the author
UnmarshalCTYValue decodes a cty.Value into the value pointed to by v.
VariablesReffarances returns variables reffarance string list in expression.
WithFilePath は file関数やtemplatefile関数で参照するファイルのパスを追加します。.
Withfsys は file関数やtemplatefile関数で参照するファイルシステムを追加します。.
WithUtilFunctions は よく使う基本的な関数を登録したEvalContextを作成します。.
WithValue returns a new EvalContext with path's value set.
WithVariables returns a new EvalContext with parent's variables and variables merged.

# Variables

DurationFunc は指定された文字列をパースして、秒数に変換します。 DurationFunc parses the specified string and converts it to seconds.
No description provided by the author
No description provided by the author
NowFunc は現在時刻を返すHCLの関数です。 NowFunc is a HCL function that returns the current time.
StrftimeFunc は指定されたフォーマットで現在時刻を返すHCLの関数です。 StrftimeFunc is a HCL function that returns the current time in the specified format.
StrftimeInZoneFunc は指定されたタイムゾーンでの時間をフォーマットするHCLの関数です。 StrftimeInZoneFunc is a HCL function that formats the time in the specified time zone.

# Structs

BlockRestrictionSchema is a schema for block restriction.
DiagnosticWriter は hcl.DiagnosticWriter のラッパーです。 通常のDiagnosticWriterに加えて以下の機能を追加します。 Output がターミナルであるのかどうかを検出し、色と幅を自動的に設定します。 Parse済みのファイル情報を保持します。 DiagnosticWriter is a wrapper for hcl.DiagnosticWriter.
InvalidUnmarshalError describes an invalid argument passed to UnmarshalCTYValue.
InvalidVersionError is an error type for invalid version.
No description provided by the author
UnmarshalTypeError describes a type missmatch between the cty.Type and the target type.
VersionConstraintNotSatisfiedError is an error type for version constraint not satisfied.
VersionConstraints is a wrapper of goVersion.Constraints to implement CTYValueMarshaler and CTYValueUnmarshaler interface.

# Interfaces

CTYValueUnmarshaler is the interface implemented by types that can unmarshal.
CTYValueUnmarshaler is an interface for types that can be decoded from a cty.Value.