harmont_cli/context.rs
1use std::io::IsTerminal;
2
3use anyhow::Result;
4
5use crate::cli::Cli;
6use crate::config::Config;
7use crate::output::OutputMode;
8
9/// Runtime context that bundles resolved config and output preferences.
10///
11/// After the plan-4 cloud-plugin cutover this is intentionally thin:
12/// API client, credential store, and active-org resolution moved into
13/// `hm-plugin-cloud`. The host context retains the config file (for
14/// future use) and the output mode.
15#[derive(Debug)]
16pub struct RunContext {
17 pub config: Config,
18 /// Output mode for the residual built-in verbs (the legacy global
19 /// `--format` flag was retired in plan 3; per-subcommand `--format`
20 /// is the only currently-wired source, so this defaults to human).
21 pub output: OutputMode,
22}
23
24impl RunContext {
25 /// Build a [`RunContext`] from parsed CLI args.
26 ///
27 /// # Errors
28 ///
29 /// Returns an error if the config file is unreadable or malformed.
30 pub fn from_cli(cli: &Cli) -> Result<Self> {
31 let config = Config::load()?;
32
33 let color =
34 !cli.no_color && std::env::var("NO_COLOR").is_err() && std::io::stderr().is_terminal();
35
36 let output = OutputMode::Human {
37 color,
38 interactive: std::io::stdout().is_terminal(),
39 };
40
41 Ok(Self { config, output })
42 }
43}