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                    "worker_url" => config.worker_url = Some(value.clone()),
46                    "api_domain" => config.api_domain = value.clone(),
47                    "worker_domain" => config.worker_domain = value.clone(),
48                    "default_confirmations" => {
49                        config.default_confirmations = value.parse().ok();
50                    }
51                    "default_rebroadcast_max" => {
52                        config.default_rebroadcast_max = value.parse().ok();
53                    }
54                    "default_valid_for" => {
55                        config.default_valid_for = value.parse().ok();
56                    }
57                    "key_file" => config.key_file = Some(value.clone()),
58                    _ => {
59                        return Err(CairnError::InvalidInput(format!(
60                            "Unknown config key: '{}'. Valid keys: chain, network, tier, output, api_url, worker_url, \
61                             api_domain, worker_domain, default_confirmations, default_rebroadcast_max, default_valid_for, key_file",
62                            key
63                        )));
64                    }
65                }
66
67                config.save()?;
68
69                let result = serde_json::json!({
70                    "key": key,
71                    "value": value,
72                    "saved": true,
73                });
74                output_json(&result, &cli.output);
75                Ok(())
76            }
77
78            ConfigCommands::Get { key } => {
79                let config = CairnConfig::load();
80
81                if let Some(k) = key {
82                    let value = match k.as_str() {
83                        "chain" => serde_json::json!(config.chain),
84                        "network" => serde_json::json!(config.network),
85                        "tier" => serde_json::json!(config.tier),
86                        "output" => serde_json::json!(config.output),
87                        "api_url" => serde_json::json!(config.api_url),
88                        "worker_url" => serde_json::json!(config.worker_url),
89                        "api_domain" => serde_json::json!(config.api_domain),
90                        "worker_domain" => serde_json::json!(config.worker_domain),
91                        "default_confirmations" => serde_json::json!(config.default_confirmations),
92                        "default_rebroadcast_max" => serde_json::json!(config.default_rebroadcast_max),
93                        "default_valid_for" => serde_json::json!(config.default_valid_for),
94                        "key_file" => serde_json::json!(config.key_file),
95                        _ => {
96                            return Err(CairnError::InvalidInput(format!(
97                                "Unknown config key: '{}'",
98                                k
99                            )));
100                        }
101                    };
102
103                    let result = serde_json::json!({ k.as_str(): value });
104                    output_json(&result, &cli.output);
105                } else {
106                    let result = serde_json::to_value(&config)
107                        .map_err(|e| CairnError::General(e.to_string()))?;
108                    output_json(&result, &cli.output);
109                }
110
111                Ok(())
112            }
113        }
114    }
115}