configcmd and version: the config and version standard commands; darwin /var/run
core/configcmd: "<program> config create|validate|upgrade|path" from a Spec (name, application, minimal/full templates, Validate, Schema), taken over from yamase-core/pkg/configcmd and generalized: Filename, Optional (programs that run on defaults) and None (no configuration). core/version: "<program> version [--json]" over the values each program stamps into its internal/version; JSON keys match yasta's existing output (yavebu reads it), VCS fallback for unstamped builds. paths: macOS CLI/Service system-scope RuntimeDir is /var/run/<app> instead of /run/<app>, which macOS does not have. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ca0f06f978
commit
90132e2d76
13 files changed
+1824
-20
No files matched your search
@@ -0,0 +1,132 @@
|
||||
package configcmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
// UnknownKey is a key of a configuration file that no option of the
|
||||
// program has: a typo, an option of another program, or one a newer
|
||||
// version dropped. The program ignores it, so it silently gets the
|
||||
// default instead of what the operator meant.
|
||||
type UnknownKey struct {
|
||||
// Key is the dotted path ("pre-accept-checks.requre-known-domain");
|
||||
// entries of a list are "listen[1].tls".
|
||||
Key string
|
||||
Line int
|
||||
}
|
||||
|
||||
func (k UnknownKey) String() string { return fmt.Sprintf("%s (line %d)", k.Key, k.Line) }
|
||||
|
||||
// UnknownKeys lists the keys of raw that schema (the program's
|
||||
// configuration struct, e.g. config.Config{}) has no field for, by its
|
||||
// yaml tags. It descends into struct-typed mappings, lists of structs
|
||||
// and maps of structs (a map of named backends: the names are the
|
||||
// operator's, their contents are options). A type with its own
|
||||
// UnmarshalYAML is taken as a whole: it decides itself what it accepts.
|
||||
func UnknownKeys(raw []byte, schema any) ([]UnknownKey, error) {
|
||||
var doc yaml.Node
|
||||
if err := yaml.Unmarshal(raw, &doc); err != nil {
|
||||
return nil, fmt.Errorf("parsing: %w", err)
|
||||
}
|
||||
root := docMapping(&doc)
|
||||
if root == nil || schema == nil {
|
||||
return nil, nil
|
||||
}
|
||||
var out []UnknownKey
|
||||
walkUnknown(nil, reflect.TypeOf(schema), root, &out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// UnknownKeysFile is UnknownKeys for the file at path.
|
||||
func UnknownKeysFile(path string, schema any) ([]UnknownKey, error) {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnknownKeys(raw, schema)
|
||||
}
|
||||
|
||||
var unmarshalerType = reflect.TypeOf((*yaml.Unmarshaler)(nil)).Elem()
|
||||
|
||||
// opaque reports whether t decodes itself (or is no struct/map/list),
|
||||
// so its contents are not checked key by key.
|
||||
func opaque(t reflect.Type) bool {
|
||||
if t.Implements(unmarshalerType) || reflect.PointerTo(t).Implements(unmarshalerType) {
|
||||
return true
|
||||
}
|
||||
switch t.Kind() {
|
||||
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func walkUnknown(path []string, t reflect.Type, n *yaml.Node, out *[]UnknownKey) {
|
||||
t = deref(t)
|
||||
if opaque(t) {
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case n.Kind == yaml.MappingNode && t.Kind() == reflect.Struct:
|
||||
for i := 0; i+1 < len(n.Content); i += 2 {
|
||||
k, v := n.Content[i], n.Content[i+1]
|
||||
sub := append(append([]string(nil), path...), k.Value)
|
||||
ft, ok := fieldType(t, k.Value)
|
||||
if !ok {
|
||||
*out = append(*out, UnknownKey{Key: strings.Join(sub, "."), Line: k.Line})
|
||||
continue
|
||||
}
|
||||
walkUnknown(sub, ft, v, out)
|
||||
}
|
||||
case n.Kind == yaml.MappingNode && t.Kind() == reflect.Map:
|
||||
for i := 0; i+1 < len(n.Content); i += 2 {
|
||||
k, v := n.Content[i], n.Content[i+1]
|
||||
walkUnknown(append(append([]string(nil), path...), k.Value), t.Elem(), v, out)
|
||||
}
|
||||
case n.Kind == yaml.SequenceNode && (t.Kind() == reflect.Slice || t.Kind() == reflect.Array):
|
||||
for i, item := range n.Content {
|
||||
sub := append([]string(nil), path...)
|
||||
if len(sub) > 0 {
|
||||
sub[len(sub)-1] = fmt.Sprintf("%s[%d]", sub[len(sub)-1], i)
|
||||
}
|
||||
walkUnknown(sub, t.Elem(), item, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// WarnUnknown logs every unknown option of the configuration file at
|
||||
// path. A program calls it after loading its configuration: it keeps
|
||||
// running with the defaults for those keys (a typo must not take a
|
||||
// server down), but the operator sees why a setting has no effect.
|
||||
func WarnUnknown(log *slog.Logger, path string, schema any) {
|
||||
unknown, err := UnknownKeysFile(path, schema)
|
||||
if err != nil {
|
||||
return // Load already reported a file it cannot parse
|
||||
}
|
||||
for _, k := range unknown {
|
||||
log.Warn("configuration: unknown option, ignored (the default applies)", "option", k.Key, "line", k.Line, "path", path)
|
||||
}
|
||||
}
|
||||
|
||||
// UnknownCheck is doctor's check of the file at path: ok without
|
||||
// unknown options, else a note listing them.
|
||||
func UnknownCheck(path string, schema any) (ok bool, note string) {
|
||||
unknown, err := UnknownKeysFile(path, schema)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
if len(unknown) == 0 {
|
||||
return true, ""
|
||||
}
|
||||
parts := make([]string, len(unknown))
|
||||
for i, k := range unknown {
|
||||
parts[i] = k.String()
|
||||
}
|
||||
return false, "ignored, the defaults apply: " + strings.Join(parts, ", ")
|
||||
}
|
||||
Reference in new issue
Block a user