♻️ refactor(cli): remove config from run args
- remove config from run args to load inside functions - load credential.json directly instead of config file
This commit is contained in:
committed by
Jeremiah Russell
parent
455007dd82
commit
5905a6fb54
@@ -57,15 +57,12 @@ async fn main() {
|
||||
}
|
||||
|
||||
async fn run(args: Cli) -> Result<()> {
|
||||
let config = get_config()?;
|
||||
log::trace!("Configuration loaded: {config:#?}");
|
||||
|
||||
let mut client = GmailClient::new(config.credential_file()).await?;
|
||||
let mut client = GmailClient::new("credential.json").await?;
|
||||
|
||||
match args.sub_command {
|
||||
SubCmds::Message(messages_cli) => messages_cli.run(&mut client).await,
|
||||
SubCmds::Labels(labels_cli) => labels_cli.run(client).await,
|
||||
SubCmds::Rules(rules_cli) => rules_cli.run(&mut client, config).await,
|
||||
SubCmds::Rules(rules_cli) => rules_cli.run(&mut client).await,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,10 +25,24 @@ pub struct RulesCli {
|
||||
}
|
||||
|
||||
impl RulesCli {
|
||||
pub async fn run(&self, client: &mut GmailClient, config: Rules) -> Result<()> {
|
||||
pub async fn run(&self, client: &mut GmailClient) -> Result<()> {
|
||||
let rules = get_config()?;
|
||||
|
||||
match &self.sub_command {
|
||||
SubCmds::Config(config_cli) => config_cli.run(config),
|
||||
SubCmds::Run(run_cli) => run_cli.run(client, config).await,
|
||||
SubCmds::Config(config_cli) => config_cli.run(rules),
|
||||
SubCmds::Run(run_cli) => run_cli.run(client, rules).await,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_config() -> Result<Rules> {
|
||||
match Rules::load() {
|
||||
Ok(c) => Ok(c),
|
||||
Err(_) => {
|
||||
log::warn!("Configuration not found, creating default config.");
|
||||
let rules = Rules::new();
|
||||
rules.save()?;
|
||||
Ok(rules)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +31,11 @@ pub struct ConfigCli {
|
||||
}
|
||||
|
||||
impl ConfigCli {
|
||||
pub fn run(&self, config: Rules) -> Result<()> {
|
||||
pub fn run(&self, rules: Rules) -> Result<()> {
|
||||
match &self.sub_command {
|
||||
SubCmds::Rules(rules_cli) => rules_cli.run(config),
|
||||
SubCmds::Label(label_cli) => label_cli.run(config),
|
||||
SubCmds::Action(action_cli) => action_cli.run(config),
|
||||
SubCmds::Rules(rules_cli) => rules_cli.run(rules),
|
||||
SubCmds::Label(label_cli) => label_cli.run(rules),
|
||||
SubCmds::Action(action_cli) => action_cli.run(rules),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ pub struct RunCli {
|
||||
}
|
||||
|
||||
impl RunCli {
|
||||
pub async fn run(&self, client: &mut GmailClient, config: Rules) -> Result<()> {
|
||||
let rules = config.get_rules_by_label();
|
||||
pub async fn run(&self, client: &mut GmailClient, rules: Rules) -> Result<()> {
|
||||
let rules_by_labels = rules.get_rules_by_label();
|
||||
|
||||
for label in config.labels() {
|
||||
let Some(rule) = rules.get(&label) else {
|
||||
for label in rules.labels() {
|
||||
let Some(rule) = rules_by_labels.get(&label) else {
|
||||
log::warn!("no rule found for label `{label}`");
|
||||
continue;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user