diff --git a/README.md b/README.md index 899c0d7..a1bf6f5 100644 --- a/README.md +++ b/README.md @@ -61,8 +61,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). @@ -72,7 +74,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 5ebe27f..71a9c33 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -181,18 +181,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 c482c97..f09a04a 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -370,6 +370,47 @@ 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) + } + } +} + func TestRunRejectsNonPositiveTimeout(t *testing.T) { cfg := writeTestConfig(t, t.TempDir()) if err := run("-c", cfg, "-m", "hi", "--timeout", "-1s"); err == nil || !strings.Contains(err.Error(), "timeout") {