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:
Overlord committed 2026-10-14 00:22:19 +02:00
1 parent 59e35c01f7
commit 463c013056
7 files changed
+1210

No files matched your search

+159
View File
@@ -0,0 +1,159 @@
// Command dibs is a personal restock watcher.
package main
import (
"errors"
"flag"
"fmt"
"io"
"io/fs"
"os"
"runtime"
"runtime/debug"
"git.security-command.org/Overlord/dibs/internal/config"
)
// version is set at build time with -ldflags "-X main.version=...".
var version = "dev"
// builtinPlugins are the plugins embedded in the binary.
var builtinPlugins = []string{"amazon"}
const usage = `Usage:
dibs [--config FILE] [--headless] watch items (TUI unless --headless)
dibs check <url> fetch and parse one URL, print JSON
dibs plugin test <file.lua> <fixture.html> [url]
dibs config validate [--config FILE]
dibs version
`
func main() {
os.Exit(run(os.Args[1:], os.Stdout, os.Stderr))
}
func run(args []string, stdout, stderr io.Writer) int {
if len(args) > 0 {
switch args[0] {
case "config":
return runConfig(args[1:], stdout, stderr)
case "version":
printVersion(stdout)
return 0
case "check", "plugin":
_, _ = fmt.Fprintf(stderr, "dibs %s: not implemented yet\n", args[0])
return 1
case "help", "-h", "--help":
_, _ = fmt.Fprint(stdout, usage)
return 0
}
}
flags, configPath := newFlagSet("dibs", stderr)
flags.Bool("headless", false, "run without the TUI, log to stdout")
if err := flags.Parse(args); err != nil {
return 2
}
if flags.NArg() > 0 {
_, _ = fmt.Fprintf(stderr, "dibs: unknown command %q\n\n%s", flags.Arg(0), usage)
return 2
}
if _, ok := loadConfig(*configPath, stderr); !ok {
return 1
}
_, _ = fmt.Fprintln(stderr, "dibs: watching is not implemented yet")
return 1
}
func runConfig(args []string, stdout, stderr io.Writer) int {
if len(args) == 0 || args[0] != "validate" {
_, _ = fmt.Fprintf(stderr, "dibs config: expected \"validate\"\n\n%s", usage)
return 2
}
flags, configPath := newFlagSet("dibs config validate", stderr)
if err := flags.Parse(args[1:]); err != nil {
return 2
}
cfg, ok := loadConfig(*configPath, stderr)
if !ok {
return 1
}
urls := 0
for _, it := range cfg.Items {
urls += len(it.URLs)
}
_, _ = fmt.Fprintf(stdout, "%s: OK (%s, %s, %s, %s)\n", cfg.Path,
plural(len(cfg.Items), "item"), plural(urls, "URL"),
plural(len(cfg.Accounts), "account"), plural(len(cfg.Plugins), "plugin"))
return 0
}
func plural(n int, noun string) string {
if n == 1 {
return "1 " + noun
}
return fmt.Sprintf("%d %ss", n, noun)
}
// newFlagSet returns a flag set with the --config flag every command takes.
func newFlagSet(name string, stderr io.Writer) (*flag.FlagSet, *string) {
flags := flag.NewFlagSet(name, flag.ContinueOnError)
flags.SetOutput(stderr)
path := flags.String("config", "", "config file (default: dibs.toml in the user config dir)")
return flags, path
}
func loadConfig(path string, stderr io.Writer) (*config.Config, bool) {
if path == "" {
var err error
if path, err = config.DefaultPath(); err != nil {
_, _ = fmt.Fprintf(stderr, "dibs: no config dir: %v\n", err)
return nil, false
}
}
cfg, err := config.Load(path, config.Options{Builtins: builtinPlugins})
if err != nil {
var errs config.Errors
switch {
case errors.As(err, &errs):
_, _ = fmt.Fprintln(stderr, errs)
_, _ = fmt.Fprintf(stderr, "%s in config\n", plural(len(errs), "problem"))
case errors.Is(err, fs.ErrNotExist):
_, _ = fmt.Fprintf(stderr, "dibs: no config at %s: create it or pass --config\n", path)
default:
_, _ = fmt.Fprintf(stderr, "dibs: %v\n", err)
}
return nil, false
}
return cfg, true
}
func printVersion(w io.Writer) {
v, rev := version, ""
if info, ok := debug.ReadBuildInfo(); ok {
if v == "dev" && info.Main.Version != "" && info.Main.Version != "(devel)" {
v = info.Main.Version
}
for _, s := range info.Settings {
if s.Key == "vcs.revision" && len(s.Value) >= 12 {
rev = " " + s.Value[:12]
}
}
}
_, _ = fmt.Fprintf(w, "dibs %s%s (%s, %s/%s)\n", v, rev, runtime.Version(), runtime.GOOS, runtime.GOARCH)
}