Skip to content

Commit

Permalink
feat(config)!: use XDG spec environment variables
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LucasFA committed Feb 5, 2024
1 parent dbc645c commit d342513
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,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
20 changes: 14 additions & 6 deletions spotify_player/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<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 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<PathBuf> {
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")),
}
}

0 comments on commit d342513

Please sign in to comment.