From 28f75e9abe874b4469f761598681b8e0d92739af Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Sat, 25 Jan 2020 14:15:50 +0100 Subject: 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. --- src/config.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src') 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> { - 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 { -- cgit v1.2.3