Skip to main content

cairn_cli/cli/
config_cmd.rs

1use clap::{Args, Subcommand};
2
3use crate::config::CairnConfig;
4use crate::errors::CairnError;
5
6use super::{output_json, Cli};
7
8#[derive(Args, Debug)]
9pub struct ConfigArgs {
10    #[command(subcommand)]
11    pub command: ConfigCommands,
12}
13
14#[derive(Subcommand, Debug)]
15pub enum ConfigCommands {
16    /// Set a configuration value.
17    Set {
18        /// Configuration key (chain, network, tier, output, api_url, etc.)
19        key: String,
20
21        /// Value to set
22        value: String,
23    },
24
25    /// Get a configuration value or dump the full config.
26    Get {
27        /// Configuration key (omit to dump all)
28        key: Option<String>,
29    },
30}
31
32impl ConfigArgs {
33    pub async fn execute(&self, cli: &Cli) -> Result<(), CairnError> {
34        match &self.command {
35            ConfigCommands::Set { key, value } => {
36                let mut config = CairnConfig::load();
37
38                match key.as_str() {
39                    "chain" => config.chain = value.clone(),
40                    "network" => config.network = value.clone(),
41                    "tier" => config.tier = value.clone(),
42                    "output" => config.output = value.clone(),
43                    "api_url" => config.api_url = Some(value.clone()),
44                    "default_confirmations" => {
45                        config.default_confirmations = value.parse().ok();
46                    }
47                    "default_rebroadcast_max" => {
48                        config.default_rebroadcast_max = value.parse().ok();
49                    }
50                    "default_valid_for" => {
51                        config.default_valid_for = value.parse().ok();
52                    }
53                    "key_file" => config.key_file = Some(value.clone()),
54                    _ => {
55                        return Err(CairnError::InvalidInput(format!(
56                            "Unknown config key: '{}'. Valid keys: chain, network, tier, output, api_url, \
57                             default_confirmations, default_rebroadcast_max, default_valid_for, key_file",
58                            key
59                        )));
60                    }
61                }
62
63                config.save()?;
64
65                let result = serde_json::json!({
66                    "key": key,
67                    "value": value,
68                    "saved": true,
69                });
70                output_json(&result, &cli.output);
71                Ok(())
72            }
73
74            ConfigCommands::Get { key } => {
75                let config = CairnConfig::load();
76
77                if let Some(k) = key {
78                    let value = match k.as_str() {
79                        "chain" => serde_json::json!(config.chain),
80                        "network" => serde_json::json!(config.network),
81                        "tier" => serde_json::json!(config.tier),
82                        "output" => serde_json::json!(config.output),
83                        "api_url" => serde_json::json!(config.api_url),
84                        "default_confirmations" => serde_json::json!(config.default_confirmations),
85                        "default_rebroadcast_max" => serde_json::json!(config.default_rebroadcast_max),
86                        "default_valid_for" => serde_json::json!(config.default_valid_for),
87                        "key_file" => serde_json::json!(config.key_file),
88                        _ => {
89                            return Err(CairnError::InvalidInput(format!(
90                                "Unknown config key: '{}'",
91                                k
92                            )));
93                        }
94                    };
95
96                    let result = serde_json::json!({ k.as_str(): value });
97                    output_json(&result, &cli.output);
98                } else {
99                    let result = serde_json::to_value(&config)
100                        .map_err(|e| CairnError::General(e.to_string()))?;
101                    output_json(&result, &cli.output);
102                }
103
104                Ok(())
105            }
106        }
107    }
108}