diff --git a/Cargo.lock b/Cargo.lock index b96eaef..f24d0c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -240,6 +240,19 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" +[[package]] +name = "config" +version = "0.15.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "180e549344080374f9b32ed41bf3b6b57885ff6a289367b3dbc10eea8acc1918" +dependencies = [ + "pathdiff", + "serde_core", + "serde_json", + "toml", + "winnow", +] + [[package]] name = "core-foundation" version = "0.10.1" @@ -263,6 +276,7 @@ dependencies = [ "chrono", "clap", "clap-verbosity-flag", + "config", "env_logger", "google-gmail1", "log", @@ -1017,6 +1031,12 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e" +[[package]] +name = "pathdiff" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3" + [[package]] name = "percent-encoding" version = "2.3.2" @@ -1964,6 +1984,9 @@ name = "winnow" version = "0.7.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf" +dependencies = [ + "memchr", +] [[package]] name = "writeable" diff --git a/Cargo.toml b/Cargo.toml index 35fa91d..68216b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ include = [ chrono = "0.4.42" clap = { version = "4.5.48", features = ["derive"] } clap-verbosity-flag = { version = "3.0.4", features = ["tracing"] } +config = { version = "0.15.18", default-features = false, features = ["json", "toml"] } env_logger = "0.11.8" google-gmail1 = "6.0.0" log = "0.4.28" diff --git a/src/cli/main.rs b/src/cli/main.rs index dd1be33..8c38d6f 100644 --- a/src/cli/main.rs +++ b/src/cli/main.rs @@ -4,8 +4,9 @@ mod labels_cli; mod messages_cli; mod rules_cli; -use cull_gmail::{GmailClient, Result, Rules}; -use std::error::Error as stdError; +use config::Config; +use cull_gmail::{GmailClient, Result}; +use std::{env, error::Error as stdError}; use labels_cli::LabelsCli; use messages_cli::MessagesCli; @@ -57,7 +58,10 @@ async fn main() { } async fn run(args: Cli) -> Result<()> { - let mut client = GmailClient::new("credential.json").await?; + let config = get_config()?; + + let credential = config.get_string("credentials")?; + let mut client = GmailClient::new(&credential).await?; match args.sub_command { SubCmds::Message(messages_cli) => messages_cli.run(&mut client).await, @@ -83,23 +87,19 @@ fn get_logging(level: log::LevelFilter) -> env_logger::Builder { builder } -fn get_config() -> Result { - // let settings = Config::builder() - // // Add in `./Settings.toml` - // .add_source(config::File::with_name("examples/simple/Settings")) - // // Add in settings from the environment (with a prefix of APP) - // // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key - // .add_source(config::Environment::with_prefix("APP")) - // .build() - // .unwrap(); +fn get_config() -> Result { + let home_dir = env::home_dir().unwrap(); + let path = home_dir.join(".cull-gmail/rules.toml"); + log::trace!("Loading config from {}", path.display()); - match Rules::load() { - Ok(c) => Ok(c), - Err(_) => { - log::warn!("Configuration not found, creating default config."); - let rules = Rules::new(); - rules.save()?; - Ok(rules) - } - } + Ok(Config::builder() + .set_default("credentials", "credential.json")? + // Add in `./Settings.toml` + .add_source(config::File::with_name( + path.to_path_buf().to_str().unwrap(), + )) + // Add in settings from the environment (with a prefix of APP) + // Eg.. `APP_DEBUG=1 ./target/app` would set the `debug` key + .add_source(config::Environment::with_prefix("APP")) + .build()?) } diff --git a/src/error.rs b/src/error.rs index 2e80582..7b83e51 100644 --- a/src/error.rs +++ b/src/error.rs @@ -45,4 +45,7 @@ pub enum Error { /// Error from toml_de #[error(transparent)] TomlDe(#[from] toml::de::Error), + /// Error from config + #[error(transparent)] + Config(#[from] config::ConfigError), }