Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:41 +01:00
1 parent 0295eb1ad2
commit b8a493b93d
138 files changed
+1898 -57607

No files matched your search

+4 -109
View File
@@ -3,33 +3,16 @@ package config
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
"github.com/sirupsen/logrus"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
)
var evalContext = &hcl.EvalContext{
Functions: map[string]function.Function{
"hex": hexFunc,
"bounds": boundsFunc,
"rotary": rotaryFunc,
"volume": volumeFunc,
//"volume": volumeFunc,
//"goto": gotoFunc,
},
}
// Load the configuration file.
func Load(name string) (*Structure, error) {
func Load(name string) (*Config, error) {
log := logrus.WithField("path", name)
log.Debug("loading configuration")
@@ -39,46 +22,14 @@ func Load(name string) (*Structure, error) {
}
var (
config = new(Structure)
config = new(Config)
ctx *hcl.EvalContext
file *hcl.File
meta toml.MetaData
diag hcl.Diagnostics
)
switch ext := strings.ToLower(filepath.Ext(name)); ext {
case ".json":
log.Trace("parsing as JSON")
file, diag = json.Parse(source, name)
case ".hcl", ".cfg", ".conf":
log.Trace("parsing as HCL")
start := hcl.Pos{Line: 1, Column: 1}
file, diag = hclsyntax.ParseConfig(source, name, start)
case ".toml":
log.Trace("parsing as TOML")
if meta, err = toml.Decode(string(source), config); err != nil {
return nil, err
} else if undecoded := meta.Undecoded(); len(undecoded) > 0 {
diag = diag.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Undecoded contents",
Detail: fmt.Sprintf("Cannot read from %s: undecoded items in configuration: %v.", name, undecoded),
})
return nil, diag
}
default:
diag = diag.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported file format",
Detail: fmt.Sprintf("Cannot read from %s: unrecognized file format suffix %q.", name, ext),
})
return nil, diag
}
if diag.HasErrors() {
start := hcl.Pos{Line: 1, Column: 1}
if file, diag = hclsyntax.ParseConfig(source, name, start); diag.HasErrors() {
return nil, diag
}
@@ -130,59 +81,3 @@ func explainSubject(diag *hcl.Diagnostic) bool {
return true
}
var (
hexFunc = function.New(&function.Spec{
Params: []function.Parameter{{
Name: "number",
Type: cty.String,
}},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
v := args[0].AsString()
if strings.HasPrefix(strings.ToLower(v), "0x") {
v = v[2:]
}
i, err := strconv.ParseUint(v, 16, 64)
if err != nil {
return cty.NumberUIntVal(0), err
}
return cty.NumberUIntVal(i), nil
},
})
rotaryFunc = function.New(&function.Spec{
Params: []function.Parameter{{
Name: "action",
Type: cty.String,
}},
Type: function.StaticReturnType(cty.String),
})
boundsFunc = function.New(&function.Spec{
Params: []function.Parameter{{
Name: "width",
Type: cty.Number,
}, {
Name: "height",
Type: cty.Number,
}},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
w := args[0].AsString()
h := args[1].AsString()
return cty.StringVal(w + "x" + h), nil
},
})
volumeFunc = function.New(&function.Spec{
Params: []function.Parameter{{
Name: "level",
Type: cty.Number,
}},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return cty.StringVal("volume:" + args[0].AsString()), nil
},
})
)