Skip to main content

auths_cli/factories/
mod.rs

1pub mod network;
2pub mod storage;
3
4use std::io::IsTerminal;
5use std::sync::Arc;
6use std::time::Duration;
7
8use anyhow::Result;
9
10use auths_core::config::EnvironmentConfig;
11use auths_core::signing::{CachedPassphraseProvider, PassphraseProvider};
12use auths_sdk::ports::agent::AgentSigningPort;
13
14use crate::cli::AuthsCli;
15use crate::config::{CliConfig, OutputFormat};
16use crate::core::provider::{CliPassphraseProvider, PrefilledPassphraseProvider};
17
18/// Builds the full CLI configuration from parsed arguments.
19///
20/// Constructs the passphrase provider, HTTP client, and output settings.
21/// This is the composition root — the only place where concrete adapter
22/// types are instantiated.
23///
24/// Args:
25/// * `cli`: The parsed CLI arguments.
26///
27/// Usage:
28/// ```ignore
29/// use auths_cli::factories::build_config;
30///
31/// let cli = AuthsCli::parse();
32/// let config = build_config(&cli)?;
33/// ```
34pub fn build_config(cli: &AuthsCli) -> Result<CliConfig> {
35    let is_json = cli.json || matches!(cli.output, OutputFormat::Json);
36    let output_format = if is_json {
37        OutputFormat::Json
38    } else {
39        cli.output
40    };
41
42    let env_config = EnvironmentConfig::from_env();
43
44    let passphrase_provider: Arc<dyn PassphraseProvider + Send + Sync> =
45        if let Some(passphrase) = env_config.keychain.passphrase.clone() {
46            Arc::new(PrefilledPassphraseProvider::new(zeroize::Zeroizing::new(
47                passphrase,
48            )))
49        } else {
50            let inner = Arc::new(CliPassphraseProvider::new());
51            Arc::new(CachedPassphraseProvider::new(
52                inner,
53                Duration::from_secs(3600),
54            ))
55        };
56
57    let is_interactive = std::io::stdout().is_terminal();
58    let http_client = network::build_http_client()?;
59
60    Ok(CliConfig {
61        repo_path: cli.repo.clone(),
62        output_format,
63        is_interactive,
64        passphrase_provider,
65        http_client,
66        env_config,
67    })
68}
69
70/// Build the platform-appropriate agent signing provider.
71///
72/// Returns `CliAgentAdapter` on Unix, `NoopAgentProvider` elsewhere.
73///
74/// Usage:
75/// ```ignore
76/// let agent = build_agent_provider();
77/// let ctx = CommitSigningContext { agent_signing: agent, .. };
78/// ```
79pub fn build_agent_provider() -> Arc<dyn AgentSigningPort + Send + Sync> {
80    #[cfg(unix)]
81    {
82        Arc::new(crate::adapters::agent::CliAgentAdapter)
83    }
84    #[cfg(not(unix))]
85    {
86        Arc::new(auths_sdk::ports::agent::NoopAgentProvider)
87    }
88}