use anyhow::{Context, Result};
use clap::Subcommand;
use worktree_io::{config::Config, opener};
#[derive(Subcommand)]
pub enum ConfigAction {
Show,
Path,
Init,
Set { key: String, value: String },
Get { key: String },
Edit,
}
pub fn cmd_config(action: ConfigAction) -> Result<()> {
match action {
ConfigAction::Show => {
let config = Config::load()?;
let pretty = toml::to_string_pretty(&config)?;
print!("{pretty}");
}
ConfigAction::Path => {
let path = Config::path()?;
println!("{}", path.display());
}
ConfigAction::Init => {
let config = Config::default();
config.save()?;
println!("Wrote default config to {}", Config::path()?.display());
}
ConfigAction::Set { key, value } => {
let mut config = Config::load()?;
config.set_value(&key, &value)?;
config.save()?;
println!("Set {key} = {value}");
}
ConfigAction::Get { key } => {
let config = Config::load()?;
println!("{}", config.get_value(&key)?);
}
ConfigAction::Edit => {
let path = Config::path()?;
if !path.exists() {
Config::default().save()?;
}
let command = Config::load()?
.editor
.command
.or_else(|| std::env::var("EDITOR").ok())
.context("No editor configured. Run: worktree config set editor.command \"code .\", or set the $EDITOR environment variable")?;
opener::open_in_editor(&path, &command, false)?;
}
}
Ok(())
}