From 685a1670a937eb3a2aeb1ffce0e1cc0637307258 Mon Sep 17 00:00:00 2001 From: Eduardo Trujillo <ed@chromabits.com> Date: Wed, 11 Jan 2023 18:28:30 -0800 Subject: [PATCH] fix(config): Use XDG config path --- Cargo.lock | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 1 + src/main.rs | 17 ++++++++++---- 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eeab304..60a9127 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -152,6 +152,26 @@ dependencies = [ "tokio", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "env_logger" version = "0.7.1" @@ -270,6 +290,17 @@ dependencies = [ "slab", ] +[[package]] +name = "getrandom" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "hashbrown" version = "0.11.2" @@ -330,9 +361,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.119" +version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1bf2e165bb3457c8e098ea76f3e3bc9db55f87aa90d52d0e6be741470916aaa4" +checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" [[package]] name = "libdbus-sys" @@ -389,7 +420,7 @@ dependencies = [ [[package]] name = "nm-reactor" -version = "0.1.0" +version = "0.1.2" dependencies = [ "anyhow", "async-recursion", @@ -409,6 +440,7 @@ dependencies = [ "serde_derive", "tokio", "tokio-stream", + "xdg", ] [[package]] @@ -584,6 +616,26 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redox_syscall" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb5a58c1855b4b6819d59012155603f0b22ad30cad752600aadfcb695265519a" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "regex" version = "1.5.5" @@ -832,6 +884,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "xdg" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4583db5cbd4c4c0303df2d15af80f0539db703fa1c68802d4cbbd2dd0f88f6" +dependencies = [ + "dirs", +] + [[package]] name = "yaml-rust" version = "0.4.5" diff --git a/Cargo.toml b/Cargo.toml index fdd9fc1..6a319ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,3 +32,4 @@ futures = "0.3.18" async-trait = "0.1.52" nm-reactor_dbus_codegen = {path = "dbus_codegen"} async-recursion = "1.0" +xdg = "2.4" diff --git a/src/main.rs b/src/main.rs index 5b783f8..4714b64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,12 +63,21 @@ impl AppOpts for Opts { } impl ConfigurableAppOpts<config::Config> for Opts { - fn get_additional_config_paths(&self) -> Vec<(PathBuf, Option<ConfigFileFormat>)> { + fn get_additional_config_paths(&self) -> Vec<(PathBuf, std::option::Option<ConfigFileFormat>)> { + let mut paths = vec![]; + + let xdg_dirs = xdg::BaseDirectories::with_prefix("nm-reactor").unwrap(); + let xdg_config_path = xdg_dirs.find_config_file("config.yaml"); + + if let Some(xdg_config_path) = xdg_config_path { + paths.push((xdg_config_path, None)); + } + if let Some(config_path) = &self.config { - vec![(config_path.clone(), None)] - } else { - vec![] + paths.push((config_path.clone(), None)) } + + paths } fn get_config_file_format(&self) -> ConfigFileFormat { -- GitLab