Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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 |
Expand Down
19 changes: 10 additions & 9 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 41 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
Loading