✨ feat(cli): add config file support
- load configuration from a file - use config crate for config loading - allow overriding config with environment variables - set default credential path - fix error handling for config loading
This commit is contained in:
committed by
Jeremiah Russell
parent
6b9ce2670e
commit
c53ad65368
23
Cargo.lock
generated
23
Cargo.lock
generated
@@ -240,6 +240,19 @@ version = "1.0.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
|
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]]
|
[[package]]
|
||||||
name = "core-foundation"
|
name = "core-foundation"
|
||||||
version = "0.10.1"
|
version = "0.10.1"
|
||||||
@@ -263,6 +276,7 @@ dependencies = [
|
|||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"clap-verbosity-flag",
|
"clap-verbosity-flag",
|
||||||
|
"config",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"google-gmail1",
|
"google-gmail1",
|
||||||
"log",
|
"log",
|
||||||
@@ -1017,6 +1031,12 @@ version = "0.1.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
|
checksum = "d05e27ee213611ffe7d6348b942e8f942b37114c00cc03cec254295a4a17852e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pathdiff"
|
||||||
|
version = "0.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df94ce210e5bc13cb6651479fa48d14f601d9858cfe0467f43ae157023b938d3"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "percent-encoding"
|
name = "percent-encoding"
|
||||||
version = "2.3.2"
|
version = "2.3.2"
|
||||||
@@ -1964,6 +1984,9 @@ name = "winnow"
|
|||||||
version = "0.7.13"
|
version = "0.7.13"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf"
|
checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "writeable"
|
name = "writeable"
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ include = [
|
|||||||
chrono = "0.4.42"
|
chrono = "0.4.42"
|
||||||
clap = { version = "4.5.48", features = ["derive"] }
|
clap = { version = "4.5.48", features = ["derive"] }
|
||||||
clap-verbosity-flag = { version = "3.0.4", features = ["tracing"] }
|
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"
|
env_logger = "0.11.8"
|
||||||
google-gmail1 = "6.0.0"
|
google-gmail1 = "6.0.0"
|
||||||
log = "0.4.28"
|
log = "0.4.28"
|
||||||
|
|||||||
@@ -4,8 +4,9 @@ mod labels_cli;
|
|||||||
mod messages_cli;
|
mod messages_cli;
|
||||||
mod rules_cli;
|
mod rules_cli;
|
||||||
|
|
||||||
use cull_gmail::{GmailClient, Result, Rules};
|
use config::Config;
|
||||||
use std::error::Error as stdError;
|
use cull_gmail::{GmailClient, Result};
|
||||||
|
use std::{env, error::Error as stdError};
|
||||||
|
|
||||||
use labels_cli::LabelsCli;
|
use labels_cli::LabelsCli;
|
||||||
use messages_cli::MessagesCli;
|
use messages_cli::MessagesCli;
|
||||||
@@ -57,7 +58,10 @@ async fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn run(args: Cli) -> Result<()> {
|
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 {
|
match args.sub_command {
|
||||||
SubCmds::Message(messages_cli) => messages_cli.run(&mut client).await,
|
SubCmds::Message(messages_cli) => messages_cli.run(&mut client).await,
|
||||||
@@ -83,23 +87,19 @@ fn get_logging(level: log::LevelFilter) -> env_logger::Builder {
|
|||||||
builder
|
builder
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_config() -> Result<Rules> {
|
fn get_config() -> Result<Config> {
|
||||||
// let settings = Config::builder()
|
let home_dir = env::home_dir().unwrap();
|
||||||
// // Add in `./Settings.toml`
|
let path = home_dir.join(".cull-gmail/rules.toml");
|
||||||
// .add_source(config::File::with_name("examples/simple/Settings"))
|
log::trace!("Loading config from {}", path.display());
|
||||||
// // 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();
|
|
||||||
|
|
||||||
match Rules::load() {
|
Ok(Config::builder()
|
||||||
Ok(c) => Ok(c),
|
.set_default("credentials", "credential.json")?
|
||||||
Err(_) => {
|
// Add in `./Settings.toml`
|
||||||
log::warn!("Configuration not found, creating default config.");
|
.add_source(config::File::with_name(
|
||||||
let rules = Rules::new();
|
path.to_path_buf().to_str().unwrap(),
|
||||||
rules.save()?;
|
))
|
||||||
Ok(rules)
|
// 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()?)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,4 +45,7 @@ pub enum Error {
|
|||||||
/// Error from toml_de
|
/// Error from toml_de
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
TomlDe(#[from] toml::de::Error),
|
TomlDe(#[from] toml::de::Error),
|
||||||
|
/// Error from config
|
||||||
|
#[error(transparent)]
|
||||||
|
Config(#[from] config::ConfigError),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user