From d342513c652d208fd11c6ee60a5b0cec581e2318 Mon Sep 17 00:00:00 2001 From: LucasFA <23667494+LucasFA@users.noreply.github.com> Date: Mon, 5 Feb 2024 17:57:07 +0100 Subject: [PATCH] feat(config)!: use XDG spec environment variables Depending on the existence of a file in the original config location, uses that or the new location. Linux users will see no change if they haven't set any XDG_* environment variable. Windows and Mac OS users may see their cache folder move, adjusting it to the dirs_next advised location. Everyone will have the option to establish their settings in their $XDG_CONFIG_HOME directory and their cache location analogously. --- README.md | 4 ++-- spotify_player/src/config/mod.rs | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0777c4ce..2cf1556b 100644 --- a/README.md +++ b/README.md @@ -380,7 +380,7 @@ To move the focus from the search input to the other windows such as track resul ## Configurations -By default, `spotify_player` will look into `$HOME/.config/spotify-player` for application's configuration files. This can be changed by either specifying `-c ` or `--config-folder ` option. +By default, `spotify_player` will look into the `$XDG_CONFIG_HOME/spotify-player` directory for application's configuration files, falling back on `$HOME/.config/spotify-player`. This can be changed by either specifying `-c ` or `--config-folder ` option. If an application configuration file is not found, one will be created with default values. @@ -388,7 +388,7 @@ Please refer to [the configuration documentation](docs/config.md) for more detai ## Caches -By default, `spotify_player` will look into `$HOME/.cache/spotify-player` for application's cache files, which include log files, Spotify's authorization credentials, audio cache files, etc. This can be changed by either specifying `-C ` or `--cache-folder ` option. +By default, `spotify_player` will look into `$XDG_CACHE_HOME/spotify-player` for application's cache files, which include log files, Spotify's authorization credentials, audio cache files, etc, falling back on `$HOME/.cache/spotify-player`. This can be changed by either specifying `-C ` or `--cache-folder ` option. ### Logging diff --git a/spotify_player/src/config/mod.rs b/spotify_player/src/config/mod.rs index b63794b1..69fe2159 100644 --- a/spotify_player/src/config/mod.rs +++ b/spotify_player/src/config/mod.rs @@ -2,7 +2,6 @@ mod keymap; mod theme; const DEFAULT_CONFIG_FOLDER: &str = ".config/spotify-player"; -const DEFAULT_CACHE_FOLDER: &str = ".cache/spotify-player"; const APP_CONFIG_FILE: &str = "app.toml"; const THEME_CONFIG_FILE: &str = "theme.toml"; const KEYMAP_CONFIG_FILE: &str = "keymap.toml"; @@ -321,18 +320,27 @@ impl AppConfig { } } +const APP_NAME: &str = "spotify-player"; + /// gets the application's configuration folder path pub fn get_config_folder_path() -> Result { - match dirs_next::home_dir() { - Some(home) => Ok(home.join(DEFAULT_CONFIG_FOLDER)), - None => Err(anyhow!("cannot find the $HOME folder")), + if let Some(home) = dirs_next::home_dir() { + let old_path = home.join(DEFAULT_CONFIG_FOLDER); + if old_path.exists() { + return Ok(old_path); + } } + if let Some(config_home) = dirs_next::config_dir() { + return Ok(config_home.join(APP_NAME)); + } + + Err(anyhow!("cannot find the $HOME folder")) } /// gets the application's cache folder path pub fn get_cache_folder_path() -> Result { - match dirs_next::home_dir() { - Some(home) => Ok(home.join(DEFAULT_CACHE_FOLDER)), + match dirs_next::cache_dir() { + Some(cache_home) => Ok(cache_home.join(APP_NAME)), None => Err(anyhow!("cannot find the $HOME folder")), } }