Skip to content

Commit

Permalink
add example and bin
Browse files Browse the repository at this point in the history
  • Loading branch information
soywod committed Sep 20, 2024
1 parent 55ecb54 commit 8f0e11a
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
79 changes: 79 additions & 0 deletions examples/keyring.rs
Original file line number Diff line number Diff line change
@@ -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}");
}
6 changes: 3 additions & 3 deletions src/account/command/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
79 changes: 79 additions & 0 deletions src/bin/keyring.rs
Original file line number Diff line number Diff line change
@@ -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}");
}

0 comments on commit 8f0e11a

Please sign in to comment.