use jj_lib::config::ConfigLayer;
use tracing::instrument;
use super::ConfigLevelArgs;
use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::command_error::print_error_sources;
use crate::ui::Ui;
#[derive(clap::Args, Clone, Debug)]
pub struct ConfigEditArgs {
#[command(flatten)]
pub level: ConfigLevelArgs,
}
#[instrument(skip_all)]
pub async fn cmd_config_edit(
ui: &mut Ui,
command: &CommandHelper,
args: &ConfigEditArgs,
) -> Result<(), CommandError> {
let editor = command.text_editor()?;
let file = args.level.edit_config_file(ui, command)?;
if !file.path().exists() {
file.save()?;
}
writeln!(ui.status(), "Editing file: {}", file.path().display())?;
loop {
editor.edit_file(file.path())?;
if let Err(e) = ConfigLayer::load_from_file(file.layer().source, file.path().to_path_buf())
{
writeln!(
ui.warning_default(),
"An error has been found inside the config:"
)?;
print_error_sources(ui, Some(&e))?;
let continue_editing = ui.prompt_yes_no(
"Do you want to keep editing the file? If not, previous config will be restored.",
Some(true),
)?;
if !continue_editing {
file.save()?;
break;
}
} else {
break;
}
}
Ok(())
}