Skip to content

Commit

Permalink
feat!: use XDG spec environment variables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LucasFA committed Mar 2, 2024
1 parent 1fba738 commit 7854c12
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,15 +381,15 @@ 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 <FOLDER_PATH>` or `--config-folder <FOLDER_PATH>` 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 <FOLDER_PATH>` or `--config-folder <FOLDER_PATH>` option.

If an application configuration file is not found, one will be created with default values.

Please refer to [the configuration documentation](docs/config.md) for more details on the configuration options.

## 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 <FOLDER_PATH>` or `--cache-folder <FOLDER_PATH>` 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 <FOLDER_PATH>` or `--cache-folder <FOLDER_PATH>` option.

### Logging

Expand Down
21 changes: 17 additions & 4 deletions spotify_player/src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -323,14 +324,26 @@ impl AppConfig {

/// gets the application's configuration folder path
pub fn get_config_folder_path() -> Result<PathBuf> {
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<PathBuf> {
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")),
Expand Down

0 comments on commit 7854c12

Please sign in to comment.