Replace ntfy.state_dir with a top-level [state] dir
Security / Vulnerability check (push) Successful in 45s
Test / Lint and test (push) Successful in 2m1s

The job store is to keep its database in the directory that holds the
ntfy subscriptions' state files, so the directory moves to a section
of its own: [state] dir, or UBERBRINGER_STATE_DIR, in place of
[ntfy] state_dir. For now only the subscriptions use it. It still has
no default and is still required with a subscription, and the checks
at start name state.dir in their errors.

install.sh writes [state] dir into new configs, and the Docker image
sets UBERBRINGER_STATE_DIR. ntfy.state_dir has never been in a
release, so its CHANGELOG entry changes in place and there is no
fallback: a config that still sets it fails to load with an unknown
key until the line moves to [state].

The image itself was not built, since the workstation has no Docker.

Part of stage 1b of docs/plans/job-store.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-12-01 12:21:29 -06:00
1 parent f0b5bf020e
commit 66531e7314
16 files changed
+92 -68

No files matched your search

+3 -3
View File
@@ -36,13 +36,13 @@ func newSubscribers(cfg *config.Config, logger *slog.Logger, syncState func(*os.
if len(subs) == 0 {
return nil, nil
}
dir := cfg.Ntfy.StateDir
dir := cfg.State.Dir
if err := os.MkdirAll(dir, 0o700); err != nil {
return nil, fmt.Errorf("config ntfy.state_dir: %w", err)
return nil, fmt.Errorf("config state.dir: %w", err)
}
probe, err := os.CreateTemp(dir, ".write-check-*")
if err != nil {
return nil, fmt.Errorf("config ntfy.state_dir: %s cannot be written: %w", dir, err)
return nil, fmt.Errorf("config state.dir: %s cannot be written: %w", dir, err)
}
_ = probe.Close()
_ = os.Remove(probe.Name())
+14 -12
View File
@@ -34,7 +34,7 @@ func skipSync(*os.File) error { return nil }
// TestServeWithSubscription runs the daemon with a subscription to a fake
// ntfy server: a message on the stream prints, /healthz lists the
// subscription as connected without changing its status code, and the
// state is saved at shutdown.
// state is saved in state.dir at shutdown.
func TestServeWithSubscription(t *testing.T) {
printer := startFakePrinter(t)
var auth sync.Map
@@ -67,8 +67,8 @@ address = "127.0.0.1"
port = %d
[log]
level = "error"
[ntfy]
state_dir = %q
[state]
dir = %q
[[ntfy.subscriptions]]
name = "home"
server = %q
@@ -138,28 +138,30 @@ token_file = %q
}
}
// The daemon refuses to start with a credentials file it cannot read, or
// one that is empty.
// The daemon refuses to start with a state directory it cannot create, or
// with a credentials file it cannot read or that is empty.
func TestNewSubscribersErrors(t *testing.T) {
dir := t.TempDir()
empty := filepath.Join(dir, "empty")
if err := os.WriteFile(empty, []byte(" \n"), 0o600); err != nil {
t.Fatal(err)
}
cfg := config.Default()
cfg.Ntfy.StateDir = filepath.Join(dir, "state")
state := filepath.Join(dir, "state")
sub := config.DefaultSubscription()
sub.Name, sub.Server, sub.Topics = "home", "https://ntfy.sh", []string{"alerts"}
for _, tt := range []struct {
file, want string
stateDir, tokenFile, want string
}{
{filepath.Join(dir, "missing"), "ntfy.subscriptions[0].token_file"},
{empty, "is empty"},
{empty, "", "config state.dir"},
{state, filepath.Join(dir, "missing"), "ntfy.subscriptions[0].token_file"},
{state, empty, "is empty"},
} {
sub.TokenFile = tt.file
cfg := config.Default()
cfg.State.Dir = tt.stateDir
sub.TokenFile = tt.tokenFile
cfg.Ntfy.Subscriptions = []config.NtfySubscription{sub}
if _, err := newSubscribers(&cfg, slog.New(slog.DiscardHandler), skipSync); err == nil || !strings.Contains(err.Error(), tt.want) {
t.Errorf("token_file %s: err = %v, want %q", tt.file, err, tt.want)
t.Errorf("state.dir %s, token_file %s: err = %v, want %q", tt.stateDir, tt.tokenFile, err, tt.want)
}
}
}
+13 -8
View File
@@ -53,6 +53,7 @@ type Config struct {
Format FormatConfig `mapstructure:"format"`
Ntfy NtfyConfig `mapstructure:"ntfy"`
Queue QueueConfig `mapstructure:"queue"`
State StateConfig `mapstructure:"state"`
Images ImagesConfig `mapstructure:"images"`
Sentry SentryConfig `mapstructure:"sentry"`
Log LogConfig `mapstructure:"log"`
@@ -180,11 +181,6 @@ type NtfyConfig struct {
// marker hides the tag.
Markers map[string]string `mapstructure:"markers"`
// StateDir is the directory each subscription keeps its state in: its
// cursor, the IDs it has seen and the messages it holds. Required when
// a subscription is configured.
StateDir string `mapstructure:"state_dir"`
// Subscriptions are the ntfy servers and topics whose messages print.
// They come from the file only: a list of tables has no environment
// variable.
@@ -319,6 +315,15 @@ type QueueConfig struct {
History int `mapstructure:"history"`
}
// StateConfig configures where the daemon keeps what must survive a
// restart.
type StateConfig struct {
// Dir is the directory each ntfy subscription keeps its state in: its
// cursor, the IDs it has seen and the messages it holds. It has no
// default, and is required when a subscription is configured.
Dir string `mapstructure:"dir"`
}
// ImagesConfig configures image elements.
type ImagesConfig struct {
Timeout time.Duration `mapstructure:"timeout"`
@@ -974,6 +979,9 @@ func (c *Config) Validate() error {
check(c.Queue.Size > 0, "queue.size must be positive")
check(c.Queue.History >= 0, "queue.history must not be negative")
check(len(c.Ntfy.Subscriptions) == 0 || strings.TrimSpace(c.State.Dir) != "",
`state.dir is required when an ntfy subscription is configured, e.g. "/var/lib/uberbringer"`)
img := c.Images
check(img.Timeout > 0, "images.timeout must be positive")
check(img.MaxBytes > 0, "images.max_bytes must be positive")
@@ -1004,9 +1012,6 @@ var (
// The server URL is never quoted, since it could hold a password.
func validateSubscriptions(n NtfyConfig) []error {
var errs []error
if len(n.Subscriptions) > 0 && strings.TrimSpace(n.StateDir) == "" {
errs = append(errs, errors.New(`ntfy.state_dir is required when a subscription is configured, e.g. "/var/lib/uberbringer"`))
}
names := make(map[string]int)
for i, s := range n.Subscriptions {
at := fmt.Sprintf("ntfy.subscriptions[%d]", i)
+7 -1
View File
@@ -39,11 +39,17 @@ func TestLoadInstallScriptConfigGetsDefaults(t *testing.T) {
[printer]
address = "192.168.1.245"
[state]
dir = "/var/lib/uberbringer"
`)
cfg, err := config.Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.State.Dir != "/var/lib/uberbringer" {
t.Errorf("state.dir = %q", cfg.State.Dir)
}
if cfg.Printer.Port != 9100 || cfg.Printer.Model != "tm-t88" || cfg.Printer.CodePage != "cp437" {
t.Errorf("printer defaults not applied: %+v", cfg.Printer)
}
@@ -400,7 +406,7 @@ func TestExampleConfigLoads(t *testing.T) {
cfg.Printer.CutFeed != def.Printer.CutFeed ||
cfg.Format.Timezone != def.Format.Timezone || cfg.Format.TimeLayout != def.Format.TimeLayout ||
cfg.Ntfy.Format != def.Ntfy.Format || len(cfg.Ntfy.Markers) != 0 ||
cfg.Ntfy.StateDir != def.Ntfy.StateDir || len(cfg.Ntfy.Subscriptions) != 0 {
cfg.State != def.State || len(cfg.Ntfy.Subscriptions) != 0 {
t.Error("example config values drifted from the defaults it documents")
}
}
+17 -11
View File
@@ -11,8 +11,9 @@ import (
"uberbringer/internal/pkg/config"
)
// base is a config with the required settings and a state directory.
const base = "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[ntfy]\nstate_dir = \"state\"\n"
// base is a config with the required settings and a state directory, which
// ends in the [ntfy] table, so that keys appended to it set ntfy keys.
const base = "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[state]\ndir = \"state\"\n[ntfy]\n"
func TestLoadSubscriptions(t *testing.T) {
cfg, err := config.Load(writeConfig(t, base+`
@@ -53,6 +54,9 @@ topics = ["long-random-topic-name"]
if err != nil {
t.Fatal(err)
}
if cfg.State.Dir != "state" {
t.Errorf("state.dir = %q, want state", cfg.State.Dir)
}
subs := cfg.Ntfy.Subscriptions
if len(subs) != 3 {
t.Fatalf("%d subscriptions, want 3", len(subs))
@@ -98,8 +102,8 @@ func TestSubscriptionDefaults(t *testing.T) {
if q.Start != "22:00" || q.End != "07:00" || len(q.Days) != 7 || q.Timezone != "" || q.MinPriority != 5 || q.Action != "digest" {
t.Errorf("quiet defaults %+v", q)
}
if cfg := config.Default(); cfg.Ntfy.StateDir != "" || cfg.Ntfy.Subscriptions != nil || cfg.Ntfy.Format.Updates != "print" {
t.Errorf("ntfy defaults %+v", cfg.Ntfy)
if cfg := config.Default(); cfg.State.Dir != "" || cfg.Ntfy.Subscriptions != nil || cfg.Ntfy.Format.Updates != "print" {
t.Errorf("state defaults %+v, ntfy defaults %+v", cfg.State, cfg.Ntfy)
}
}
@@ -151,7 +155,9 @@ func TestLoadSubscriptionErrors(t *testing.T) {
tests := []struct {
name, body, wantErr string
}{
{"no state dir", strings.Replace(base, "state_dir = \"state\"", "", 1) + sub, "ntfy.state_dir is required"},
{"no state dir", strings.Replace(base, "dir = \"state\"", "", 1) + sub, "state.dir is required"},
{"blank state dir", strings.Replace(base, "dir = \"state\"", "dir = \" \"", 1) + sub, "state.dir is required"},
{"state dir under ntfy", "state_dir = \"state\"\n", "unknown keys: ntfy.state_dir"},
{"unknown key", sub + "topic = \"x\"\n", "unknown keys: ntfy.subscriptions[0].topic"},
{"unknown quiet key", quiet + "begin = \"22:00\"\n", "unknown keys: ntfy.subscriptions[0].quiet.begin"},
{"keys that differ in case", sub + "Name = \"x\"\n", "keys that differ only in case: ntfy.subscriptions[0].Name and ntfy.subscriptions[0].name"},
@@ -226,7 +232,7 @@ func TestLoadSubscriptionErrorHidesSecrets(t *testing.T) {
func TestLoadSubscriptionsHaveNoVariable(t *testing.T) {
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
t.Setenv("UBERBRINGER_NTFY_STATE_DIR", "/var/lib/uberbringer")
t.Setenv("UBERBRINGER_STATE_DIR", "/var/lib/uberbringer")
t.Setenv("UBERBRINGER_NTFY_SUBSCRIPTIONS", "home")
t.Setenv("UBERBRINGER_NTFY_SUBSCRIPTIONS_NAME", "home")
t.Setenv("UBERBRINGER_LOG_LEVEL", "loud")
@@ -243,17 +249,17 @@ func TestLoadSubscriptionsHaveNoVariable(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if cfg.Ntfy.StateDir != "/var/lib/uberbringer" || len(cfg.Ntfy.Subscriptions) != 0 {
t.Errorf("ntfy = %+v", cfg.Ntfy)
if cfg.State.Dir != "/var/lib/uberbringer" || len(cfg.Ntfy.Subscriptions) != 0 {
t.Errorf("state = %+v, ntfy = %+v", cfg.State, cfg.Ntfy)
}
}
func TestValueOfSubscriptions(t *testing.T) {
cfg := config.Default()
cfg.Ntfy.StateDir = "/var/lib/uberbringer"
cfg.State.Dir = "/var/lib/uberbringer"
cfg.Ntfy.Subscriptions = []config.NtfySubscription{{Name: "home", Token: "tk_secret"}}
if got, err := cfg.Value("ntfy.state_dir"); err != nil || got != "/var/lib/uberbringer" {
t.Errorf("state_dir = %q, %v", got, err)
if got, err := cfg.Value("state.dir"); err != nil || got != "/var/lib/uberbringer" {
t.Errorf("state.dir = %q, %v", got, err)
}
if got, err := cfg.Value("ntfy.format.updates"); err != nil || got != "print" {
t.Errorf("updates = %q, %v", got, err)