Skip to main content

doiget_cli/commands/
config.rs

1//! `doiget config <action>` — config introspection.
2//!
3//! This subcommand is intentionally read-only and does NOT touch the network
4//! or instantiate the Store. Phase 1 resolves config from environment
5//! variables only with default fallbacks; the user `config.toml` reader
6//! lands in a follow-up. See `docs/CONFIG.md` for the canonical schema.
7//!
8//! `print_stdout` is denied workspace-wide for MCP stdio safety (ADR-0001 /
9//! `docs/SECURITY.md` §3). The `config show` and `config path` actions are
10//! the *spec'd* stdout channel for human-facing introspection — they are
11//! never invoked from inside an MCP session (`doiget serve` runs a
12//! different code path), so the lint is locally relaxed below.
13
14use anyhow::Result;
15use camino::Utf8PathBuf;
16
17use super::fetch::CliExit;
18
19/// Snapshot of the env-var + default-fallback config that `doiget` would
20/// use on the current machine.
21///
22/// Phase 1 surface: env vars only (`DOIGET_STORE_ROOT`, `DOIGET_LOG_PATH`,
23/// `DOIGET_CONTACT_EMAIL`, `DOIGET_UNPAYWALL_EMAIL`) layered over
24/// XDG / known-folder defaults. Phase 2 will layer the user config.toml
25/// underneath the env vars per `docs/CONFIG.md` §1.
26///
27/// Issue #142: `log_path` is resolved from `DOIGET_LOG_PATH` — the ONLY
28/// log env var `docs/CONFIG.md` §4 documents — using the exact same
29/// resolution the provenance-log *writer*
30/// (`commands::fetch::resolve_log_path` / `commands::audit_log`) uses, so
31/// `config show` reports the path the writer actually uses. The previously
32/// read, undocumented `DOIGET_LOG_DIR` has been dropped.
33#[derive(Debug, serde::Serialize)]
34pub struct ResolvedConfig {
35    /// Root of the on-disk paper store. Default: `$HOME/papers`.
36    pub store_root: Utf8PathBuf,
37    /// Directory holding doiget's append-only logs. Derived from
38    /// `log_path`'s parent so it always agrees with the writer.
39    pub log_dir: Utf8PathBuf,
40    /// JSON-Lines provenance log file path. `DOIGET_LOG_PATH` when set,
41    /// otherwise `<config_dir>/doiget/access.jsonl` (`docs/CONFIG.md` §4).
42    pub log_path: Utf8PathBuf,
43    /// Directory holding `config.toml` and `credentials.toml`.
44    pub config_dir: Utf8PathBuf,
45    /// Path of the user config file (may not exist on disk yet).
46    pub config_path: Utf8PathBuf,
47    /// Contact email for the polite User-Agent header (and Unpaywall fallback).
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub contact_email: Option<String>,
50    /// Unpaywall-specific contact email; falls back to `contact_email` when unset.
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub unpaywall_email: Option<String>,
53}
54
55impl ResolvedConfig {
56    /// Resolve the live config from process environment + platform defaults.
57    ///
58    /// Errors only if neither a home directory nor a config directory can
59    /// be determined for the current user (e.g. an unknown / locked-down
60    /// platform); on every realistic POSIX or Windows host this returns
61    /// `Ok` even with no `DOIGET_*` env vars set.
62    pub fn from_env() -> Result<Self> {
63        // `dirs::home_dir()` / `dirs::config_dir()` return `std::path::PathBuf`;
64        // hoist them into `Utf8PathBuf` immediately at the OS boundary so the
65        // rest of the function (and the public struct) stays UTF-8-only per
66        // the workspace `disallowed-types` clippy rule. A non-UTF-8 home dir
67        // is exotic and unsupported; surface it as an explicit error.
68        let home =
69            Utf8PathBuf::try_from(dirs::home_dir().ok_or_else(|| anyhow::anyhow!("no home dir"))?)?;
70        let cfg = Utf8PathBuf::try_from(
71            dirs::config_dir().ok_or_else(|| anyhow::anyhow!("no config dir"))?,
72        )?;
73
74        let store_root = std::env::var("DOIGET_STORE_ROOT")
75            .map(Utf8PathBuf::from)
76            .unwrap_or_else(|_| home.join("papers"));
77
78        // Issue #142: resolve the log path the SAME way the writer does
79        // (`commands::fetch::resolve_log_path` / `commands::audit_log`):
80        // `DOIGET_LOG_PATH` (the only log env var documented in
81        // `docs/CONFIG.md` §4) when set, otherwise
82        // `<config_dir>/doiget/access.jsonl`. The undocumented
83        // `DOIGET_LOG_DIR` is no longer read, so `config show` can no
84        // longer disagree with the path the provenance log is written to.
85        let log_path = match std::env::var("DOIGET_LOG_PATH") {
86            Ok(s) if !s.is_empty() => Utf8PathBuf::from(s),
87            _ => cfg.join("doiget").join("access.jsonl"),
88        };
89        // `log_dir` is purely derived from `log_path` so the two can never
90        // drift; fall back to the config dir for a path with no parent.
91        let log_dir = log_path
92            .parent()
93            .map(Utf8PathBuf::from)
94            .unwrap_or_else(|| cfg.join("doiget"));
95
96        let config_dir = cfg.join("doiget");
97        let config_path = config_dir.join("config.toml");
98
99        Ok(Self {
100            store_root,
101            log_dir,
102            log_path,
103            config_dir,
104            config_path,
105            contact_email: std::env::var("DOIGET_CONTACT_EMAIL").ok(),
106            unpaywall_email: std::env::var("DOIGET_UNPAYWALL_EMAIL").ok(),
107        })
108    }
109}
110
111/// Dispatch entrypoint for `doiget config <action>`.
112///
113/// `action` is one of `show`, `path`, `doctor`. Anything else returns
114/// `Err`; clap currently passes the raw string through.
115//
116// `print_stdout` and `print_stderr` are workspace-deny / workspace-warn for
117// MCP stdio safety. The `config` subcommand is the explicit human-facing
118// stdout channel for the resolved config; `doctor`'s checklist lines also
119// belong on stderr by design (stdout stays clean for `| jq` style pipes
120// when we add `--json` later).
121#[allow(clippy::print_stdout, clippy::print_stderr)]
122pub fn run(action: String, mode: super::output::OutputMode) -> Result<()> {
123    // `mode` honors ADR-0017: `Quiet` suppresses the TOML dump (`show`)
124    // and the path println! (`path`); `doctor` is unaffected because its
125    // per-check output is on stderr and only the failure/success exit
126    // code is the user-visible signal (#203). Json body for `show` is
127    // tracked in #204.
128    let cfg = ResolvedConfig::from_env()?;
129    match action.as_str() {
130        "show" => match mode {
131            super::output::OutputMode::Quiet => {}
132            super::output::OutputMode::Json => {
133                // #204: `ResolvedConfig` is `Serialize` (already used for
134                // the TOML branch).
135                let s = serde_json::to_string_pretty(&cfg)
136                    .map_err(|e| anyhow::anyhow!("serialise config to JSON: {e}"))?;
137                println!("{s}");
138            }
139            _ => {
140                let s = toml::to_string_pretty(&cfg)?;
141                print!("{s}");
142            }
143        },
144        "path" => match mode {
145            super::output::OutputMode::Quiet => {}
146            super::output::OutputMode::Json => {
147                // Minimal JSON object so callers can parse the path
148                // uniformly; no trailing-newline ambiguity vs the raw
149                // `path` form.
150                println!(
151                    "{}",
152                    serde_json::json!({ "config_path": cfg.config_path.as_str() })
153                );
154            }
155            _ => {
156                println!("{}", cfg.config_path);
157            }
158        },
159        "doctor" => {
160            let mut all_ok = true;
161            check(
162                "store_root parent exists",
163                cfg.store_root.parent().map(|p| p.exists()).unwrap_or(true),
164                &mut all_ok,
165            );
166            check(
167                "log_dir parent exists",
168                cfg.log_dir.parent().map(|p| p.exists()).unwrap_or(true),
169                &mut all_ok,
170            );
171            check(
172                "contact_email set",
173                cfg.contact_email.is_some(),
174                &mut all_ok,
175            );
176            // Trying to actually create the dirs would have side-effects;
177            // keep doctor read-only and just check existence of parents.
178            if !all_ok {
179                // Issue #149: a failing doctor means missing/invalid
180                // config — `docs/ERRORS.md` §4 classes "missing config"
181                // as misuse → exit 2 (the per-check `[FAIL]` lines were
182                // already written to stderr by `check`).
183                eprintln_err("error: config doctor: one or more checks failed");
184                return Err(anyhow::Error::new(CliExit(2)));
185            }
186        }
187        other => {
188            // Issue #149: an unknown subcommand action is clear argument
189            // misuse → `docs/ERRORS.md` §4 exit 2, not the generic exit 1
190            // a bare `bail!` produced.
191            eprintln_err(&format!(
192                "error: unknown config action: {other}; expected `show` / `path` / `doctor`"
193            ));
194            return Err(anyhow::Error::new(CliExit(2)));
195        }
196    }
197    Ok(())
198}
199
200/// Stderr sink for the `docs/ERRORS.md` §3 human-error lines. The
201/// localized `#[allow]` is the minimal intervention for the workspace
202/// `clippy::print_stderr` lint (same pattern as `commands::fetch`).
203#[allow(clippy::print_stderr)]
204fn eprintln_err(msg: &str) {
205    eprintln!("{msg}");
206}
207
208/// Emit one `[ ok ]` / `[FAIL]` checklist line to stderr and update the
209/// running pass/fail flag. Stderr is used so that `doiget config doctor`
210/// stdout stays empty for green runs (script-friendly).
211#[allow(clippy::print_stderr)]
212fn check(label: &str, ok: bool, all_ok: &mut bool) {
213    let mark = if ok { "[ ok ]" } else { "[FAIL]" };
214    eprintln!("{mark} {label}");
215    if !ok {
216        *all_ok = false;
217    }
218}
219
220// ---------------------------------------------------------------------------
221// Tests — env-mutating, serialized via serial_test (same convention as
222// `doiget-core::tests`). Each test resets the four env vars it touches via
223// an EnvGuard RAII drop guard so that prior values are restored on panic.
224// ---------------------------------------------------------------------------
225#[cfg(test)]
226mod tests {
227    #![allow(clippy::expect_used, clippy::unwrap_used, clippy::panic)]
228
229    use super::*;
230
231    /// RAII guard that captures the prior value of an env var on
232    /// construction and restores it on drop. Mirrors the convention in
233    /// `crates/doiget-core/src/lib.rs::tests`.
234    struct EnvGuard {
235        var: &'static str,
236        prior: Option<std::ffi::OsString>,
237    }
238
239    impl EnvGuard {
240        fn unset(var: &'static str) -> Self {
241            let prior = std::env::var_os(var);
242            // SAFETY: tests are serialized via `#[serial_test::serial]`;
243            // no other thread reads/writes env state concurrently.
244            std::env::remove_var(var);
245            EnvGuard { var, prior }
246        }
247
248        fn set(var: &'static str, value: &str) -> Self {
249            let prior = std::env::var_os(var);
250            std::env::set_var(var, value);
251            EnvGuard { var, prior }
252        }
253    }
254
255    impl Drop for EnvGuard {
256        fn drop(&mut self) {
257            match &self.prior {
258                Some(v) => std::env::set_var(self.var, v),
259                None => std::env::remove_var(self.var),
260            }
261        }
262    }
263
264    /// Unset every env var the `config` subcommand reads. Returns guards
265    /// that restore prior values on drop.
266    fn unset_all_doiget_config_env() -> Vec<EnvGuard> {
267        [
268            "DOIGET_STORE_ROOT",
269            "DOIGET_LOG_PATH",
270            "DOIGET_CONTACT_EMAIL",
271            "DOIGET_UNPAYWALL_EMAIL",
272        ]
273        .iter()
274        .map(|v| EnvGuard::unset(v))
275        .collect()
276    }
277
278    #[test]
279    #[serial_test::serial]
280    fn from_env_uses_home_default_when_unset() {
281        let _g = unset_all_doiget_config_env();
282        let cfg = ResolvedConfig::from_env().expect("home dir must resolve on test host");
283        assert!(
284            cfg.store_root.as_str().ends_with("papers"),
285            "store_root should fall back to <home>/papers when DOIGET_STORE_ROOT is unset; got {}",
286            cfg.store_root
287        );
288        assert_eq!(cfg.contact_email, None);
289        assert_eq!(cfg.unpaywall_email, None);
290    }
291
292    #[test]
293    #[serial_test::serial]
294    fn from_env_overrides_via_env() {
295        let _g = unset_all_doiget_config_env();
296        // Use a platform-appropriate absolute path so Utf8PathBuf::try_from
297        // succeeds on Windows too (where "/tmp/foo" is a relative path on
298        // the current drive — still UTF-8, still fine for this assertion).
299        let _override = EnvGuard::set("DOIGET_STORE_ROOT", "/tmp/foo");
300        let cfg = ResolvedConfig::from_env().expect("home dir must resolve on test host");
301        assert_eq!(cfg.store_root.as_str(), "/tmp/foo");
302    }
303
304    /// Issue #142: `config show` MUST report the same `log_path` the
305    /// provenance-log writer uses. The writer keys off `DOIGET_LOG_PATH`
306    /// (the only log env var documented in `docs/CONFIG.md` §4); the
307    /// resolver must do the same, and `log_dir` must be that path's
308    /// parent — never an independently-resolved (and divergent) value.
309    #[test]
310    #[serial_test::serial]
311    fn log_path_follows_doiget_log_path_env() {
312        let _g = unset_all_doiget_config_env();
313        let _override = EnvGuard::set("DOIGET_LOG_PATH", "/var/lib/doiget/access.jsonl");
314        let cfg = ResolvedConfig::from_env().expect("home dir must resolve on test host");
315        assert_eq!(
316            cfg.log_path.as_str(),
317            "/var/lib/doiget/access.jsonl",
318            "config show must echo DOIGET_LOG_PATH verbatim (issue #142)"
319        );
320        assert_eq!(
321            cfg.log_dir.as_str(),
322            "/var/lib/doiget",
323            "log_dir must be derived from log_path's parent so the two cannot drift"
324        );
325    }
326
327    #[test]
328    #[serial_test::serial]
329    fn doctor_fails_without_contact_email() {
330        // Issue #149: a failing doctor is "missing config" → exit 2.
331        // The human-readable line moved to stderr; the error now carries
332        // a `CliExit(2)` rather than a Display-formatted anyhow string.
333        let _g = unset_all_doiget_config_env();
334        let err = run("doctor".into(), crate::commands::output::OutputMode::Human)
335            .expect_err("doctor should fail when DOIGET_CONTACT_EMAIL is unset");
336        let cli_exit = err
337            .downcast_ref::<CliExit>()
338            .expect("failing doctor must carry a CliExit (issue #149)");
339        assert_eq!(
340            cli_exit.0, 2,
341            "missing/invalid config is misuse → exit 2, not the generic exit 1"
342        );
343    }
344
345    #[test]
346    #[serial_test::serial]
347    fn doctor_passes_with_contact_email() {
348        let _g = unset_all_doiget_config_env();
349        let _email = EnvGuard::set("DOIGET_CONTACT_EMAIL", "alice@example.org");
350        // home_dir() / config_dir() resolve to real, existing parents on
351        // every supported test host (CI runners always have $HOME).
352        run("doctor".into(), crate::commands::output::OutputMode::Human)
353            .expect("doctor should pass with contact email + real home dir");
354    }
355
356    #[test]
357    #[serial_test::serial]
358    fn unknown_action_errors() {
359        // Issue #149: an unknown action is clear argument misuse →
360        // `docs/ERRORS.md` §4 exit 2. The descriptive line moved to
361        // stderr; the error carries `CliExit(2)`.
362        let _g = unset_all_doiget_config_env();
363        let err = run("bogus".into(), crate::commands::output::OutputMode::Human)
364            .expect_err("bogus action should error");
365        let cli_exit = err
366            .downcast_ref::<CliExit>()
367            .expect("unknown config action must carry a CliExit (issue #149)");
368        assert_eq!(
369            cli_exit.0, 2,
370            "unknown config action is misuse → exit 2, not the generic exit 1"
371        );
372    }
373}