Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementTsang committed Aug 22, 2024
1 parent 843d979 commit 1755168
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,54 +60,60 @@ macro_rules! is_flag_enabled {
};
}

cfg_if::cfg_if! {
if #[cfg(test)] {
const DEFAULT_CONFIG_FILE_PATH: &str = "bottom_this_is_a_test/bottom.toml";
} else {
const DEFAULT_CONFIG_FILE_PATH: &str = "bottom/bottom.toml";
}
}
/// The default config file sub-path.
const DEFAULT_CONFIG_FILE_LOCATION: &str = "bottom/bottom.toml";

/// Returns the config path to use. If `override_config_path` is specified, then
/// we will use that. If not, then return the "default" config path, which is:
///
/// - If a path already exists at `<HOME>/bottom/bottom.toml`, then use that for
/// legacy reasons.
/// - Otherwise, use `<SYSTEM_CONFIG_FOLDER>/bottom/bottom.toml`.
///
/// For more details on this, see [dirs](https://docs.rs/dirs/latest/dirs/fn.config_dir.html)'
/// documentation.
///
/// XXX: For macOS, we additionally will manually check `$XDG_CONFIG_HOME` as well first
/// before falling back to `dirs`.
fn get_config_path(override_config_path: Option<&Path>) -> Option<PathBuf> {
if let Some(conf_loc) = override_config_path {
return Some(conf_loc.to_path_buf());
} else if let Some(home_path) = dirs::home_dir() {
let mut old_home_path = home_path;
old_home_path.push(".config/");
old_home_path.push(DEFAULT_CONFIG_FILE_PATH);
if old_home_path.exists() {
// We used to create it at `<HOME>/DEFAULT_CONFIG_FILE_PATH`, but changed it
// to be more correct later. However, for legacy reasons, if it already exists,
// use the old one.
return Some(old_home_path);
old_home_path.push(DEFAULT_CONFIG_FILE_LOCATION);
if let Ok(res) = old_home_path.try_exists() {
if res {
// We used to create it at `<HOME>/DEFAULT_CONFIG_FILE_PATH`, but changed it
// to be more correct later. However, for legacy reasons, if it already exists,
// use the old one.
return Some(old_home_path);
}
}
}

let config_path = dirs::config_dir().map(|mut path| {
path.push(DEFAULT_CONFIG_FILE_PATH);
path.push(DEFAULT_CONFIG_FILE_LOCATION);
path
});

if cfg!(target_os = "macos") {
if let Some(old_macos_path) = &config_path {
if old_macos_path.exists() {
return config_path;
}
}

if let Ok(xdg_config_path) = std::env::var("XDG_CONFIG_HOME") {
if !xdg_config_path.is_empty() {
// If XDG_CONFIG_HOME exists and is non-empty, _but_ we previously used the Library-based path
// for a config and it exists, then use that instead for backwards-compatibility.
if let Some(old_macos_path) = &config_path {
if let Ok(res) = old_macos_path.try_exists() {
if res {
return config_path;
}
}
}

// Otherwise, try and use the XDG_CONFIG_HOME-based path.
let mut cfg_path = PathBuf::new();
cfg_path.push(xdg_config_path);
cfg_path.push(DEFAULT_CONFIG_FILE_PATH);
cfg_path.push(DEFAULT_CONFIG_FILE_LOCATION);

return Some(cfg_path);
}
Expand Down Expand Up @@ -1215,7 +1221,7 @@ mod test {
#[test]
fn test_get_config_path_macos() {
use super::get_config_path;
use super::DEFAULT_CONFIG_FILE_PATH;
use super::DEFAULT_CONFIG_FILE_LOCATION;
use std::path::PathBuf;

// Case three: no previous config, no XDG var.
Expand All @@ -1231,15 +1237,21 @@ mod test {
})
.unwrap();

assert_eq!(get_config_path(None), Some(case_1));
// Skip this test if the file already exists.
if !case_1.exists() {
assert_eq!(get_config_path(None), Some(case_1));
}

// Case two: no previous config, XDG var exists.
std::env::set_var("XDG_CONFIG_HOME", "/tmp");
let mut case_2 = PathBuf::new();
case_2.push("/tmp");
case_2.push(DEFAULT_CONFIG_FILE_PATH);

assert_eq!(get_config_path(None), Some(case_2));
// Skip this test if the file already exists.
if !case_2.exists() {
assert_eq!(get_config_path(None), Some(case_2));
}

// Case one: old non-XDG exists already, XDG var exists.
// let case_3 = case_1;
Expand Down

0 comments on commit 1755168

Please sign in to comment.