diff --git a/README.md b/README.md index a157766..28c979f 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/internal/cli/cli.go b/internal/cli/cli.go index ba39b70..2291697 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -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") @@ -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{} diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index bf91849..c482c97 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -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) + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 74053fd..94925c4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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" + } } } } @@ -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"), diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 071da42..b97f3b3 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -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: @@ -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 { @@ -63,6 +65,7 @@ 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) @@ -70,6 +73,9 @@ channels: 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) } @@ -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) + } + } +}