From 7ba64f5edc67030d21b9900a5214685087bb3076 Mon Sep 17 00:00:00 2001 From: VG Date: Wed, 4 Oct 2023 15:10:57 +0800 Subject: [PATCH] chore: handle cloud client creation --- dozer-cli/src/cli/cloud.rs | 2 +- dozer-cli/src/cloud/client.rs | 5 ++- dozer-cli/src/cloud/mod.rs | 3 +- dozer-cli/src/main.rs | 82 ++++++++++++++++++++++------------- 4 files changed, 58 insertions(+), 34 deletions(-) diff --git a/dozer-cli/src/cli/cloud.rs b/dozer-cli/src/cli/cloud.rs index 3457d47430..ee88e65303 100644 --- a/dozer-cli/src/cli/cloud.rs +++ b/dozer-cli/src/cli/cloud.rs @@ -4,7 +4,7 @@ use clap::ArgAction; use dozer_types::grpc_types::cloud::Secret; use std::error::Error; -#[derive(Debug, Args)] +#[derive(Debug, Args, Clone)] #[command(args_conflicts_with_subcommands = true)] pub struct Cloud { #[arg(global = true, short = 't', long)] diff --git a/dozer-cli/src/cloud/client.rs b/dozer-cli/src/cloud/client.rs index 77beb57bfd..cd4d32a476 100644 --- a/dozer-cli/src/cloud/client.rs +++ b/dozer-cli/src/cloud/client.rs @@ -33,6 +33,7 @@ use dozer_types::prettytable::{row, table}; use futures::{select, FutureExt, StreamExt}; use std::io; use std::sync::Arc; +use tokio::runtime::Runtime; use tonic::transport::Endpoint; use tower::ServiceBuilder; @@ -354,7 +355,7 @@ impl DozerGrpcCloudClient for CloudClient { } fn login( - &mut self, + runtime: Arc, cloud: Cloud, organisation_slug: Option, profile: Option, @@ -374,7 +375,7 @@ impl DozerGrpcCloudClient for CloudClient { Some(name) => name, }; - self.runtime.block_on(async move { + runtime.block_on(async move { let login_svc = LoginSvc::new( organisation_slug, cloud diff --git a/dozer-cli/src/cloud/mod.rs b/dozer-cli/src/cloud/mod.rs index 7411003144..588ea092a2 100644 --- a/dozer-cli/src/cloud/mod.rs +++ b/dozer-cli/src/cloud/mod.rs @@ -12,6 +12,7 @@ pub mod monitor; pub mod progress_printer; mod token_layer; pub use client::CloudClient; +use tokio::runtime::Runtime; pub trait DozerGrpcCloudClient { fn deploy( @@ -26,7 +27,7 @@ pub trait DozerGrpcCloudClient { fn monitor(&mut self, cloud: Cloud) -> Result<(), OrchestrationError>; fn trace_logs(&mut self, cloud: Cloud, logs: LogCommandArgs) -> Result<(), OrchestrationError>; fn login( - &mut self, + runtime: std::sync::Arc, cloud: Cloud, organisation_slug: Option, profile: Option, diff --git a/dozer-cli/src/main.rs b/dozer-cli/src/main.rs index 1ef5c109ea..3946ab9e3f 100644 --- a/dozer-cli/src/main.rs +++ b/dozer-cli/src/main.rs @@ -126,48 +126,44 @@ fn run() -> Result<(), OrchestrationError> { let runtime = Arc::new(Runtime::new().map_err(CliError::FailedToCreateTokioRuntime)?); - let config = init_configuration(&cli, runtime.clone())?; let (shutdown_sender, shutdown_receiver) = shutdown::new(&runtime); set_ctrl_handler(shutdown_sender); - // Now we have access to telemetry configuration. Telemetry must be initialized in tokio runtime. - let app_id = config.cloud.app_id.as_deref().unwrap_or(&config.app_name); - - // We always enable telemetry when running live. - let telemetry_config = if matches!(cli.cmd, Commands::Live(_)) { - TelemetryConfig { - trace: None, - metrics: Some(TelemetryMetricsConfig::Prometheus), - } - } else { - config.telemetry.clone() - }; - - let _telemetry = runtime.block_on(async { Telemetry::new(Some(app_id), &telemetry_config) }); - set_panic_hook(); // Run Cloud #[cfg(feature = "cloud")] - if let Commands::Cloud(cloud) = cli.cmd { + if let Commands::Cloud(cloud) = &cli.cmd { render_logo(); + let cloud = cloud.clone(); + if let CloudCommands::Login { + organisation_slug, + profile_name, + client_id, + client_secret, + } = cloud.command.clone() + { + return CloudClient::login( + runtime.clone(), + cloud, + organisation_slug, + profile_name, + client_id, + client_secret, + ); + } + let config = init_configuration(&cli, runtime.clone())?; let mut cloud_client = CloudClient::new(config.clone(), runtime.clone()); let res = match cloud.command.clone() { CloudCommands::Deploy(deploy) => { cloud_client.deploy(cloud, deploy, cli.config_paths.clone()) } CloudCommands::Login { - organisation_slug, - profile_name, - client_id, - client_secret, - } => cloud_client.login( - cloud, - organisation_slug, - profile_name, - client_id, - client_secret, - ), + organisation_slug: _, + profile_name: _, + client_id: _, + client_secret: _, + } => unreachable!("This is handled earlier"), CloudCommands::Secrets(command) => cloud_client.execute_secrets_command(cloud, command), CloudCommands::Delete => cloud_client.delete(cloud), CloudCommands::Status => cloud_client.status(cloud), @@ -184,8 +180,31 @@ fn run() -> Result<(), OrchestrationError> { cloud_client.print_api_request_samples(cloud, endpoint) } }; - return res; + return match res { + Ok(_) => Ok(()), + Err(e) => { + display_error(&e); + Err(e) + } + }; } + + let config = init_configuration(&cli, runtime.clone())?; + // Now we have access to telemetry configuration. Telemetry must be initialized in tokio runtime. + let app_id = config.cloud.app_id.as_deref().unwrap_or(&config.app_name); + + // We always enable telemetry when running live. + let telemetry_config = if matches!(cli.cmd, Commands::Live(_)) { + TelemetryConfig { + trace: None, + metrics: Some(TelemetryMetricsConfig::Prometheus), + } + } else { + config.telemetry.clone() + }; + + let _telemetry = runtime.block_on(async { Telemetry::new(Some(app_id), &telemetry_config) }); + let mut dozer = init_dozer( runtime.clone(), config.clone(), @@ -298,7 +317,10 @@ fn init_configuration(cli: &Cli, runtime: Arc) -> Result Err(e), + Err(e) => { + error!("{}", e); + Err(e) + } } }, )