From fa0105fa96101a12fa54df49840fb8a33d093f59 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Tue, 9 Nov 2021 10:29:46 +0100 Subject: [PATCH] cli: Add a "send test email" command Still unimplemented. This re-organizes the command-line flags. --- server/src/infra/cli.rs | 91 ++++++++++++++++++++++++++----- server/src/infra/configuration.rs | 9 ++- server/src/main.rs | 1 + 3 files changed, 84 insertions(+), 17 deletions(-) diff --git a/server/src/infra/cli.rs b/server/src/infra/cli.rs index 175cac0..2fe7cf5 100644 --- a/server/src/infra/cli.rs +++ b/server/src/infra/cli.rs @@ -1,4 +1,5 @@ use clap::Clap; +use lettre::message::Mailbox; /// lldap is a lightweight LDAP server #[derive(Debug, Clap, Clone)] @@ -17,31 +18,93 @@ pub enum Command { /// Run the LDAP and GraphQL server. #[clap(name = "run")] Run(RunOpts), + /// Send a test email. + #[clap(name = "send_test_email")] + SendTestEmail(TestEmailOpts), } #[derive(Debug, Clap, Clone)] -pub struct RunOpts { +pub struct GeneralConfigOpts { /// Change config file name. - #[clap(short, long, default_value = "lldap_config.toml")] + #[clap( + short, + long, + default_value = "lldap_config.toml", + env = "LLDAP_CONFIG_FILE" + )] pub config_file: String, - /// Change ldap port. Default: 3890 - #[clap(long)] - pub ldap_port: Option, - - /// Change ldap ssl port. Default: 6360 - #[clap(long)] - pub ldaps_port: Option, - - /// Change HTTP API port. Default: 17170 - #[clap(long)] - pub http_port: Option, - /// Set verbose logging. #[clap(short, long)] pub verbose: bool, } +#[derive(Debug, Clap, Clone)] +pub struct RunOpts { + #[clap(flatten)] + pub general_config: GeneralConfigOpts, + + /// Change ldap port. Default: 3890 + #[clap(long, env = "LLDAP_LDAP_PORT")] + pub ldap_port: Option, + + /// Change ldap ssl port. Default: 6360 + #[clap(long, env = "LLDAP_LDAPS_PORT")] + pub ldaps_port: Option, + + /// Change HTTP API port. Default: 17170 + #[clap(long, env = "LLDAP_HTTP_PORT")] + pub http_port: Option, + + #[clap(flatten)] + pub smtp_opts: SmtpOpts, +} + +#[derive(Debug, Clap, Clone)] +pub struct TestEmailOpts { + #[clap(flatten)] + pub general_config: GeneralConfigOpts, + + /// Email address to send an email to. + #[clap(long, env = "LLDAP_TEST_EMAIL_TO")] + pub to: String, + + #[clap(flatten)] + pub smtp_opts: SmtpOpts, +} + +#[derive(Debug, Clap, Clone)] +pub struct SmtpOpts { + /// Sender email address. + #[clap(long)] + #[clap(long, env = "LLDAP_SMTP_OPTIONS__FROM")] + pub smtp_from: Option, + + /// Reply-to email address. + #[clap(long, env = "LLDAP_SMTP_OPTIONS__TO")] + pub smtp_reply_to: Option, + + /// SMTP server. + #[clap(long, env = "LLDAP_SMTP_OPTIONS__SERVER")] + pub smtp_server: Option, + + /// SMTP port, 587 by default. + #[clap(long, env = "LLDAP_SMTP_OPTIONS__PORT")] + pub smtp_port: Option, + + /// SMTP user. + #[clap(long, env = "LLDAP_SMTP_OPTIONS__USER")] + pub smtp_user: Option, + + /// SMTP password. + #[clap(long, env = "LLDAP_SMTP_OPTIONS__PASSWORD", hide_env_values = true)] + pub smtp_password: Option, + + /// Whether TLS should be used to connect to SMTP. + #[clap(long, env = "LLDAP_SMTP_OPTIONS__TLS_REQUIRED")] + pub smtp_tls_required: Option, +} + #[derive(Debug, Clap, Clone)] pub struct ExportGraphQLSchemaOpts { /// Output to a file. If not specified, the config is printed to the standard output. diff --git a/server/src/infra/configuration.rs b/server/src/infra/configuration.rs index 49eda50..aa74eca 100644 --- a/server/src/infra/configuration.rs +++ b/server/src/infra/configuration.rs @@ -99,7 +99,7 @@ impl Configuration { } fn merge_with_cli(mut self: Configuration, cli_opts: RunOpts) -> Configuration { - if cli_opts.verbose { + if cli_opts.general_config.verbose { self.verbose = true; } @@ -138,9 +138,12 @@ fn get_server_setup(file_path: &str) -> Result { } pub fn init(cli_opts: RunOpts) -> Result { - let config_file = cli_opts.config_file.clone(); + let config_file = cli_opts.general_config.config_file.clone(); - println!("Loading configuration from {}", cli_opts.config_file); + println!( + "Loading configuration from {}", + cli_opts.general_config.config_file + ); let config: Configuration = Figment::from(Serialized::defaults( ConfigurationBuilder::default().build().unwrap(), diff --git a/server/src/main.rs b/server/src/main.rs index d45a647..15f22ae 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -104,5 +104,6 @@ fn main() -> Result<()> { match cli_opts.command { Command::ExportGraphQLSchema(opts) => infra::graphql::api::export_schema(opts), Command::Run(opts) => run_server_command(opts), + Command::SendTestEmail(_opts) => Ok(()), } }