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. --- CHANGELOG.md | 3 ++ Cargo.lock | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 ++ src/config.rs | 12 +++++- 4 files changed, 137 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 020bdc6..208ccc2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,13 @@ Unreleased ---------- - Added support for configuration files + - Added support for configuration files that can be used to set + default values for some arguments - Added `toml` dependency in version `0.5.6` - Added `serde` dependency in version `1.0.114` - Added `envy` dependency in version `0.4.1` - Added `merge` dependency in version `0.1.0` + - Added `directories` dependency in version `3.0.1` - Changed default OTP format from `hex` to `base32` - Improved error reporting format and fidelity - Added `anyhow` dependency in version `1.0.32` diff --git a/Cargo.lock b/Cargo.lock index a1256f8..bf55b89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -15,11 +15,23 @@ version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" +[[package]] +name = "arrayref" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" + +[[package]] +name = "arrayvec" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" + [[package]] name = "autocfg" -version = "1.0.1" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d" [[package]] name = "base32" @@ -27,12 +39,29 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23ce669cd6c8588f79e15cf450314f9638f967fc5770ff1c7c1deb0925ea7cfa" +[[package]] +name = "base64" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "blake2b_simd" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" +dependencies = [ + "arrayref", + "arrayvec", + "constant_time_eq", +] + [[package]] name = "cc" version = "1.0.50" @@ -56,6 +85,43 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "constant_time_eq" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "directories" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fed639d60b58d0f53498ab13d26f621fd77569cc6edb031f4cc36a2ad9da0f" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "envy" version = "0.4.1" @@ -131,6 +197,7 @@ version = "0.3.3" dependencies = [ "anyhow", "base32", + "directories", "envy", "libc", "merge", @@ -243,6 +310,23 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redox_syscall" +version = "0.1.57" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" + +[[package]] +name = "redox_users" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431" +dependencies = [ + "getrandom", + "redox_syscall", + "rust-argon2", +] + [[package]] name = "regex" version = "1.3.7" @@ -261,6 +345,18 @@ version = "0.6.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae" +[[package]] +name = "rust-argon2" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" +dependencies = [ + "base64", + "blake2b_simd", + "constant_time_eq", + "crossbeam-utils", +] + [[package]] name = "serde" version = "1.0.114" @@ -383,3 +479,25 @@ name = "wasi" version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 493e0c5..e9c91b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,6 +55,9 @@ version = "0.4.0" [dependencies.envy] version = "0.4.1" +[dependencies.directories] +version = "3" + [dependencies.libc] version = "0.2" 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.1