Skip to content

Commit

Permalink
config: rename ConfigEnv methods to clarify the intent
Browse files Browse the repository at this point in the history
We'll add existing/new_repo_config_path() methods.
  • Loading branch information
yuja committed Nov 26, 2024
1 parent 39063f1 commit 3186d91
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2772,7 +2772,7 @@ pub fn get_new_config_file_path(
// TODO(#531): Special-case for editors that can't handle viewing directories?
ConfigSource::User => command
.config_env()
.new_config_path()?
.new_user_config_path()?
.ok_or_else(|| user_error("No repo config path found to edit"))?
.to_owned(),
ConfigSource::Repo => command.workspace_loader()?.repo_path().join("config.toml"),
Expand Down Expand Up @@ -3553,7 +3553,7 @@ impl CliRunner {
"Did you update to a commit where the directory doesn't exist?",
)
})?;
let config_env = ConfigEnv::new()?;
let config_env = ConfigEnv::from_environment()?;
// Use cwd-relative workspace configs to resolve default command and
// aliases. WorkspaceLoader::init() won't do any heavy lifting other
// than the path resolution.
Expand All @@ -3569,7 +3569,7 @@ impl CliRunner {
}
let config = layered_configs.merge();
ui.reset(&config).map_err(|e| {
let user_config_path = config_env.existing_config_path();
let user_config_path = config_env.existing_user_config_path();
let paths = [repo_config_path.as_deref(), user_config_path]
.into_iter()
.flatten()
Expand Down
2 changes: 1 addition & 1 deletion cli/src/complete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ fn get_jj_command() -> Result<(std::process::Command, Config), CommandError> {
let cwd = std::env::current_dir()
.and_then(|cwd| cwd.canonicalize())
.map_err(user_error)?;
let config_env = ConfigEnv::new()?;
let config_env = ConfigEnv::from_environment()?;
let maybe_cwd_workspace_loader = DefaultWorkspaceLoaderFactory.create(find_workspace_dir(&cwd));
let _ = layered_configs.read_user_config(&config_env);
if let Ok(loader) = &maybe_cwd_workspace_loader {
Expand Down
12 changes: 6 additions & 6 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl LayeredConfigs {
#[instrument]
pub fn read_user_config(&mut self, env: &ConfigEnv) -> Result<(), ConfigError> {
self.inner.remove_layers(ConfigSource::User);
if let Some(path) = env.existing_config_path() {
if let Some(path) = env.existing_user_config_path() {
if path.is_dir() {
self.inner.load_dir(ConfigSource::User, path)?;
} else {
Expand Down Expand Up @@ -318,7 +318,7 @@ pub struct ConfigEnv {

impl ConfigEnv {
/// Initializes configuration loader based on environment variables.
pub fn new() -> Result<Self, ConfigEnvError> {
pub fn from_environment() -> Result<Self, ConfigEnvError> {
let env = UnresolvedConfigEnv {
config_dir: dirs::config_dir(),
home_dir: dirs::home_dir(),
Expand All @@ -330,7 +330,7 @@ impl ConfigEnv {
}

/// Returns a path to the existing user-specific config file or directory.
pub fn existing_config_path(&self) -> Option<&Path> {
pub fn existing_user_config_path(&self) -> Option<&Path> {
match &self.user_config_path {
ConfigPath::Existing(path) => Some(path),
_ => None,
Expand All @@ -342,7 +342,7 @@ impl ConfigEnv {
/// If no config file is found, tries to guess a reasonable new location for
/// it. If a path to a new config file is returned, the parent directory may
/// be created as a result of this call.
pub fn new_config_path(&self) -> Result<Option<&Path>, ConfigEnvError> {
pub fn new_user_config_path(&self) -> Result<Option<&Path>, ConfigEnvError> {
match &self.user_config_path {
ConfigPath::Existing(path) => Ok(Some(path)),
ConfigPath::New(path) => {
Expand Down Expand Up @@ -1205,7 +1205,7 @@ mod tests {
let env = self
.resolve(tmp.path())
.map_err(|e| anyhow!("existing_config_path: {e}"))?;
let got = env.existing_config_path();
let got = env.existing_user_config_path();
if got != want.as_deref() {
return Err(anyhow!("existing_config_path: got {got:?}, want {want:?}"));
}
Expand All @@ -1223,7 +1223,7 @@ mod tests {
.resolve(tmp.path())
.map_err(|e| anyhow!("new_config_path: {e}"))?;
let got = env
.new_config_path()
.new_user_config_path()
.map_err(|e| anyhow!("new_config_path: {e}"))?;
if got != want.as_deref() {
return Err(anyhow!("new_config_path: got {got:?}, want {want:?}"));
Expand Down

0 comments on commit 3186d91

Please sign in to comment.