use clap::{ArgAction, Subcommand};
use std::process::Command;
use crate::config::Config;
use crate::errors::GeneralError;
#[derive(Subcommand, Debug, Clone)]
pub enum ConfigSubcommand {
Open {
#[arg(short = 'p', long = "path", action = ArgAction::SetTrue)]
show_path_only: bool,
},
}
impl ConfigSubcommand {
pub(crate) fn invoke(&self, config: &mut Config) -> Result<(), GeneralError> {
match self {
ConfigSubcommand::Open { show_path_only } => {
ConfigSubcommand::open(config, *show_path_only)
}
}
}
fn open(config: &mut Config, print_path: bool) -> Result<(), GeneralError> {
let config_path = &config.config_path;
if print_path {
println!("{}", config_path.display());
return Ok(());
}
println!("Opening config {}", config_path.display());
let editor = std::env::var("EDITOR").unwrap_or("vi".to_string());
Command::new(editor).arg(config_path).spawn()?.wait()?;
Ok(())
}
}