Skip to content

Commit 2fc04ce

Browse files
authored
fix: relay command startup (#310)
* Make `log_output_path` optional * Avoid multiple `pluto_tracing` initializations * Move `pluto_tracing::init` to main * Simplify `pluto_tracing::init` * Simplify `relay::run` * Simplify main - Allow bubbling up errors - Remove `ExitResult` indirection
1 parent 86d74d8 commit 2fc04ce

4 files changed

Lines changed: 41 additions & 63 deletions

File tree

crates/cli/src/commands/relay.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,9 @@ pub struct RelayLogFlags {
266266
#[arg(
267267
long = "log-output-path",
268268
env = "PLUTO_LOG_OUTPUT_PATH",
269-
default_value = "",
270269
help = "Path in which to write on-disk logs."
271270
)]
272-
pub log_output_path: PathBuf,
271+
pub log_output_path: Option<PathBuf>,
273272
}
274273

275274
#[derive(clap::ValueEnum, Clone, Default)]
@@ -299,19 +298,7 @@ pub struct RelayLokiArgs {
299298
pub loki_service: String,
300299
}
301300

302-
pub async fn run(args: RelayArgs, ct: CancellationToken) -> Result<(), CliError> {
303-
let config: pluto_relay_server::config::Config = args.try_into()?;
304-
305-
let log_config = config
306-
.log_config
307-
.as_ref()
308-
.expect("Log config is always configured");
309-
pluto_tracing::init(log_config).expect("Failed to initialize tracing");
310-
311-
run_with_config(config, ct).await
312-
}
313-
314-
async fn run_with_config(
301+
pub async fn run(
315302
config: pluto_relay_server::config::Config,
316303
ct: CancellationToken,
317304
) -> Result<(), CliError> {
@@ -350,7 +337,7 @@ async fn run_with_config(
350337
#[cfg(test)]
351338
mod tests {
352339
use backon::{BackoffBuilder, Retryable};
353-
use std::{path::PathBuf, str::FromStr, time};
340+
use std::{str::FromStr, time};
354341
use tokio::net;
355342
use tokio_util::sync::CancellationToken;
356343

@@ -547,7 +534,7 @@ mod tests {
547534
format: "console".into(),
548535
level: "error".into(),
549536
color: super::ConsoleColor::Disable,
550-
log_output_path: PathBuf::new(),
537+
log_output_path: None,
551538
},
552539
loki: super::RelayLokiArgs {
553540
loki_addresses: vec![],
@@ -559,7 +546,7 @@ mod tests {
559546
let cfg: pluto_relay_server::config::Config = args.clone().try_into().unwrap();
560547
let ct = CancellationToken::new();
561548

562-
let relay = tokio::spawn(super::run_with_config(cfg.clone(), ct.child_token()));
549+
let relay = tokio::spawn(super::run(cfg.clone(), ct.child_token()));
563550

564551
test_fn(cfg.clone()).await;
565552

crates/cli/src/error.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
//! Error types for the Pluto CLI.
22
3-
use std::{
4-
path::PathBuf,
5-
process::{ExitCode, Termination},
6-
};
7-
8-
use thiserror::Error;
3+
use std::path::PathBuf;
94

105
/// Result type for CLI operations.
116
pub type Result<T> = std::result::Result<T, CliError>;
127

13-
pub struct ExitResult(pub Result<()>);
14-
15-
impl Termination for ExitResult {
16-
fn report(self) -> ExitCode {
17-
match self.0 {
18-
Ok(()) => ExitCode::SUCCESS,
19-
Err(err) => {
20-
eprintln!("Error: {}", err);
21-
ExitCode::FAILURE
22-
}
23-
}
24-
}
25-
}
26-
278
/// Errors that can occur in the Pluto CLI.
28-
#[derive(Error, Debug)]
9+
#[derive(thiserror::Error, Debug)]
2910
pub enum CliError {
3011
/// Private key file not found.
3112
#[error(
@@ -87,11 +68,15 @@ pub enum CliError {
8768
#[error("test case not supported")]
8869
_TestCaseNotSupported,
8970

90-
/// Generic error with message.
91-
#[error("{0}")]
92-
Other(String),
93-
9471
/// Relay P2P error.
9572
#[error("Relay P2P error: {0}")]
9673
RelayP2PError(#[from] pluto_relay_server::error::RelayP2PError),
74+
75+
/// Command parsing error.
76+
#[error("Command parsing error: {0}")]
77+
CommandParsingError(#[from] clap::Error),
78+
79+
/// Generic error with message.
80+
#[error("{0}")]
81+
Other(String),
9782
}

crates/cli/src/main.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,33 @@
44
//! This crate provides the CLI tools and commands for managing and operating
55
//! Pluto validator nodes.
66
7+
use crate::error::CliError;
78
use clap::{CommandFactory, FromArgMatches};
9+
use cli::{AlphaCommands, Cli, Commands, CreateCommands, TestCommands};
10+
use std::process::ExitCode;
11+
use tokio_util::sync::CancellationToken;
812

913
mod ascii;
1014
mod cli;
1115
mod commands;
1216
mod duration;
1317
mod error;
1418

15-
use cli::{AlphaCommands, Cli, Commands, CreateCommands, TestCommands};
16-
17-
use crate::error::ExitResult;
18-
use tokio_util::sync::CancellationToken;
19-
2019
#[tokio::main]
21-
async fn main() -> ExitResult {
22-
let config = pluto_tracing::TracingConfig::builder()
23-
.with_default_console()
24-
.build();
25-
let _ = pluto_tracing::init(&config);
20+
async fn main() -> ExitCode {
21+
match run().await {
22+
Ok(()) => ExitCode::SUCCESS,
23+
Err(err) => {
24+
eprintln!("Error: {}", err);
25+
ExitCode::FAILURE
26+
}
27+
}
28+
}
2629

30+
async fn run() -> std::result::Result<(), CliError> {
2731
let cmd = commands::test::update_test_cases_help(Cli::command());
2832
let matches = cmd.get_matches();
29-
let cli = match Cli::from_arg_matches(&matches) {
30-
Ok(cli) => cli,
31-
Err(e) => return ExitResult(Err(error::CliError::Other(e.to_string()))),
32-
};
33+
let cli = Cli::from_arg_matches(&matches)?;
3334

3435
// Top level cancellation token for graceful shutdown on Ctrl+C
3536
let ct = CancellationToken::new();
@@ -41,13 +42,17 @@ async fn main() -> ExitResult {
4142
}
4243
});
4344

44-
let result = match cli.command {
45+
match cli.command {
4546
Commands::Create(args) => match args.command {
4647
CreateCommands::Enr(args) => commands::create_enr::run(args),
4748
},
4849
Commands::Enr(args) => commands::enr::run(args),
4950
Commands::Version(args) => commands::version::run(args),
50-
Commands::Relay(args) => commands::relay::run(*args, ct.clone()).await,
51+
Commands::Relay(args) => {
52+
let config: pluto_relay_server::config::Config = (*args).clone().try_into()?;
53+
pluto_tracing::init(&config.log_config).expect("Failed to initialize tracing");
54+
commands::relay::run(config, ct.clone()).await
55+
}
5156
Commands::Alpha(args) => match args.command {
5257
AlphaCommands::Test(args) => {
5358
let mut stdout = std::io::stdout();
@@ -56,6 +61,8 @@ async fn main() -> ExitResult {
5661
.await
5762
.map(|_| ()),
5863
TestCommands::Beacon(args) => {
64+
pluto_tracing::init(&pluto_tracing::TracingConfig::default())
65+
.expect("Failed to initialize tracing");
5966
commands::test::beacon::run(args, &mut stdout, ct.clone())
6067
.await
6168
.map(|_| ())
@@ -75,7 +82,5 @@ async fn main() -> ExitResult {
7582
}
7683
}
7784
},
78-
};
79-
80-
ExitResult(result)
85+
}
8186
}

crates/relay-server/src/config.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ pub struct Config {
2929
/// The P2P configuration.
3030
pub p2p_config: P2PConfig,
3131
/// The logging configuration.
32-
pub log_config: Option<TracingConfig>,
32+
#[builder(default)]
33+
pub log_config: TracingConfig,
3334
/// Whether to automatically generate a P2P key.
3435
#[builder(default = false)]
3536
pub auto_p2p_key: bool,

0 commit comments

Comments
 (0)