Skip to content

Commit

Permalink
chore: handle cloud client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
v3g42 committed Oct 4, 2023
1 parent c0d1a7b commit 7ba64f5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
2 changes: 1 addition & 1 deletion dozer-cli/src/cli/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
5 changes: 3 additions & 2 deletions dozer-cli/src/cloud/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -354,7 +355,7 @@ impl DozerGrpcCloudClient for CloudClient {
}

fn login(
&mut self,
runtime: Arc<Runtime>,
cloud: Cloud,
organisation_slug: Option<String>,
profile: Option<String>,
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion dozer-cli/src/cloud/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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<Runtime>,
cloud: Cloud,
organisation_slug: Option<String>,
profile: Option<String>,
Expand Down
82 changes: 52 additions & 30 deletions dozer-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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(),
Expand Down Expand Up @@ -298,7 +317,10 @@ fn init_configuration(cli: &Cli, runtime: Arc<Runtime>) -> Result<Config, CliErr
runtime.spawn(check_update());
Ok(config)
}
Err(e) => Err(e),
Err(e) => {
error!("{}", e);
Err(e)
}
}
},
)
Expand Down

0 comments on commit 7ba64f5

Please sign in to comment.