cellstate/entity/
config.rs1use anyhow::Result;
6use clap::{Arg, ArgMatches, Command};
7
8use cellstate_core::api_types::{ConfigResponse, UpdateConfigRequest, ValidateConfigRequest};
9
10use super::client::ApiClient;
11use super::output::OutputConfig;
12
13const BASE: &str = "/api/v1/config";
14
15pub fn build_command() -> Command {
16 Command::new("config")
17 .about("Manage configuration")
18 .subcommand_required(true)
19 .subcommand(Command::new("get").about("Get current configuration"))
20 .subcommand(
21 Command::new("update").about("Update configuration").arg(
22 Arg::new("config")
23 .required(true)
24 .help("Configuration as JSON string"),
25 ),
26 )
27 .subcommand(
28 Command::new("validate")
29 .about("Validate configuration without applying")
30 .arg(
31 Arg::new("config")
32 .required(true)
33 .help("Configuration as JSON string to validate"),
34 ),
35 )
36}
37
38pub async fn dispatch(
39 matches: &ArgMatches,
40 client: &ApiClient,
41 output: &OutputConfig,
42 _session: &crate::session::CliSession,
43) -> Result<()> {
44 match matches.subcommand() {
45 Some(("get", _sub)) => {
46 let resp: ConfigResponse = client.get(BASE).await?;
47 output.print(&resp);
48 }
49 Some(("update", sub)) => {
50 let config: serde_json::Value =
51 serde_json::from_str(sub.get_one::<String>("config").unwrap())?;
52 let req = UpdateConfigRequest { config };
53 let resp: ConfigResponse = client.patch(BASE, &req).await?;
54 output.print(&resp);
55 }
56 Some(("validate", sub)) => {
57 let config: serde_json::Value =
58 serde_json::from_str(sub.get_one::<String>("config").unwrap())?;
59 let req = ValidateConfigRequest { config };
60 let resp: serde_json::Value =
61 client.post_raw(&format!("{BASE}/validate"), &req).await?;
62 output.print_value(&resp);
63 }
64 _ => unreachable!("subcommand_required(true) prevents this"),
65 }
66 Ok(())
67}