From cdebcbd4dc627b587ce721eebc93e3591f5bd552 Mon Sep 17 00:00:00 2001 From: veeso Date: Sat, 15 Mar 2025 16:54:36 +0100 Subject: [PATCH] fix: the return value of `--version` should be `0` fix #317 --- CHANGELOG.md | 1 + src/cli.rs | 1 + src/host/mod.rs | 2 +- src/main.rs | 55 ++++++++++++++++++++++++++++--------------------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a59d468..f5ee860 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ Released on ?? +- [issue 317](https://github.com/veeso/termscp/issues/317): the return value of `--version` should be `0` - [issue 319](https://github.com/veeso/termscp/issues/319): fixed a crash when the local directory specified in the auth form does not exist - [issue 327](https://github.com/veeso/termscp/issues/327): fixed a panic when trying to go up from local directory on localhost in the auth form - Dependencies: diff --git a/src/cli.rs b/src/cli.rs index 66b9d30..0aef632 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -17,6 +17,7 @@ pub enum Task { Activity(NextActivity), ImportTheme(PathBuf), InstallUpdate, + Version, } #[derive(Default, FromArgs)] diff --git a/src/host/mod.rs b/src/host/mod.rs index 579c0d6..f1a1560 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -44,7 +44,7 @@ pub enum HostErrorType { } /// HostError is a wrapper for the error type and the exact io error -#[derive(Debug)] +#[derive(Debug, Error)] pub struct HostError { pub error: HostErrorType, ioerr: Option, diff --git a/src/main.rs b/src/main.rs index 922c455..a7e9e70 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,24 +33,24 @@ const APP_NAME: &str = env!("CARGO_PKG_NAME"); const APP_BUILD_DATE: &str = env!("VERGEN_BUILD_TIMESTAMP"); const APP_GIT_BRANCH: &str = env!("VERGEN_GIT_BRANCH"); const APP_GIT_HASH: &str = env!("VERGEN_GIT_SHA"); -const EXIT_CODE_SUCCESS: i32 = 0; -const EXIT_CODE_ERROR: i32 = 1; const TERMSCP_VERSION: &str = env!("CARGO_PKG_VERSION"); const TERMSCP_AUTHORS: &str = env!("CARGO_PKG_AUTHORS"); +type MainResult = std::result::Result>; + #[inline] fn git_hash() -> &'static str { APP_GIT_HASH[0..8].as_ref() } -fn main() { +fn main() -> MainResult<()> { let args: Args = argh::from_env(); // Parse args let run_opts: RunOpts = match parse_args(args) { Ok(opts) => opts, Err(err) => { eprintln!("{err}"); - std::process::exit(255); + return Err(err.into()); } }; // Setup logging @@ -63,10 +63,7 @@ fn main() { ); // Run info!("Starting activity manager..."); - let rc = run(run_opts); - info!("termscp terminated with exitcode {}", rc); - // Then return - std::process::exit(rc); + run(run_opts) } /// Parse arguments @@ -81,10 +78,8 @@ fn parse_args(args: Args) -> Result { let mut run_opts: RunOpts = RunOpts::default(); // Version if args.version { - return Err(format!( - "{APP_NAME} v{TERMSCP_VERSION} ({APP_GIT_BRANCH}, {git_hash}, {APP_BUILD_DATE}) - Developed by {TERMSCP_AUTHORS}", - git_hash = git_hash() - )); + run_opts.task = Task::Version; + return Ok(run_opts); } // Logging if args.debug { @@ -125,57 +120,71 @@ fn parse_args(args: Args) -> Result { } /// Run task and return rc -fn run(run_opts: RunOpts) -> i32 { +fn run(run_opts: RunOpts) -> MainResult<()> { match run_opts.task { Task::ImportTheme(theme) => run_import_theme(&theme), Task::InstallUpdate => run_install_update(), Task::Activity(activity) => run_activity(activity, run_opts.ticks, run_opts.remote), + Task::Version => print_version(), } } -fn run_import_theme(theme: &Path) -> i32 { +fn print_version() -> MainResult<()> { + println!( + "{APP_NAME} v{TERMSCP_VERSION} ({APP_GIT_BRANCH}, {git_hash}, {APP_BUILD_DATE}) - Developed by {TERMSCP_AUTHORS}", + git_hash = git_hash() + ); + + Ok(()) +} + +fn run_import_theme(theme: &Path) -> MainResult<()> { match support::import_theme(theme) { Ok(_) => { println!("Theme has been successfully imported!"); - EXIT_CODE_ERROR + Ok(()) } Err(err) => { eprintln!("{err}"); - EXIT_CODE_ERROR + Err(err.into()) } } } -fn run_install_update() -> i32 { +fn run_install_update() -> MainResult<()> { match support::install_update() { Ok(msg) => { println!("{msg}"); - EXIT_CODE_SUCCESS + Ok(()) } Err(err) => { eprintln!("Could not install update: {err}"); - EXIT_CODE_ERROR + Err(err.into()) } } } -fn run_activity(activity: NextActivity, ticks: Duration, remote_args: RemoteArgs) -> i32 { +fn run_activity( + activity: NextActivity, + ticks: Duration, + remote_args: RemoteArgs, +) -> MainResult<()> { // Create activity manager (and context too) let mut manager: ActivityManager = match ActivityManager::new(ticks) { Ok(m) => m, Err(err) => { eprintln!("Could not start activity manager: {err}"); - return EXIT_CODE_ERROR; + return Err(err.into()); } }; // Set file transfer params if set if let Err(err) = manager.configure_remote_args(remote_args) { eprintln!("{err}"); - return EXIT_CODE_ERROR; + return Err(err.into()); } manager.run(activity); - EXIT_CODE_SUCCESS + Ok(()) }