Skip to main content

pebble_cms/cli/
config.rs

1use crate::global::{GlobalConfig, PebbleHome};
2use anyhow::Result;
3
4pub async fn run(command: super::ConfigCommand) -> Result<()> {
5    let home = PebbleHome::init()?;
6    let mut config = GlobalConfig::load(&home.config_path)?;
7
8    match command {
9        super::ConfigCommand::Get { key } => match config.get(&key) {
10            Some(value) => println!("{}", value),
11            None => {
12                eprintln!("Unknown config key: {}", key);
13                std::process::exit(1);
14            }
15        },
16        super::ConfigCommand::Set { key, value } => {
17            config.set(&key, &value)?;
18            config.save(&home.config_path)?;
19            println!("Set {} = {}", key, value);
20        }
21        super::ConfigCommand::List => {
22            let items = config.list();
23            let max_key_len = items.iter().map(|(k, _)| k.len()).max().unwrap_or(0);
24            for (key, value) in items {
25                println!("{:width$}  {}", key, value, width = max_key_len);
26            }
27        }
28        super::ConfigCommand::Remove { key } => {
29            if config.remove(&key)? {
30                config.save(&home.config_path)?;
31                println!("Removed {}", key);
32            } else {
33                println!("Key not found: {}", key);
34            }
35        }
36        super::ConfigCommand::Path => {
37            println!("{}", home.config_path.display());
38        }
39    }
40
41    Ok(())
42}