Skip to main content

peek_core/
config.rs

1//! General config file support: `~/.config/peek/config.toml` (or XDG_CONFIG_HOME/peek/config.toml).
2//!
3//! Used by peek-cli for default flags and peekd socket path, and by peekd for socket path.
4//! Env vars (e.g. PEEK_PEEKD_SOCKET) override config file values.
5//!
6//! Example `config.toml`:
7//!
8//! ```toml
9//! [defaults]
10//! no-color = false
11//! resolve = true
12//!
13//! [peekd]
14//! socket-path = "/run/peekd/peekd.sock"
15//!
16//! [export]
17//! default-format = "md"
18//! ```
19
20use serde::Deserialize;
21use std::path::PathBuf;
22
23#[derive(Debug, Clone, Default, Deserialize)]
24#[serde(rename_all = "kebab-case", default)]
25pub struct PeekConfig {
26    pub defaults: DefaultsSection,
27    pub peekd: PeekdSection,
28    pub export: ExportSection,
29}
30
31#[derive(Debug, Clone, Default, Deserialize)]
32#[serde(rename_all = "kebab-case", default)]
33pub struct DefaultsSection {
34    /// Default for --no-color (disable colored output).
35    pub no_color: bool,
36    /// Default for --resolve (resolve remote addresses).
37    pub resolve: bool,
38}
39
40#[derive(Debug, Clone, Default, Deserialize)]
41#[serde(rename_all = "kebab-case", default)]
42pub struct PeekdSection {
43    /// Unix socket path for peekd. Overridden by PEEK_PEEKD_SOCKET.
44    pub socket_path: Option<String>,
45    /// Directory for history JSONL files. Overridden by XDG_STATE_HOME / ~/.local/state/peekd.
46    pub history_dir: Option<String>,
47}
48
49#[derive(Debug, Clone, Default, Deserialize)]
50#[serde(rename_all = "kebab-case", default)]
51pub struct ExportSection {
52    /// Default export/output format: json | json_snapshot | md | html | pdf.
53    pub default_format: Option<String>,
54}
55
56/// Returns the path to the config file (XDG_CONFIG_HOME/peek/config.toml or ~/.config/peek/config.toml).
57/// Does not check if the file exists.
58pub fn config_path() -> PathBuf {
59    if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
60        return PathBuf::from(xdg).join("peek").join("config.toml");
61    }
62    if let Ok(home) = std::env::var("HOME") {
63        return PathBuf::from(home)
64            .join(".config")
65            .join("peek")
66            .join("config.toml");
67    }
68    PathBuf::from(".config").join("peek").join("config.toml")
69}
70
71/// Load config from the standard location. Returns `None` if the file is missing or invalid.
72pub fn load_config() -> Option<PeekConfig> {
73    let path = config_path();
74    let raw = std::fs::read_to_string(&path).ok()?;
75    toml::from_str(&raw).ok()
76}