Skip to main content

solana_tools_lite_cli/shell/
config.rs

1use crate::models::cmds::OutFmt;
2use std::env;
3
4/// Resolve global and command-specific configuration from environment variables.
5pub struct ConfigResolver;
6
7impl ConfigResolver {
8    /// Resolve the signer keypair path with fallback to environment.
9    pub fn resolve_keypair(explicit: Option<String>) -> Option<String> {
10        explicit.or_else(|| env::var("SOLANA_SIGNER_KEYPAIR").ok())
11    }
12
13    /// Resolve max fee with fallback to environment.
14    pub fn resolve_max_fee(explicit: Option<u64>) -> Option<u64> {
15        explicit.or_else(|| {
16            env::var("SOLANA_TOOLS_LITE_MAX_FEE")
17                .ok()
18                .and_then(|v| v.parse().ok())
19        })
20    }
21
22    /// Resolve output format with fallback to environment.
23    pub fn resolve_output_format(explicit: Option<OutFmt>) -> Option<OutFmt> {
24        explicit.or_else(|| {
25            env::var("SOLANA_TOOLS_LITE_OUTPUT_FORMAT")
26                .ok()
27                .and_then(|v| match v.to_lowercase().as_str() {
28                    "json" => Some(OutFmt::Json),
29                    "base64" => Some(OutFmt::Base64),
30                    "base58" => Some(OutFmt::Base58),
31                    _ => None,
32                })
33        })
34    }
35
36    /// Resolve global JSON flag.
37    pub fn resolve_json(explicit: bool) -> bool {
38        if explicit {
39            return true;
40        }
41        env::var("SOLANA_TOOLS_LITE_JSON")
42            .map(|v| v == "true" || v == "1")
43            .unwrap_or(false)
44    }
45
46    /// Resolve global Force flag.
47    pub fn resolve_force(explicit: bool) -> bool {
48        if explicit {
49            return true;
50        }
51        env::var("SOLANA_TOOLS_LITE_FORCE")
52            .map(|v| v == "true" || v == "1")
53            .unwrap_or(false)
54    }
55
56    /// Resolve global Yes flag.
57    pub fn resolve_yes(explicit: bool) -> bool {
58        if explicit {
59            return true;
60        }
61        env::var("SOLANA_TOOLS_LITE_YES")
62            .map(|v| v == "true" || v == "1")
63            .unwrap_or(false)
64    }
65}