Skip to content

Commit

Permalink
refactor: align return codes from cli_main, server_main and client_main
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyscot committed Jan 8, 2025
1 parent e9e651a commit 7f2b243
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
64 changes: 25 additions & 39 deletions src/cli/cli_main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
//! Main CLI entrypoint for qcp
//! Main CLI for qcp
// (c) 2024 Ross Younger

use std::process::ExitCode;

use super::args::CliArgs;
use crate::{
client::{client_main, Parameters as ClientParameters, MAX_UPDATE_FPS},
Expand Down Expand Up @@ -31,6 +29,8 @@ enum MainMode {
Server,
Client(MultiProgress),
ShowConfig,
HelpBuffers,
ShowConfigFiles,
}

impl From<&CliArgs> for MainMode {
Expand All @@ -39,6 +39,10 @@ impl From<&CliArgs> for MainMode {
MainMode::Server
} else if args.show_config {
MainMode::ShowConfig
} else if args.help_buffers {
MainMode::HelpBuffers
} else if args.config_files {
MainMode::ShowConfigFiles
} else {
MainMode::Client(MultiProgress::with_draw_target(
ProgressDrawTarget::stderr_with_hz(MAX_UPDATE_FPS),
Expand All @@ -59,32 +63,13 @@ impl MainMode {
/// Main CLI entrypoint
///
/// Call this from `main`. It reads argv.
/// # Exit status
/// 0 indicates success; non-zero indicates failure.
/// # Return
/// true indicates success. false indicates a failure we have logged. An Error is a failure we have not output or logged.
#[tokio::main(flavor = "current_thread")]
#[allow(clippy::missing_panics_doc)]
pub async fn cli() -> anyhow::Result<ExitCode> {
pub async fn cli() -> anyhow::Result<bool> {
let args = CliArgs::custom_parse();
if args.help_buffers {
os::print_udp_buffer_size_help_message(
Configuration::recv_buffer(),
Configuration::send_buffer(),
);
return Ok(ExitCode::SUCCESS);
}
if args.config_files {
// do this before attempting to read config, in case it fails
setup_tracing(
trace_level(&args.client_params),
None,
&None,
args.config.time_format.unwrap_or_default(),
)?;
println!("{:?}", Manager::config_files());
return Ok(ExitCode::SUCCESS);
}

let main_mode = MainMode::from(&args); // side-effect: holds progress bar, if we need one
let mode = MainMode::from(&args); // side-effect: holds progress bar, if we need one

// Now fold the arguments in with the CLI config (which may fail)
// (to provoke an error here: `qcp host: host2:`)
Expand All @@ -93,31 +78,32 @@ pub async fn cli() -> anyhow::Result<ExitCode> {

setup_tracing(
trace_level(&args.client_params),
main_mode.progress(),
mode.progress(),
&args.client_params.log_file,
config.time_format,
)?; // to provoke error: set RUST_LOG=.

match main_mode {
match mode {
MainMode::HelpBuffers => {
os::print_udp_buffer_size_help_message(
Configuration::recv_buffer(),
Configuration::send_buffer(),
);
}
MainMode::ShowConfigFiles => {
println!("{:?}", Manager::config_files());
}
MainMode::ShowConfig => {
println!("{}", config_manager.to_display_adapter::<Configuration>());
return Ok(ExitCode::SUCCESS);
}
MainMode::Server => {
let _span = error_span!("REMOTE").entered();
server_main(&config).await?;
return Ok(ExitCode::SUCCESS);
}
MainMode::Client(progress) => {
return client_main(&config, progress, args.client_params)
.await
.map(|success| {
if success {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
}
});
// this mode may return false
return client_main(&config, progress, args.client_params).await;
}
};
Ok(true)
}
22 changes: 12 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
// (c) 2024 Ross Younger

use qcp::styles::{ERROR, RESET};
use std::process::ExitCode;

#[cfg(all(target_env = "musl", target_pointer_width = "64"))]
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn main() -> std::process::ExitCode {
match qcp::cli() {
Ok(code) => code,
Err(e) => {
if qcp::util::tracing_is_initialised() {
tracing::error!("{e}");
} else {
eprintln!("{ERROR}Error:{RESET} {e}");
}
std::process::ExitCode::FAILURE
fn main() -> ExitCode {
if qcp::cli().unwrap_or_else(|e| {
if qcp::util::tracing_is_initialised() {
tracing::error!("{e}");
} else {
eprintln!("{ERROR}Error:{RESET} {e}");
}
false
}) {
ExitCode::SUCCESS
} else {
ExitCode::FAILURE
}
}

0 comments on commit 7f2b243

Please sign in to comment.