diff options
author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-25 14:15:50 +0100 |
---|---|---|
committer | Daniel Mueller <deso@posteo.net> | 2020-09-05 11:36:12 -0700 |
commit | aa28268d9087572dc7eb62df46fcdc9db12d6de5 (patch) | |
tree | a8f955ac15302799b48f94466fc7b8473f247366 /src | |
parent | b4b7605e1f947ef0c58d94354c10fd74fd010a53 (diff) | |
download | nitrocli-aa28268d9087572dc7eb62df46fcdc9db12d6de5.tar.gz nitrocli-aa28268d9087572dc7eb62df46fcdc9db12d6de5.tar.bz2 |
Use standard configuration file locations
This patch uses the directories crate to query the appropriate path for
the configuration files. For Linux, paths according to the XDG Base
Directory Specification are used.
Note that directories does not yet support the XDG_CONFIG_DIRS variable
for system-wide configuration files. Therefore we only use a user
configuration file.
Diffstat (limited to 'src')
-rw-r--r-- | src/config.rs | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/config.rs b/src/config.rs index 2e8f3ba..d146c14 100644 --- a/src/config.rs +++ b/src/config.rs @@ -24,6 +24,14 @@ use crate::args; use anyhow::Context as _; +/// The name of nitrocli's configuration file relative to the +/// application configuration directory. +/// +/// The application configuration directory is determined using the +/// `directories` crate. For Unix, it is `$XDG_CONFIG_HOME/nitrocli` +/// (defaults to `$HOME/.config/nitrocli`). +const CONFIG_FILE: &str = "config.toml"; + /// The configuration for nitrocli, usually read from configuration /// files and environment variables. #[derive(Clone, Copy, Debug, Default, PartialEq, merge::Merge, serde::Deserialize)] @@ -64,7 +72,9 @@ impl Config { } fn load_user_config() -> anyhow::Result<Option<Config>> { - let path = path::Path::new("config.toml"); + let project_dirs = directories::ProjectDirs::from("", "", "nitrocli") + .ok_or_else(|| anyhow::anyhow!("Could not determine the nitrocli application directory"))?; + let path = project_dirs.config_dir().join(CONFIG_FILE); if path.is_file() { read_config_file(&path).map(Some) } else { |