From 8f0e11a260e170762001cdc395af262246f7e057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Fri, 20 Sep 2024 13:50:10 +0200 Subject: [PATCH] add example and bin --- Cargo.toml | 8 ++++ examples/keyring.rs | 79 ++++++++++++++++++++++++++++++++++++++ src/account/command/mod.rs | 6 +-- src/bin/keyring.rs | 79 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 examples/keyring.rs create mode 100644 src/bin/keyring.rs diff --git a/Cargo.toml b/Cargo.toml index d24d772e..16afc7b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -78,6 +78,14 @@ tracing = "0.1" url = "2.2" uuid = { version = "0.8", features = ["v4"] } +[[example]] +name = "keyring" +required-features = ["imap", "keyring"] + +[[bin]] +name = "keyring" +required-features = ["imap", "keyring"] + [patch.crates-io] # IMAP imap-next = { git = "https://github.com/duesee/imap-next" } diff --git a/examples/keyring.rs b/examples/keyring.rs new file mode 100644 index 00000000..46d0058d --- /dev/null +++ b/examples/keyring.rs @@ -0,0 +1,79 @@ +use std::{collections::HashMap, env}; + +use email::{ + account::config::passwd::PasswdConfig, + imap::config::{ImapAuthConfig, ImapConfig}, +}; +use himalaya::{ + account::{ + arg::name::AccountNameArg, command::configure::AccountConfigureCommand, + config::TomlAccountConfig, + }, + config::Config, + output::OutputFmt, + printer::StdoutPrinter, +}; +use pimalaya_tui::tracing::Tracing; +use secret::{keyring::KeyringEntry, Secret}; +use tracing::info; + +#[tokio::main] +async fn main() { + env::set_var("RUST_LOG", "debug"); + Tracing::install().unwrap(); + + secret::keyring::set_global_service_name("himalaya-cli"); + + info!("checking keyring-lib"); + + let entry = KeyringEntry::try_new("key").unwrap(); + entry.set_secret("val").await.unwrap(); + assert_eq!("val", entry.get_secret().await.unwrap()); + + info!("checking secret-lib"); + + let mut secret = Secret::new_keyring_entry(entry); + assert_eq!(secret.get().await.unwrap(), "val"); + + secret.set("val2").await.unwrap(); + assert_eq!(secret.get().await.unwrap(), "val2"); + + info!("checking email-lib"); + + let config = PasswdConfig(secret); + config.reset().await.unwrap(); + config.configure(|| Ok(String::from("val3"))).await.unwrap(); + assert_eq!(config.get().await.unwrap(), "val3"); + + info!("checking himalaya"); + + let mut printer = StdoutPrinter::new(OutputFmt::Plain); + let cmd = AccountConfigureCommand { + account: AccountNameArg { + name: String::from("account"), + }, + reset: true, + }; + + cmd.execute( + &mut printer, + &Config { + accounts: HashMap::from_iter([( + String::from("account"), + TomlAccountConfig { + imap: Some(ImapConfig { + auth: ImapAuthConfig::Passwd(config.clone()), + ..Default::default() + }), + ..Default::default() + }, + )]), + ..Default::default() + }, + ) + .await + .unwrap(); + + let secret = config.get().await.unwrap(); + println!("secret: {secret}"); +} diff --git a/src/account/command/mod.rs b/src/account/command/mod.rs index dca5739a..fd1a7baa 100644 --- a/src/account/command/mod.rs +++ b/src/account/command/mod.rs @@ -1,6 +1,6 @@ -mod check_up; -mod configure; -mod list; +pub mod check_up; +pub mod configure; +pub mod list; use clap::Subcommand; use color_eyre::Result; diff --git a/src/bin/keyring.rs b/src/bin/keyring.rs new file mode 100644 index 00000000..46d0058d --- /dev/null +++ b/src/bin/keyring.rs @@ -0,0 +1,79 @@ +use std::{collections::HashMap, env}; + +use email::{ + account::config::passwd::PasswdConfig, + imap::config::{ImapAuthConfig, ImapConfig}, +}; +use himalaya::{ + account::{ + arg::name::AccountNameArg, command::configure::AccountConfigureCommand, + config::TomlAccountConfig, + }, + config::Config, + output::OutputFmt, + printer::StdoutPrinter, +}; +use pimalaya_tui::tracing::Tracing; +use secret::{keyring::KeyringEntry, Secret}; +use tracing::info; + +#[tokio::main] +async fn main() { + env::set_var("RUST_LOG", "debug"); + Tracing::install().unwrap(); + + secret::keyring::set_global_service_name("himalaya-cli"); + + info!("checking keyring-lib"); + + let entry = KeyringEntry::try_new("key").unwrap(); + entry.set_secret("val").await.unwrap(); + assert_eq!("val", entry.get_secret().await.unwrap()); + + info!("checking secret-lib"); + + let mut secret = Secret::new_keyring_entry(entry); + assert_eq!(secret.get().await.unwrap(), "val"); + + secret.set("val2").await.unwrap(); + assert_eq!(secret.get().await.unwrap(), "val2"); + + info!("checking email-lib"); + + let config = PasswdConfig(secret); + config.reset().await.unwrap(); + config.configure(|| Ok(String::from("val3"))).await.unwrap(); + assert_eq!(config.get().await.unwrap(), "val3"); + + info!("checking himalaya"); + + let mut printer = StdoutPrinter::new(OutputFmt::Plain); + let cmd = AccountConfigureCommand { + account: AccountNameArg { + name: String::from("account"), + }, + reset: true, + }; + + cmd.execute( + &mut printer, + &Config { + accounts: HashMap::from_iter([( + String::from("account"), + TomlAccountConfig { + imap: Some(ImapConfig { + auth: ImapAuthConfig::Passwd(config.clone()), + ..Default::default() + }), + ..Default::default() + }, + )]), + ..Default::default() + }, + ) + .await + .unwrap(); + + let secret = config.get().await.unwrap(); + println!("secret: {secret}"); +}