radicle_cli/commands/
config.rs1mod args;
2
3pub use args::Args;
4use args::Command;
5
6use std::path::Path;
7
8use radicle::profile::{Config, config};
9
10#[allow(deprecated)]
11use radicle::profile::config::{ConfigPath, RawConfig};
12
13use crate::terminal::Element as _;
14use crate::{terminal as term, warning};
15
16pub fn run(args: Args, ctx: impl term::Context) -> anyhow::Result<()> {
17 let home = ctx.home()?;
18 let path = home.config();
19 let command = args.command.unwrap_or(Command::Show);
20
21 match command {
22 Command::Show => {
23 let profile = ctx.profile()?;
24 term::json::to_pretty(&profile.config, path.as_path())?.print();
25 }
26 Command::Schema => {
27 term::json::to_pretty(&schemars::schema_for!(Config), path.as_path())?.print()
28 }
29 #[allow(deprecated)]
30 Command::Get { key } => {
31 let mut temp_config = RawConfig::from_file(&path)?;
32 let key: ConfigPath = key.into();
33 let value = temp_config.get_mut(&key).ok_or_else(|| {
34 anyhow::anyhow!("{key} does not exist in configuration found at {path:?}")
35 })?;
36 print_value(value)?;
37 }
38 #[allow(deprecated)]
39 Command::Set { key, value } => {
40 warning::obsolete("rad config set");
41 let value = modify(path, |tmp| tmp.set(&key.into(), value.into()))?;
42 print_value(&value)?;
43 }
44 #[allow(deprecated)]
45 Command::Push { key, value } => {
46 warning::obsolete("rad config push");
47 let value = modify(path, |tmp| tmp.push(&key.into(), value.into()))?;
48 print_value(&value)?;
49 }
50 #[allow(deprecated)]
51 Command::Remove { key, value } => {
52 warning::obsolete("rad config remove");
53 let value = modify(path, |tmp| tmp.remove(&key.into(), value.into()))?;
54 print_value(&value)?;
55 }
56 #[allow(deprecated)]
57 Command::Unset { key } => {
58 warning::obsolete("rad config unset");
59 let value = modify(path, |tmp| tmp.unset(&key.into()))?;
60 print_value(&value)?;
61 }
62 Command::Init { alias } => {
63 if path.try_exists()? {
64 anyhow::bail!("configuration file already exists at `{}`", path.display());
65 }
66 Config::init(alias, &path)?;
67 term::success!(
68 "Initialized new Radicle configuration at {}",
69 path.display()
70 );
71 }
72 Command::Edit => match term::editor::Editor::new(&path)?.extension("json").edit()? {
73 Some(_) => {
74 term::success!("Successfully made changes to the configuration at {path:?}")
75 }
76 None => term::info!("No changes were made to the configuration at {path:?}"),
77 },
78 }
79
80 Ok(())
81}
82
83#[deprecated]
84#[allow(deprecated)]
85fn modify<P, M>(path: P, modification: M) -> anyhow::Result<serde_json::Value>
86where
87 P: AsRef<Path>,
88 M: FnOnce(&mut RawConfig) -> Result<serde_json::Value, config::ModifyError>,
89{
90 let path = path.as_ref();
91 let mut temp_config = RawConfig::from_file(path)?;
92 let value = modification(&mut temp_config).map_err(|err| {
93 anyhow::anyhow!("failed to modify configuration found at {path:?} due to {err}")
94 })?;
95 temp_config.write(path)?;
96 Ok(value)
97}
98
99#[deprecated]
101#[allow(deprecated)]
102fn print_value(value: &serde_json::Value) -> anyhow::Result<()> {
103 match value {
104 serde_json::Value::Null => {}
105 serde_json::Value::Bool(b) => term::println(b),
106 serde_json::Value::Array(a) => a.iter().try_for_each(print_value)?,
107 serde_json::Value::Number(n) => term::println(n),
108 serde_json::Value::String(s) => term::println(s),
109 serde_json::Value::Object(o) => {
110 term::json::to_pretty(&o, Path::new("config.json"))?.print()
111 }
112 }
113 Ok(())
114}