Skip to content

Commit

Permalink
cli: move get_new_config_file_path() to commands::config
Browse files Browse the repository at this point in the history
There aren't any other callers internally, and it is a thin wrapper around
ConfigEnv functions which can be easily implemented.
  • Loading branch information
yuja committed Nov 30, 2024
1 parent e1cd1a0 commit 36e83ed
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
23 changes: 0 additions & 23 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/config/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
}
27 changes: 23 additions & 4 deletions cli/src/commands/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mod path;
mod set;
mod unset;

use std::path::Path;

use jj_lib::config::ConfigSource;
use tracing::instrument;

Expand All @@ -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)]
Expand All @@ -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)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/config/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
"{}",
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/config/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)",
Expand Down
3 changes: 1 addition & 2 deletions cli/src/commands/config/unset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)",
Expand Down

0 comments on commit 36e83ed

Please sign in to comment.