https://github.com/mashiike/hclutil
HCL utilitiy for Golang
https://github.com/mashiike/hclutil
Last synced: 3 months ago
JSON representation
HCL utilitiy for Golang
- Host: GitHub
- URL: https://github.com/mashiike/hclutil
- Owner: mashiike
- License: mit
- Created: 2023-05-16T08:39:43.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-11T20:59:51.000Z (over 1 year ago)
- Last Synced: 2024-05-01T16:15:23.477Z (about 1 year ago)
- Language: Go
- Size: 105 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# hclutil
HCL utility for Golang[](https://godoc.org/github.com/mashiike/hclutil)
[](https://goreportcard.com/report/github.com/mashiike/hclutil)
[](https://opensource.org/licenses/MIT)## 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
```bash
go get github.com/mashiike/hclutil
```## Usage
sample code
```go
package mainimport (
"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