From 5165c335c19cb624e42e79beb1deb91f5935c8dc 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!: use XDG spec environment variables Use original config location if it exists, falls back on XDG. Users will see no change if they haven't set any XDG_* environment variable. Everyone will have the option to establish their settings in their $XDG_CONFIG_HOME directory and their cache location analogously. No platform specific locations are used for simplicity. --- README.md | 4 ++-- spotify_player/src/config/mod.rs | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c975d2e4..daee0811 100644 --- a/README.md +++ b/README.md @@ -381,7 +381,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 `$HOME/.config/spotify-player` and the `$XDG_CONFIG_HOME/spotify-player` directories for application's configuration files, with that preference. 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. @@ -389,7 +389,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..aa007fe2 100644 --- a/spotify_player/src/config/mod.rs +++ b/spotify_player/src/config/mod.rs @@ -1,6 +1,7 @@ mod keymap; mod theme; +const APP_NAME: &str = "spotify-player"; const DEFAULT_CONFIG_FOLDER: &str = ".config/spotify-player"; const DEFAULT_CACHE_FOLDER: &str = ".cache/spotify-player"; const APP_CONFIG_FILE: &str = "app.toml"; @@ -323,14 +324,26 @@ impl AppConfig { /// 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 default_config_path = home.join(DEFAULT_CONFIG_FOLDER); + if default_config_path.exists() { + return Ok(default_config_path); + } + + return match std::env::var("XDG_CONFIG_HOME") { + Ok(config_home) => Ok(PathBuf::from(config_home).join(APP_NAME)), + Err(_) => Ok(default_config_path), + }; + }; + + Err(anyhow!("cannot find the $HOME folder")) } /// gets the application's cache folder path pub fn get_cache_folder_path() -> Result { + if let Ok(cache_home) = std::env::var("XDG_CACHE_HOME") { + return Ok(PathBuf::from(cache_home).join(APP_NAME)); + } match dirs_next::home_dir() { Some(home) => Ok(home.join(DEFAULT_CACHE_FOLDER)), None => Err(anyhow!("cannot find the $HOME folder")),