Documentation ¶
Overview ¶
Example ¶
package main import ( "fmt" "os" "github.com/mashiike/hclconfig" ) func main() { os.Setenv("HCLCONFIG_VAR", "hoge") type ExampleConfig struct { Value string `hcl:"value"` Groups []*struct { Type string `hcl:"type,label"` Name string `hcl:"name,label"` Value int `hcl:"value"` } `hcl:"group,block"` } var cfg ExampleConfig err := hclconfig.LoadWithBytes(&cfg, "config.hcl", []byte(` value = must_env("HCLCONFIG_VAR") group "type1" "default" { value = 1 } group "type2" "default" { value = group.type1.default.value + 1 } `)) if err != nil { fmt.Println(err) } fmt.Println("value = ", cfg.Value) for _, g := range cfg.Groups { fmt.Printf("group.%s.%s.value = %v\n", g.Type, g.Name, g.Value) } }
Output: value = hoge group.type1.default.value = 1 group.type2.default.value = 2
Index ¶
- Variables
- func AttributeRange(content *hcl.BodyContent, tagName string) *hcl.Range
- func DecodeBody(body hcl.Body, ctx *hcl.EvalContext, cfg interface{}) hcl.Diagnostics
- func DecodeExpression(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics
- func DefaultDiagnosticOutput(w io.Writer)
- func DiagnosticWriter(w hcl.DiagnosticWriter)
- func Functions(functions map[string]function.Function)
- func Load(cfg interface{}, paths ...string) error
- func LoadWithBody(cfg interface{}, ctx *hcl.EvalContext, body hcl.Body) hcl.Diagnostics
- func LoadWithBytes(cfg interface{}, filename string, src []byte) error
- func MakeFileFunc(basePaths ...string) function.Function
- func MakeTemplateFileFunc(newEvalContext func() *hcl.EvalContext, basePaths ...string) function.Function
- func NewDiagnosticError(summary string, detail string, r *hcl.Range) *hcl.Diagnostic
- func NewDiagnosticWarn(summary string, detail string, r *hcl.Range) *hcl.Diagnostic
- func NewEvalContext(paths ...string) *hcl.EvalContext
- func RestrictOnlyOneBlock(content *hcl.BodyContent, blockTypes ...string) hcl.Diagnostics
- func RestrictRequiredBlock(content *hcl.BodyContent, blockTypes ...string) hcl.Diagnostics
- func RestrictUniqueBlockLabels(content *hcl.BodyContent, blockTypes ...string) hcl.Diagnostics
- func Strftime(layout string, loc *time.Location, t time.Time) (string, error)
- func StrftimeInZone(layout string, zone string, t time.Time) (string, error)
- func Variables(variables map[string]cty.Value)
- type BodyDecoder
- type DiagnosticWriterFunc
- type ExpressionDecoder
- type Loader
- func (l *Loader) DefaultDiagnosticOutput(w io.Writer)
- func (l *Loader) DiagnosticWriter(w hcl.DiagnosticWriter)
- func (l *Loader) Functions(functions map[string]function.Function)
- func (l *Loader) Load(cfg interface{}, paths ...string) error
- func (l *Loader) LoadWithBody(cfg interface{}, ctx *hcl.EvalContext, body hcl.Body) hcl.Diagnostics
- func (l *Loader) LoadWithBytes(cfg interface{}, filename string, src []byte) error
- func (l *Loader) NewEvalContext(paths ...string) *hcl.EvalContext
- func (l *Loader) Variables(variables map[string]cty.Value)
- type Restrictor
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DurationFunc = function.New(&function.Spec{ Params: []function.Parameter{ { Name: "d", Type: cty.String, AllowMarked: true, }, }, Type: function.StaticReturnType(cty.Number), Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { durationArg, durationMarks := args[0].Unmark() durationStr := durationArg.AsString() d, err := time.ParseDuration(durationStr) if err != nil { return cty.UnknownVal(cty.Number), err } return cty.NumberFloatVal(float64(d) / float64(time.Second)).WithMarks(durationMarks), nil }, })
var EnvFunc = function.New(&function.Spec{ Params: []function.Parameter{ { Name: "key", Type: cty.String, AllowMarked: true, }, { Name: "default", Type: cty.String, AllowNull: true, }, }, Type: function.StaticReturnType(cty.String), Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { keyArg, keyMarks := args[0].Unmark() key := keyArg.AsString() if value := os.Getenv(key); value != "" { return cty.StringVal(value).WithMarks(keyMarks), nil } if args[1].IsNull() { return cty.StringVal("").WithMarks(keyMarks), nil } return cty.StringVal(args[1].AsString()).WithMarks(keyMarks), nil }, })
var MustEnvFunc = function.New(&function.Spec{ Params: []function.Parameter{ { Name: "key", Type: cty.String, AllowMarked: true, }, }, Type: function.StaticReturnType(cty.String), Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { keyArg, keyMarks := args[0].Unmark() key := keyArg.AsString() value := os.Getenv(key) if value == "" { err := function.NewArgError(0, fmt.Errorf("env `%s` is not set", key)) return cty.UnknownVal(cty.String), err } return cty.StringVal(value).WithMarks(keyMarks), nil }, })
var NowFunc = function.New(&function.Spec{ Params: []function.Parameter{}, Type: function.StaticReturnType(cty.Number), Impl: func(_ []cty.Value, retType cty.Type) (cty.Value, error) { return cty.NumberFloatVal(nowUnixSeconds()), nil }, })
var StrftimeFunc = function.New(&function.Spec{ Params: []function.Parameter{ { Name: "layout", Type: cty.String, AllowMarked: true, }, { Name: "unixSeconds", Type: cty.Number, AllowMarked: true, AllowNull: true, }, }, Type: function.StaticReturnType(cty.String), Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { layoutArg, layoutMarks := args[0].Unmark() layout := layoutArg.AsString() unixSecondsArg, unixSeconcsMarks := args[1].Unmark() var unixSeconds float64 if unixSecondsArg.IsNull() { unixSeconds = nowUnixSeconds() } else { f := unixSecondsArg.AsBigFloat() unixSeconds, _ = f.Float64() } t, err := Strftime(layout, time.Local, unixSecondsToTime(unixSeconds)) if err != nil { return cty.UnknownVal(cty.String), err } return cty.StringVal(t).WithMarks(layoutMarks, unixSeconcsMarks), nil }, })
var StrftimeInZoneFunc = function.New(&function.Spec{ Params: []function.Parameter{ { Name: "layout", Type: cty.String, AllowMarked: true, }, { Name: "timeZone", Type: cty.String, AllowMarked: true, }, { Name: "unixSeconds", Type: cty.Number, AllowMarked: true, AllowNull: true, }, }, Type: function.StaticReturnType(cty.String), Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) { layoutArg, layoutMarks := args[0].Unmark() layout := layoutArg.AsString() zoneArg, zoneMarks := args[1].Unmark() zone := zoneArg.AsString() unixSecondsArg, unixSeconcsMarks := args[2].Unmark() var unixSeconds float64 if unixSecondsArg.IsNull() { unixSeconds = nowUnixSeconds() } else { f := unixSecondsArg.AsBigFloat() unixSeconds, _ = f.Float64() } t, err := StrftimeInZone(layout, zone, unixSecondsToTime(unixSeconds)) if err != nil { return cty.UnknownVal(cty.String), err } return cty.StringVal(t).WithMarks(layoutMarks, zoneMarks, unixSeconcsMarks), nil }, })
Functions ¶
func AttributeRange ¶
func AttributeRange(content *hcl.BodyContent, tagName string) *hcl.Range
func DecodeBody ¶ added in v0.1.0
func DecodeBody(body hcl.Body, ctx *hcl.EvalContext, cfg interface{}) hcl.Diagnostics
func DecodeExpression ¶ added in v0.1.0
func DecodeExpression(expr hcl.Expression, ctx *hcl.EvalContext, val interface{}) hcl.Diagnostics
DecodeExpression is an extension of gohcl.DecodeExpression, which supports Decode to interface{}, etc. when the ExpressionDecoder interface is satisfied.
func DefaultDiagnosticOutput ¶
DefaultDiagnosticOutput specifies the standard diagnostic output destination. If a separate DiagnosticWriter is specified, that setting takes precedence.
func DiagnosticWriter ¶
func DiagnosticWriter(w hcl.DiagnosticWriter)
DiagnosticWriter sets up a Writer to write the diagnostic when an error occurs in the Loader.
func Load ¶
Load considers `paths` as a configuration file county written in HCL and reads *.hcl and *.hcl.json. and assigns the decoded values to the `cfg` values.
func LoadWithBody ¶
func LoadWithBody(cfg interface{}, ctx *hcl.EvalContext, body hcl.Body) hcl.Diagnostics
LoadWithBody assigns a value to `val` using a parsed hcl.Body and hcl.EvalContext. mainly used to achieve partial loading when implementing Restrict functions.
func LoadWithBytes ¶
func MakeFileFunc ¶ added in v0.3.0
func MakeTemplateFileFunc ¶ added in v0.4.0
func NewDiagnosticError ¶
NewDiagnosticError generates a new diagnostic error.
func NewDiagnosticWarn ¶
NewDiagnosticError generates a new diagnostic warn.
func NewEvalContext ¶
func NewEvalContext(paths ...string) *hcl.EvalContext
NewEvalContext creates a new evaluation context.
func RestrictOnlyOneBlock ¶ added in v0.2.0
func RestrictOnlyOneBlock(content *hcl.BodyContent, blockTypes ...string) hcl.Diagnostics
RestrictOnlyOneBlock implements the restriction that unique block per block type
func RestrictRequiredBlock ¶ added in v0.6.0
func RestrictRequiredBlock(content *hcl.BodyContent, blockTypes ...string) hcl.Diagnostics
RestrictRequiredBlock implements the restriction that required block per block type
func RestrictUniqueBlockLabels ¶
func RestrictUniqueBlockLabels(content *hcl.BodyContent, blockTypes ...string) hcl.Diagnostics
RestrictUniqueBlockLabels implements the restriction that labels for each Block be unique.
func StrftimeInZone ¶ added in v0.5.0
Types ¶
type BodyDecoder ¶ added in v0.1.0
type BodyDecoder interface {
DecodeBody(hcl.Body, *hcl.EvalContext) hcl.Diagnostics
}
BodyDecoder is an interface for custom decoding methods. If the load target does not satisfy this interface, gohcl.DecodeBody is used, but if it does, the functions of this interface are used.
type DiagnosticWriterFunc ¶
type DiagnosticWriterFunc func(diag *hcl.Diagnostic) error
DiagnosticWriterFunc is an alias for a function that specifies how to output diagnostics.
func (DiagnosticWriterFunc) WriteDiagnostic ¶
func (f DiagnosticWriterFunc) WriteDiagnostic(diag *hcl.Diagnostic) error
func (DiagnosticWriterFunc) WriteDiagnostics ¶
func (f DiagnosticWriterFunc) WriteDiagnostics(diags hcl.Diagnostics) error
type ExpressionDecoder ¶ added in v0.1.0
type ExpressionDecoder interface {
DecodeExpression(expr hcl.Expression, ctx *hcl.EvalContext) hcl.Diagnostics
}
ExpressionDecoder is used for special decoding. Note that it is not used with gohcl.DecodeExpression.
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader represents config loader.
func (*Loader) DefaultDiagnosticOutput ¶
DefaultDiagnosticOutput specifies the standard diagnostic output destination. If a separate DiagnosticWriter is specified, that setting takes precedence.
func (*Loader) DiagnosticWriter ¶
func (l *Loader) DiagnosticWriter(w hcl.DiagnosticWriter)
DiagnosticWriter sets up a Writer to write the diagnostic when an error occurs in the Loader.
func (*Loader) Load ¶
Load considers `paths` as a configuration file county written in HCL and reads *.hcl and *.hcl.json. and assigns the decoded values to the `cfg` values.
func (*Loader) LoadWithBody ¶
func (l *Loader) LoadWithBody(cfg interface{}, ctx *hcl.EvalContext, body hcl.Body) hcl.Diagnostics
LoadWithBody assigns a value to `val` using a parsed hcl.Body and hcl.EvalContext. mainly used to achieve partial loading when implementing Restrict functions.
func (*Loader) LoadWithBytes ¶
func (*Loader) NewEvalContext ¶
NewEvalContext creates a new evaluation context.
type Restrictor ¶
type Restrictor interface {
Restrict(bodyContent *hcl.BodyContent, ctx *hcl.EvalContext) hcl.Diagnostics
}