Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use XDG specification environment variables #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 `$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 <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
Loading