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    #[command(alias = "show", alias = "list")]
27    Get {
28        /// Configuration key (omit to dump all)
29        key: Option<String>,
30    },
31}
32
33impl ConfigArgs {
34    pub async fn execute(&self, cli: &Cli) -> Result<(), CairnError> {
35        match &self.command {
36            ConfigCommands::Set { key, value } => {
37                let mut config = CairnConfig::load();
38
39                match key.as_str() {
40                    "chain" => config.chain = value.clone(),
41                    "network" => config.network = value.clone(),
42                    "tier" => config.tier = value.clone(),
43                    "output" => config.output = value.clone(),
44                    "api_url" => config.api_url = Some(value.clone()),
45                    "default_confirmations" => {
46                        config.default_confirmations = value.parse().ok();
47                    }
48                    "default_rebroadcast_max" => {
49                        config.default_rebroadcast_max = value.parse().ok();
50                    }
51                    "default_valid_for" => {
52                        config.default_valid_for = value.parse().ok();
53                    }
54                    "key_file" => config.key_file = Some(value.clone()),
55                    _ => {
56                        return Err(CairnError::InvalidInput(format!(
57                            "Unknown config key: '{}'. Valid keys: chain, network, tier, output, api_url, \
58                             default_confirmations, default_rebroadcast_max, default_valid_for, key_file",
59                            key
60                        )));
61                    }
62                }
63
64                config.save()?;
65
66                let result = serde_json::json!({
67                    "key": key,
68                    "value": value,
69                    "saved": true,
70                });
71                output_json(&result, &cli.output);
72                Ok(())
73            }
74
75            ConfigCommands::Get { key } => {
76                let config = CairnConfig::load();
77
78                if let Some(k) = key {
79                    let value = match k.as_str() {
80                        "chain" => serde_json::json!(config.chain),
81                        "network" => serde_json::json!(config.network),
82                        "tier" => serde_json::json!(config.tier),
83                        "output" => serde_json::json!(config.output),
84                        "api_url" => serde_json::json!(config.api_url),
85                        "default_confirmations" => serde_json::json!(config.default_confirmations),
86                        "default_rebroadcast_max" => serde_json::json!(config.default_rebroadcast_max),
87                        "default_valid_for" => serde_json::json!(config.default_valid_for),
88                        "key_file" => serde_json::json!(config.key_file),
89                        _ => {
90                            return Err(CairnError::InvalidInput(format!(
91                                "Unknown config key: '{}'",
92                                k
93                            )));
94                        }
95                    };
96
97                    let result = serde_json::json!({ k.as_str(): value });
98                    output_json(&result, &cli.output);
99                } else {
100                    let result = serde_json::to_value(&config)
101                        .map_err(|e| CairnError::General(e.to_string()))?;
102                    output_json(&result, &cli.output);
103                }
104
105                Ok(())
106            }
107        }
108    }
109}