cli: move get_new_config_file_path() to commands::config

There aren't any other callers internally, and it is a thin wrapper around
ConfigEnv functions which can be easily implemented.
This commit is contained in:
Yuya Nishihara 2024-11-28 23:12:32 +09:00
parent e1cd1a00e6
commit 36e83ed260
6 changed files with 27 additions and 35 deletions

View file

@ -58,7 +58,6 @@ use jj_lib::backend::MergedTreeId;
use jj_lib::backend::TreeValue;
use jj_lib::commit::Commit;
use jj_lib::config::ConfigError;
use jj_lib::config::ConfigSource;
use jj_lib::config::StackedConfig;
use jj_lib::conflicts::ConflictMarkerStyle;
use jj_lib::file_util;
@ -2760,28 +2759,6 @@ impl LogContentFormat {
}
}
pub fn get_new_config_file_path(
config_source: ConfigSource,
command: &CommandHelper,
) -> Result<&Path, CommandError> {
let config_env = command.config_env();
let edit_path = match config_source {
// TODO(#531): Special-case for editors that can't handle viewing directories?
ConfigSource::User => config_env
.new_user_config_path()?
.ok_or_else(|| user_error("No user config path found to edit"))?,
ConfigSource::Repo => config_env
.new_repo_config_path()
.ok_or_else(|| user_error("No repo config path found to edit"))?,
_ => {
return Err(user_error(format!(
"Can't get path for config source {config_source:?}"
)));
}
};
Ok(edit_path)
}
pub fn run_ui_editor(settings: &UserSettings, edit_path: &Path) -> Result<(), CommandError> {
// Work around UNC paths not being well supported on Windows (no-op for
// non-Windows): https://github.com/martinvonz/jj/issues/3986

View file

@ -15,7 +15,6 @@
use tracing::instrument;
use super::ConfigLevelArgs;
use crate::cli_util::get_new_config_file_path;
use crate::cli_util::run_ui_editor;
use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
@ -37,6 +36,6 @@ pub fn cmd_config_edit(
command: &CommandHelper,
args: &ConfigEditArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
let config_path = args.level.new_config_file_path(command.config_env())?;
run_ui_editor(command.settings(), config_path)
}

View file

@ -19,6 +19,8 @@ mod path;
mod set;
mod unset;
use std::path::Path;
use jj_lib::config::ConfigSource;
use tracing::instrument;
@ -35,7 +37,9 @@ use self::set::ConfigSetArgs;
use self::unset::cmd_config_unset;
use self::unset::ConfigUnsetArgs;
use crate::cli_util::CommandHelper;
use crate::command_error::user_error;
use crate::command_error::CommandError;
use crate::config::ConfigEnv;
use crate::ui::Ui;
#[derive(clap::Args, Clone, Debug)]
@ -51,10 +55,6 @@ pub(crate) struct ConfigLevelArgs {
}
impl ConfigLevelArgs {
fn expect_source_kind(&self) -> ConfigSource {
self.get_source_kind().expect("No config_level provided")
}
fn get_source_kind(&self) -> Option<ConfigSource> {
if self.user {
Some(ConfigSource::User)
@ -64,6 +64,25 @@ impl ConfigLevelArgs {
None
}
}
fn new_config_file_path<'a>(
&self,
config_env: &'a ConfigEnv,
) -> Result<&'a Path, CommandError> {
if self.user {
// TODO(#531): Special-case for editors that can't handle viewing
// directories?
config_env
.new_user_config_path()?
.ok_or_else(|| user_error("No user config path found to edit"))
} else if self.repo {
config_env
.new_repo_config_path()
.ok_or_else(|| user_error("No repo config path found to edit"))
} else {
panic!("No config_level provided")
}
}
}
/// Manage config options

View file

@ -17,7 +17,6 @@ use std::io::Write as _;
use tracing::instrument;
use super::ConfigLevelArgs;
use crate::cli_util::get_new_config_file_path;
use crate::cli_util::CommandHelper;
use crate::command_error::user_error;
use crate::command_error::CommandError;
@ -40,7 +39,7 @@ pub fn cmd_config_path(
command: &CommandHelper,
args: &ConfigPathArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
let config_path = args.level.new_config_file_path(command.config_env())?;
writeln!(
ui.stdout(),
"{}",

View file

@ -21,7 +21,6 @@ use jj_lib::repo::Repo;
use tracing::instrument;
use super::ConfigLevelArgs;
use crate::cli_util::get_new_config_file_path;
use crate::cli_util::CommandHelper;
use crate::cli_util::WorkspaceCommandHelper;
use crate::command_error::user_error;
@ -54,7 +53,7 @@ pub fn cmd_config_set(
command: &CommandHelper,
args: &ConfigSetArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
let config_path = args.level.new_config_file_path(command.config_env())?;
if config_path.is_dir() {
return Err(user_error(format!(
"Can't set config in path {path} (dirs not supported)",

View file

@ -17,7 +17,6 @@ use jj_lib::config::ConfigNamePathBuf;
use tracing::instrument;
use super::ConfigLevelArgs;
use crate::cli_util::get_new_config_file_path;
use crate::cli_util::CommandHelper;
use crate::command_error::user_error;
use crate::command_error::CommandError;
@ -40,7 +39,7 @@ pub fn cmd_config_unset(
command: &CommandHelper,
args: &ConfigUnsetArgs,
) -> Result<(), CommandError> {
let config_path = get_new_config_file_path(args.level.expect_source_kind(), command)?;
let config_path = args.level.new_config_file_path(command.config_env())?;
if config_path.is_dir() {
return Err(user_error(format!(
"Can't set config in path {path} (dirs not supported)",