diff --git a/src/cli/cli_args.rs b/src/cli/cli_args.rs index 122dd75..5416b54 100644 --- a/src/cli/cli_args.rs +++ b/src/cli/cli_args.rs @@ -24,7 +24,7 @@ pub struct CliArgs { pub enum HuskSubcommands { Daemon(DaemonCommand), Introducer(IntroducerCommand), - Local, + Local(LocalCommand), } // Daemon subcommands @@ -62,10 +62,10 @@ pub enum DaemonSubcommand { )] pub struct IntroducerCommand { #[clap(subcommand)] - pub subcommand: IntroducerSubcommands, + pub subcommand: IntroducerSubcommand, } #[derive(Debug, Subcommand)] -pub enum IntroducerSubcommands { +pub enum IntroducerSubcommand { Add(IntroducerAddCommand), Remove(IntroducerRemoveCommand), List @@ -105,3 +105,52 @@ pub struct IntroducerRemoveCommand { )] pub cert: String, } + +// Local subcommands + +#[derive(Parser, Debug)] +#[clap( + name = "local", + about = "Manage manually added certificates.", + subcommand_required = true, + arg_required_else_help = true, + disable_colored_help = true, + disable_version_flag = true, +)] +pub struct LocalCommand { + #[clap(subcommand)] + pub subcommand: LocalSubcommand, +} +#[derive(Debug, Subcommand)] +pub enum LocalSubcommand { + Add(LocalAddCommand), + Remove(LocalRemoveCommand), + List +} + +#[derive(Parser, Debug)] +#[clap( + name = "local_add", + about = "Add an authenticated certificate.", +)] +pub struct LocalAddCommand { + #[clap( + long = "cert", + help = "Certificate to add." + )] + pub cert: String, +} + +#[derive(Parser, Debug)] +#[clap( + name = "local_remove", + about = "Certificate to remove.", +)] +pub struct LocalRemoveCommand { + #[clap( + long = "cert", + help = "Certificate to removed." + )] + pub cert: String, +} + diff --git a/src/commands.rs b/src/commands.rs index 2ca8edf..26b4f54 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -5,6 +5,8 @@ use crate::cli::cli_args::{CliArgs, HuskSubcommands}; use crate::config::HuskConfigContainer; pub mod daemon; +pub mod introducer; +pub mod local; pub async fn dispatch(cli: CliArgs, config: HuskConfigContainer) -> Result<()> { @@ -12,7 +14,12 @@ pub async fn dispatch(cli: CliArgs, config: HuskConfigContainer) -> Result<()> { HuskSubcommands::Daemon(subcmd) => { commands::daemon::dispatch(subcmd, config).await?; }, - _ => { println!("something else"); } + HuskSubcommands::Introducer(subcmd) => { + commands::introducer::dispatch(subcmd, config).await?; + }, + HuskSubcommands::Local(subcmd) => { + commands::local::dispatch(subcmd, config).await?; + }, } Ok(()) diff --git a/src/commands/introducer.rs b/src/commands/introducer.rs new file mode 100644 index 0000000..9b76179 --- /dev/null +++ b/src/commands/introducer.rs @@ -0,0 +1,34 @@ +use anyhow::Result; + +use crate::cli::cli_args::{IntroducerCommand, IntroducerSubcommand}; +use crate::config::HuskConfigContainer; +use crate::types::husk_context::HuskContext; +use crate::crypto; + +pub mod add; +pub mod remove; + +pub async fn dispatch(cmd: IntroducerCommand, config: HuskConfigContainer) -> Result<()> { + match cmd.subcommand { + IntroducerSubcommand::Add(add_cmd) => { + add::dispatch(add_cmd, &config).await?; + }, + IntroducerSubcommand::Remove(rm_cmd) => { + remove::dispatch(rm_cmd, &config).await?; + }, + IntroducerSubcommand::List => { + println!("list"); + let context = HuskContext::new(&config.into())?; + + for introducer in crypto::get_introducers( + &context.cert_store, + &context.policy, + context.local_trust_root.fingerprint()) { + + println!("{}", introducer); + } + }, + } + + Ok(()) +} diff --git a/src/commands/introducer/add.rs b/src/commands/introducer/add.rs new file mode 100644 index 0000000..3ac238c --- /dev/null +++ b/src/commands/introducer/add.rs @@ -0,0 +1,12 @@ +use anyhow::Result; + +use crate::cli::cli_args::IntroducerAddCommand; +use crate::config::HuskConfigContainer; + +pub async fn dispatch(cmd: IntroducerAddCommand, _config: &HuskConfigContainer) -> Result<()> { + println!("add"); + println!(" {:?}", cmd.cert); + println!(" {:?}", cmd.domains); + Ok(()) +} + diff --git a/src/commands/introducer/remove.rs b/src/commands/introducer/remove.rs new file mode 100644 index 0000000..8e05611 --- /dev/null +++ b/src/commands/introducer/remove.rs @@ -0,0 +1,11 @@ +use anyhow::Result; + +use crate::cli::cli_args::IntroducerRemoveCommand; +use crate::config::HuskConfigContainer; + +pub async fn dispatch(cmd: IntroducerRemoveCommand, _config: &HuskConfigContainer) -> Result<()> { + println!("remove"); + println!(" {:?}", cmd.cert); + Ok(()) +} + diff --git a/src/commands/local.rs b/src/commands/local.rs new file mode 100644 index 0000000..f5ad066 --- /dev/null +++ b/src/commands/local.rs @@ -0,0 +1,23 @@ +use anyhow::Result; + +use crate::cli::cli_args::{LocalCommand, LocalSubcommand}; +use crate::config::HuskConfigContainer; + +pub mod add; +pub mod remove; + +pub async fn dispatch(cmd: LocalCommand, config: HuskConfigContainer) -> Result<()> { + match cmd.subcommand { + LocalSubcommand::Add(add_cmd) => { + add::dispatch(add_cmd, &config).await?; + }, + LocalSubcommand::Remove(rm_cmd) => { + remove::dispatch(rm_cmd, &config).await?; + }, + LocalSubcommand::List => { + println!("list"); + }, + } + + Ok(()) +} diff --git a/src/commands/local/add.rs b/src/commands/local/add.rs new file mode 100644 index 0000000..059c4ec --- /dev/null +++ b/src/commands/local/add.rs @@ -0,0 +1,12 @@ +use anyhow::Result; + +use crate::cli::cli_args::LocalAddCommand; +use crate::config::HuskConfigContainer; + +pub async fn dispatch(cmd: LocalAddCommand, _config: &HuskConfigContainer) -> Result<()> { + println!("add"); + println!(" {:?}", cmd.cert); + Ok(()) +} + + diff --git a/src/commands/local/remove.rs b/src/commands/local/remove.rs new file mode 100644 index 0000000..9f7fa27 --- /dev/null +++ b/src/commands/local/remove.rs @@ -0,0 +1,12 @@ +use anyhow::Result; + +use crate::cli::cli_args::LocalRemoveCommand; +use crate::config::HuskConfigContainer; + +pub async fn dispatch(cmd: LocalRemoveCommand, _config: &HuskConfigContainer) -> Result<()> { + println!("remove"); + println!(" {:?}", cmd.cert); + Ok(()) +} + + diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs index 3d6be54..c007d77 100644 --- a/src/daemon/mod.rs +++ b/src/daemon/mod.rs @@ -15,7 +15,6 @@ use indymilter::{ Callbacks, Context, EomContext, - Macros, NegotiateContext, ProtoOpts, SocketInfo, @@ -248,8 +247,9 @@ impl Daemon { Status::Continue } +/* fn print_macros(macros: &Macros) { println!(" macros: {:?}", macros.to_hash_map()); } - +*/ } diff --git a/src/main.rs b/src/main.rs index 56ba307..1e2ffa1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,12 +42,8 @@ async fn main() { let config_container: HuskConfigContainer = Arc::new(Mutex::new(husk_config)); - match commands::dispatch(args, config_container).await { - Ok(_) => { - println!("exiting..."); - }, - Err(e) => { - println!("exit with {:?}", e); - } + if let Err(e) = commands::dispatch(args, config_container).await { + eprintln!("exit with {:?}", e); + // XXX: actually return an error code } } diff --git a/src/types/introducer.rs b/src/types/introducer.rs index 6b3af39..7bbfdee 100644 --- a/src/types/introducer.rs +++ b/src/types/introducer.rs @@ -56,3 +56,17 @@ impl Introducer { } } +use std::fmt::{Display, Formatter}; +impl Display for Introducer { + + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let uids = self.cert.userids() + .map(|u| String::from_utf8_lossy(u.userid().value()).to_string()) + .collect::>() + .join(", "); + write!(f, "Introducer: {}\n", self.cert.fingerprint())?; + write!(f, " UserIDs: {}", uids)?; + Ok(()) + } +} +