Add config loading and validation.
Strict dibs.toml decoding with line-numbered errors, defaults, and a [sandbox] section for the Lua call limits. Adds `dibs config validate` and `dibs version`.
This commit is contained in:
1 parent
59e35c01f7
commit
463c013056
7 files changed
+1210
No files matched your search
@@ -0,0 +1,133 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/pelletier/go-toml/v2/unstable"
|
||||
)
|
||||
|
||||
// positions maps key paths such as "item[1].interval" or
|
||||
// "notify.webhooks[0].url" to where they appear in the document, so that
|
||||
// validation errors can point at a line.
|
||||
type positions map[string]unstable.Position
|
||||
|
||||
func indexPositions(doc []byte) positions {
|
||||
ps := positions{}
|
||||
var p unstable.Parser
|
||||
p.Reset(doc)
|
||||
|
||||
prefix := ""
|
||||
arrays := map[string]int{}
|
||||
// A parse error stops the index early. The decoder reports it.
|
||||
for p.NextExpression() {
|
||||
e := p.Expression()
|
||||
|
||||
switch e.Kind {
|
||||
case unstable.Table, unstable.ArrayTable:
|
||||
keys, first := keyParts(e.Key())
|
||||
path := strings.Join(keys, ".")
|
||||
if e.Kind == unstable.ArrayTable {
|
||||
n := arrays[path]
|
||||
arrays[path]++
|
||||
path = fmt.Sprintf("%s[%d]", path, n)
|
||||
}
|
||||
prefix = path
|
||||
ps.add(&p, path, first)
|
||||
case unstable.KeyValue:
|
||||
ps.keyValue(&p, prefix, e)
|
||||
}
|
||||
}
|
||||
|
||||
return ps
|
||||
}
|
||||
|
||||
func (ps positions) keyValue(p *unstable.Parser, prefix string, kv *unstable.Node) {
|
||||
keys, first := keyParts(kv.Key())
|
||||
path := strings.Join(keys, ".")
|
||||
|
||||
if prefix != "" {
|
||||
path = prefix + "." + path
|
||||
}
|
||||
|
||||
ps.add(p, path, first)
|
||||
ps.value(p, path, kv.Value())
|
||||
}
|
||||
|
||||
func (ps positions) value(p *unstable.Parser, path string, v *unstable.Node) {
|
||||
switch v.Kind {
|
||||
case unstable.Array:
|
||||
it := v.Children()
|
||||
for i := 0; it.Next(); i++ {
|
||||
el := it.Node()
|
||||
elPath := fmt.Sprintf("%s[%d]", path, i)
|
||||
ps.add(p, elPath, el)
|
||||
ps.value(p, elPath, el)
|
||||
}
|
||||
case unstable.InlineTable:
|
||||
it := v.Children()
|
||||
for it.Next() {
|
||||
if kv := it.Node(); kv.Kind == unstable.KeyValue {
|
||||
ps.keyValue(p, path, kv)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ps positions) add(p *unstable.Parser, path string, n *unstable.Node) {
|
||||
if _, ok := ps[path]; !ok && n != nil {
|
||||
ps[path] = p.Shape(n.Raw).Start
|
||||
}
|
||||
}
|
||||
|
||||
// find returns the position of path, or of its closest present parent, so
|
||||
// that a missing key points at the table it belongs in.
|
||||
func (ps positions) find(path string) (unstable.Position, bool) {
|
||||
for path != "" {
|
||||
if pos, ok := ps[path]; ok {
|
||||
return pos, true
|
||||
}
|
||||
|
||||
i := strings.LastIndexAny(path, ".[")
|
||||
if i < 0 {
|
||||
break
|
||||
}
|
||||
|
||||
path = path[:i]
|
||||
}
|
||||
|
||||
return unstable.Position{}, false
|
||||
}
|
||||
|
||||
// keyAt returns the path of the last key that starts on line at or before
|
||||
// col, or "" if there is none.
|
||||
func (ps positions) keyAt(line, col int) string {
|
||||
best, bestCol := "", 0
|
||||
|
||||
for path, p := range ps {
|
||||
if p.Line == line && p.Column <= col && p.Column >= bestCol {
|
||||
// Prefer the longer path when an array element and its first key
|
||||
// share a position.
|
||||
if p.Column > bestCol || len(path) > len(best) {
|
||||
best, bestCol = path, p.Column
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best
|
||||
}
|
||||
|
||||
func keyParts(it unstable.Iterator) ([]string, *unstable.Node) {
|
||||
var keys []string
|
||||
var first *unstable.Node
|
||||
|
||||
for it.Next() {
|
||||
n := it.Node()
|
||||
if first == nil {
|
||||
first = n
|
||||
}
|
||||
keys = append(keys, string(n.Data))
|
||||
}
|
||||
|
||||
return keys, first
|
||||
}
|
||||
Reference in new issue
Block a user