From e5aca605e43f34595ea22c92f3a91b8001f68530 Mon Sep 17 00:00:00 2001 From: Matt Edmonston Date: Sat, 14 Nov 2026 05:53:40 +0000 Subject: [PATCH] Let channel subject templates wrap the base subject {{ .Subject }} in a channel subject template was always empty, and -s bypassed channel subject templates, so the example email subject always rendered as "[production] my-app: ". The base subject is now -s, else the rendered defaults.subject. Channel subject/subject_template render with the base as .Subject; channels without one use the base as is. Fixes #38 --- README.md | 8 +++++--- internal/cli/cli.go | 19 ++++++++++--------- internal/cli/cli_test.go | 41 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 8e063ca..47e7a68 100644 --- a/README.md +++ b/README.md @@ -57,8 +57,10 @@ stdin of `/dev/null`, as under cron or systemd, is an empty message. Body: `--template FILE` flag → channel `body_template` (file) → channel `body` (inline) → raw message text. -Subject: `--subject` flag → channel `subject_template` / `subject` → -`defaults.subject` (also a template). +Subject: the base subject is `--subject`, else `defaults.subject` (a +template). A channel `subject_template` / `subject` renders with the base as +`{{ .Subject }}` (e.g. `"[prod] {{ .Subject }}"`); without one the base is +used as is. Template variables merge in order: global `vars` → channel `vars` → `--var key=value` flags (repeatable, later wins). @@ -68,7 +70,7 @@ Template variables merge in order: global `vars` → channel `vars` → | Field | Meaning | |---|---| | `{{ .Message }}` | raw message text | -| `{{ .Subject }}` | resolved subject | +| `{{ .Subject }}` | resolved subject (the base subject inside a channel subject template) | | `{{ .To }}` | recipients from `--to` (`{{ index .To 0 }}` for the first) | | `{{ .Channel }}` | channel name | | `{{ .Vars.key }}` | merged variables | diff --git a/internal/cli/cli.go b/internal/cli/cli.go index cbe5041..67a9953 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -178,18 +178,19 @@ func Run(args []string, stdin *os.File, stdout, stderr io.Writer) error { Hostname: hostname, } - // Subject: flag > channel template/inline > defaults.subject. - subj := *subject - if subj == "" { - inline := ch.Common.Subject - if ch.Common.SubjectTemplate == "" && inline == "" { - inline = cfg.Defaults.Subject // defaults.subject is a template too - } + // Subject: base (flag > defaults.subject), wrapped by the channel template/inline. + base := *subject + if base == "" { var err error - if subj, err = renderer.Resolve(ch.Common.SubjectTemplate, inline, "", data); err != nil { - return "", fmt.Errorf("subject: %w", err) + if base, err = renderer.Resolve("", cfg.Defaults.Subject, "", data); err != nil { + return "", fmt.Errorf("defaults.subject: %w", err) } } + data.Subject = base + subj, err := renderer.Resolve(ch.Common.SubjectTemplate, ch.Common.Subject, base, data) + if err != nil { + return "", fmt.Errorf("subject: %w", err) + } data.Subject = subj // Body: --template flag > channel body_template > channel inline body > raw message. diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index bf91849..5c61220 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -369,3 +369,44 @@ func TestRetryDelay(t *testing.T) { } } } + +func TestRunChannelSubjectWrapsBase(t *testing.T) { + outDir := t.TempDir() + cfg := ` +defaults: + subject: "default {{ .Channel }}" +channels: + c: + type: exec + command: /bin/sh + args: ["-c", "cat > ` + outDir + `/c.out"] + subject: "[prod] {{ .Subject }}" + body: "{{ .Subject }}" + plain: + type: exec + command: /bin/sh + args: ["-c", "cat > ` + outDir + `/plain.out"] + body: "{{ .Subject }}" +` + path := filepath.Join(t.TempDir(), "noty.yaml") + if err := os.WriteFile(path, []byte(cfg), 0o600); err != nil { + t.Fatal(err) + } + cases := []struct { + flags []string + file, want string + }{ + {[]string{"-s", "disk full", "--channel", "c"}, "c.out", "[prod] disk full"}, + {[]string{"--channel", "c"}, "c.out", "[prod] default c"}, + {[]string{"-s", "disk full", "--channel", "plain"}, "plain.out", "disk full"}, + {[]string{"--channel", "plain"}, "plain.out", "default plain"}, + } + for _, tc := range cases { + if err := run(append([]string{"-c", path, "-m", "x"}, tc.flags...)...); err != nil { + t.Fatal(err) + } + if got := readOut(t, filepath.Join(outDir, tc.file)); got != tc.want { + t.Errorf("%v: subject = %q, want %q", tc.flags, got, tc.want) + } + } +}