Add the list block and the module layer with the note module
The list block prints items with a dot, dash, number or checkbox bullet,
wrapping a long item with a hanging indent; a checkbox list ticks its
done items as [√], or [x] on code pages without √. It stands alone or
inside a box, and its items, bullet and done fields carry their enums
and limits as jsonschema tags.
internal/pkg/modules holds modules that build ordinary jobs from their
own payloads: a registry by name, an Env with the clock, time zone and
time layout, strict payload decoding that reports every problem, ErrSkip
and PayloadError. The note module prints a boxed note with an optional
checklist and a time stamp in the footer.
Three routes join the route table: POST /api/v1/print/{module} and
POST /api/v1/preview/{module}, which take ?copies and a reserved
?printer, and GET /api/v1/modules. Print and the module route share one
submit path, and preview shares one too. New error codes:
invalid_payload, invalid_query, unknown_printer, module_not_found and
module_failed; a module that builds an invalid receipt is logged and
reported to Sentry once. [format] gains timezone and time_layout, the
binary embeds the time zone database, and uberbringer preview takes
--module.
The note receipt passed on the TM-T88IV, and its bytes are pinned in
TestPaperReceipts. docs/api.md describes the list block, the module
routes and the note payload; docs/swagger.json is regenerated.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6b2e402f1a
commit
3e9779f71a
69 files changed
+3092
-262
No files matched your search
@@ -118,6 +118,13 @@ type FormatConfig struct {
|
||||
// MaxLines fails a job that would print more lines, all copies
|
||||
// counted. It is a backstop against runaway text.
|
||||
MaxLines int `mapstructure:"max_lines"`
|
||||
|
||||
// Timezone is the zone the times modules print are in: an IANA name
|
||||
// such as "Europe/London", "UTC", or "Local" for the host's zone.
|
||||
Timezone string `mapstructure:"timezone"`
|
||||
|
||||
// TimeLayout is the Go time layout those times are printed in.
|
||||
TimeLayout string `mapstructure:"time_layout"`
|
||||
}
|
||||
|
||||
// QueueConfig configures the print queue.
|
||||
@@ -192,7 +199,9 @@ func Default() Config {
|
||||
RetryDelay: time.Second,
|
||||
},
|
||||
Format: FormatConfig{
|
||||
MaxLines: 2000,
|
||||
MaxLines: 2000,
|
||||
Timezone: "Local",
|
||||
TimeLayout: "02 Jan 15:04",
|
||||
},
|
||||
Queue: QueueConfig{
|
||||
Size: 100,
|
||||
@@ -574,6 +583,13 @@ func (c *Config) Validate() error {
|
||||
check(p.RetryDelay > 0, "printer.retry_delay must be positive")
|
||||
|
||||
check(c.Format.MaxLines > 0, "format.max_lines must be positive")
|
||||
if c.Format.Timezone == "" {
|
||||
errs = append(errs, errors.New(`format.timezone is required: an IANA name such as "Europe/London", "UTC" or "Local"`))
|
||||
} else if _, err := time.LoadLocation(c.Format.Timezone); err != nil {
|
||||
errs = append(errs, fmt.Errorf(`format.timezone: %q is not a known time zone; use an IANA name such as "Europe/London", "UTC" or "Local"`, c.Format.Timezone))
|
||||
}
|
||||
check(showsTime(c.Format.TimeLayout),
|
||||
`format.time_layout: %q shows no date or time; write it in Go's layout, such as "02 Jan 15:04"`, c.Format.TimeLayout)
|
||||
|
||||
check(c.Queue.Size > 0, "queue.size must be positive")
|
||||
check(c.Queue.History >= 0, "queue.history must not be negative")
|
||||
@@ -592,6 +608,16 @@ func (c *Config) Validate() error {
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
// showsTime reports whether layout prints something of the time it
|
||||
// formats: two times that differ in every field format differently. A
|
||||
// layout with none of Go's reference values, such as "HH:MM", prints
|
||||
// itself.
|
||||
func showsTime(layout string) bool {
|
||||
a := time.Date(2001, 2, 3, 4, 5, 6, 0, time.UTC)
|
||||
b := time.Date(2012, 11, 22, 16, 47, 58, 0, time.FixedZone("X", 3600))
|
||||
return a.Format(layout) != b.Format(layout)
|
||||
}
|
||||
|
||||
func validPort(port int) bool {
|
||||
return port >= 1 && port <= 65535
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
// Embeds the zone database, so that IANA names load on any host.
|
||||
_ "time/tzdata"
|
||||
|
||||
"uberbringer/internal/pkg/config"
|
||||
)
|
||||
|
||||
@@ -53,6 +56,9 @@ func TestLoadInstallScriptConfigGetsDefaults(t *testing.T) {
|
||||
if cfg.Format.MaxLines != 2000 || cfg.Printer.BoxLineSpacing != 0 {
|
||||
t.Errorf("format defaults not applied: max_lines %d, box_line_spacing %d", cfg.Format.MaxLines, cfg.Printer.BoxLineSpacing)
|
||||
}
|
||||
if cfg.Format.Timezone != "Local" || cfg.Format.TimeLayout != "02 Jan 15:04" {
|
||||
t.Errorf("format defaults not applied: timezone %q, time_layout %q", cfg.Format.Timezone, cfg.Format.TimeLayout)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadOverridesAndDurations(t *testing.T) {
|
||||
@@ -66,6 +72,8 @@ paper_width = 384
|
||||
box_line_spacing = 24
|
||||
[format]
|
||||
max_lines = 500
|
||||
timezone = "Europe/London"
|
||||
time_layout = "2006-01-02 15:04"
|
||||
[images]
|
||||
allowed_hosts = ["images.lan"]
|
||||
[sentry]
|
||||
@@ -77,6 +85,9 @@ format = "json"
|
||||
if err != nil {
|
||||
t.Fatalf("Load: %v", err)
|
||||
}
|
||||
if cfg.Format.Timezone != "Europe/London" || cfg.Format.TimeLayout != "2006-01-02 15:04" {
|
||||
t.Errorf("format = %+v", cfg.Format)
|
||||
}
|
||||
if cfg.Printer.DialTimeout != 750*time.Millisecond {
|
||||
t.Errorf("DialTimeout = %v, want 750ms", cfg.Printer.DialTimeout)
|
||||
}
|
||||
@@ -154,6 +165,31 @@ func TestLoadErrors(t *testing.T) {
|
||||
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nbox_line_spacing = 256\n",
|
||||
wantErr: "printer.box_line_spacing must be 0-255",
|
||||
},
|
||||
{
|
||||
name: "unknown time zone",
|
||||
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntimezone = \"Europe/Londn\"\n",
|
||||
wantErr: `format.timezone: "Europe/Londn" is not a known time zone`,
|
||||
},
|
||||
{
|
||||
name: "time zone that is a path",
|
||||
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntimezone = \"../etc/passwd\"\n",
|
||||
wantErr: "format.timezone",
|
||||
},
|
||||
{
|
||||
name: "empty time zone",
|
||||
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntimezone = \"\"\n",
|
||||
wantErr: "format.timezone is required",
|
||||
},
|
||||
{
|
||||
name: "time layout without a time",
|
||||
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntime_layout = \"HH:MM\"\n",
|
||||
wantErr: `format.time_layout: "HH:MM" shows no date or time`,
|
||||
},
|
||||
{
|
||||
name: "empty time layout",
|
||||
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\ntime_layout = \"\"\n",
|
||||
wantErr: "format.time_layout",
|
||||
},
|
||||
{
|
||||
name: "unknown format key",
|
||||
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[format]\nmax_line = 10\n",
|
||||
@@ -289,7 +325,8 @@ func TestExampleConfigLoads(t *testing.T) {
|
||||
}
|
||||
if def := config.Default(); cfg.WebServer.RateLimit != def.WebServer.RateLimit ||
|
||||
cfg.Images.MaxBytes != def.Images.MaxBytes || cfg.Printer.Model != def.Printer.Model ||
|
||||
cfg.Format.MaxLines != def.Format.MaxLines || cfg.Printer.BoxLineSpacing != def.Printer.BoxLineSpacing {
|
||||
cfg.Format.MaxLines != def.Format.MaxLines || cfg.Printer.BoxLineSpacing != def.Printer.BoxLineSpacing ||
|
||||
cfg.Format.Timezone != def.Format.Timezone || cfg.Format.TimeLayout != def.Format.TimeLayout {
|
||||
t.Error("example config values drifted from the defaults it documents")
|
||||
}
|
||||
}
|
||||
@@ -447,6 +484,7 @@ func TestLoadEnvironmentErrors(t *testing.T) {
|
||||
{"only spaces", "UBERBRINGER_IMAGES_ALLOWED_HOSTS", " ", "UBERBRINGER_IMAGES_ALLOWED_HOSTS holds only spaces"},
|
||||
{"trailing comma", "UBERBRINGER_WEBSERVER_TRUSTED_PROXIES", "10.0.0.1,", "a list item is empty"},
|
||||
{"number for a flag", "UBERBRINGER_SENTRY_DEBUG", "1", "expected true or false"},
|
||||
{"unknown time zone", "UBERBRINGER_FORMAT_TIMEZONE", "Mars/Olympus", "format.timezone"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
@@ -480,6 +518,8 @@ func TestValue(t *testing.T) {
|
||||
{key: "webserver.trusted_proxies", want: "127.0.0.1,::1"},
|
||||
{key: "images.allowed_hosts", want: "a.lan,b.lan"},
|
||||
{key: "images.allow_private_networks", want: "false"},
|
||||
{key: "format.timezone", want: "Local"},
|
||||
{key: "format.time_layout", want: "02 Jan 15:04"},
|
||||
{key: "printer", wantErr: `"printer" is a section`},
|
||||
{key: "printer.adress", wantErr: `unknown key "printer.adress"`},
|
||||
{key: "printer.port.x", wantErr: `unknown key "printer.port.x"`},
|
||||
|
||||
Reference in new issue
Block a user