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: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ go build -o noty .

## Config resolution

`--config PATH` → `$NOTY_CONFIG` → `./noty.yaml` →
`~/.config/noty/config.yaml` → `/etc/noty/config.yaml`
`--config PATH` → `$NOTY_CONFIG` → `~/.config/noty/config.yaml` (or
`.yml`) → `/etc/noty/config.yaml`

The current directory is never searched: a config can run `exec`
commands, so a checked-out repo must not be able to supply one. Use
`-c ./noty.yaml` for a project-local file.

See `noty.example.yaml` for a full example with eight channels.

Expand Down
5 changes: 4 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func Run(args []string, stdin *os.File, stdout, stderr io.Writer) error {
fs.Usage = func() { usage(fs) }

var (
cfgPath = fs.String("config", "", "config file path (env: NOTY_CONFIG; default: search noty.yaml, ~/.config/noty/config.yaml, /etc/noty/config.yaml)")
cfgPath = fs.String("config", "", "config file path (env: NOTY_CONFIG; default: ~/.config/noty/config.yaml, then /etc/noty/config.yaml)")
channels = fs.String("channel", "", "channel name(s) to send through, comma-separated (default: defaults.channel from config)")
message = fs.String("message", "", "message text (default: read from stdin when piped)")
subject = fs.String("subject", "", "subject override")
Expand Down Expand Up @@ -140,6 +140,9 @@ func Run(args []string, stdin *os.File, stdout, stderr io.Writer) error {
to = d
}
}
if to <= 0 {
return fmt.Errorf("timeout must be positive, got %v", to)
}

// --- Flag vars ---
flagVars := map[string]string{}
Expand Down
14 changes: 14 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,3 +369,17 @@ func TestRetryDelay(t *testing.T) {
}
}
}

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") {
t.Errorf("negative --timeout should be rejected, got %v", err)
}
zero := filepath.Join(t.TempDir(), "noty.yaml")
if err := os.WriteFile(zero, []byte("defaults:\n timeout: 0s\nchannels:\n c:\n type: exec\n command: \"true\"\n"), 0o600); err != nil {
t.Fatal(err)
}
if err := run("-c", zero, "-m", "hi", "--channel", "c"); err == nil || !strings.Contains(err.Error(), "timeout") {
t.Errorf("defaults.timeout 0s should be rejected, got %v", err)
}
}
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ func expandEnv(n *yaml.Node, missing *[]string) {
n.Value = v
if n.Style == 0 {
n.Tag = ""
if v == "~" || strings.EqualFold(v, "null") {
n.Tag = "!!str"
}
}
}
}
Expand Down Expand Up @@ -214,7 +217,6 @@ func DefaultPaths() []string {
if env := os.Getenv("NOTY_CONFIG"); env != "" {
paths = append(paths, env)
}
paths = append(paths, "noty.yaml", "noty.yml")
if home, err := os.UserHomeDir(); err == nil {
paths = append(paths,
filepath.Join(home, ".config", "noty", "config.yaml"),
Expand Down
15 changes: 15 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestLoadEnvValuesAreNotParsedAsYAML(t *testing.T) {
secret := `abc #123: x "y' &z *w`
t.Setenv("NOTY_TEST_SECRET", secret)
t.Setenv("NOTY_TEST_PORT", "2525")
t.Setenv("NOTY_TEST_NULL", "NULL")
path := writeConfig(t, `
# comments may reference ${NOTY_TEST_DEFINITELY_UNSET}
channels:
Expand All @@ -54,6 +55,7 @@ channels:
plain: ${NOTY_TEST_SECRET}
quoted: "${NOTY_TEST_SECRET}"
port: ${NOTY_TEST_PORT}
nullish: ${NOTY_TEST_NULL}
`)
cfg, err := Load(path)
if err != nil {
Expand All @@ -63,13 +65,17 @@ channels:
Plain string `yaml:"plain"`
Quoted string `yaml:"quoted"`
Port int `yaml:"port"`
Null string `yaml:"nullish"`
}
if err := cfg.Channels["c"].Raw.Decode(&got); err != nil {
t.Fatal(err)
}
if got.Plain != secret || got.Quoted != secret {
t.Errorf("plain = %q, quoted = %q, want %q", got.Plain, got.Quoted, secret)
}
if got.Null != "NULL" {
t.Errorf("null = %q, want %q (env values must never decode as YAML null)", got.Null, "NULL")
}
if got.Port != 2525 {
t.Errorf("port = %d, want 2525 (plain scalars must keep resolving as YAML types)", got.Port)
}
Expand Down Expand Up @@ -200,3 +206,12 @@ func TestDiscover(t *testing.T) {
t.Errorf("Discover = %q, want %q", got, path)
}
}

func TestDefaultPathsSkipWorkingDirectory(t *testing.T) {
t.Setenv("NOTY_CONFIG", "")
for _, p := range DefaultPaths() {
if !filepath.IsAbs(p) {
t.Errorf("default search must not include working-directory path %q", p)
}
}
}
Loading