From c80b3d03ef32287c66b62ffc48d47bc5b7b5431d Mon Sep 17 00:00:00 2001 From: Didier Wenzek Date: Wed, 8 Jan 2025 13:22:29 +0100 Subject: [PATCH] Align tedge-apt-plugin with tedge services/plugins tedge-apt-plugin specific treatment in tedge/main.rs was only for exiting with 1 and not 2 when the command line cannot be parsed. Signed-off-by: Didier Wenzek --- crates/core/tedge/src/cli/mod.rs | 12 ++++++++---- crates/core/tedge/src/main.rs | 20 +++++++++++++++----- plugins/tedge_apt_plugin/src/lib.rs | 11 +---------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/crates/core/tedge/src/cli/mod.rs b/crates/core/tedge/src/cli/mod.rs index 0abad56addc..32146c36de9 100644 --- a/crates/core/tedge/src/cli/mod.rs +++ b/crates/core/tedge/src/cli/mod.rs @@ -7,6 +7,7 @@ use c8y_firmware_plugin::FirmwarePluginOpt; use c8y_remote_access_plugin::C8yRemoteAccessPluginOpt; pub use connect::*; use tedge_agent::AgentOpt; +use tedge_apt_plugin::AptCli; use tedge_config::cli::CommonArgs; use tedge_mapper::MapperOpt; use tedge_watchdog::WatchdogOpt; @@ -51,15 +52,18 @@ pub enum TEdgeOptMulticall { #[derive(clap::Parser, Debug)] pub enum Component { - TedgeMapper(MapperOpt), + C8yFirmwarePlugin(FirmwarePluginOpt), + + C8yRemoteAccessPlugin(C8yRemoteAccessPluginOpt), TedgeAgent(AgentOpt), - C8yFirmwarePlugin(FirmwarePluginOpt), + #[clap(alias = "apt")] + TedgeAptPlugin(AptCli), - TedgeWatchdog(WatchdogOpt), + TedgeMapper(MapperOpt), - C8yRemoteAccessPlugin(C8yRemoteAccessPluginOpt), + TedgeWatchdog(WatchdogOpt), TedgeWrite(TedgeWriteOpt), } diff --git a/crates/core/tedge/src/main.rs b/crates/core/tedge/src/main.rs index 28ed9c44355..a36d7c1c67a 100644 --- a/crates/core/tedge/src/main.rs +++ b/crates/core/tedge/src/main.rs @@ -32,11 +32,6 @@ static ALLOCATOR: Cap = Cap::new(alloc::System, usize::MAX); fn main() -> anyhow::Result<()> { let executable_name = executable_name(); - if matches!(executable_name.as_deref(), Some("apt" | "tedge-apt-plugin")) { - let try_opt = AptCli::try_parse(); - tedge_apt_plugin::run_and_exit(try_opt); - } - let opt = parse_multicall(&executable_name, std::env::args_os()); match opt { TEdgeOptMulticall::Component(Component::TedgeMapper(opt)) => { @@ -64,6 +59,9 @@ fn main() -> anyhow::Result<()> { block_on(tedge_watchdog::run(opt)) } TEdgeOptMulticall::Component(Component::TedgeWrite(opt)) => tedge_write::bin::run(opt), + TEdgeOptMulticall::Component(Component::TedgeAptPlugin(opt)) => { + tedge_apt_plugin::run_and_exit(opt) + } TEdgeOptMulticall::Tedge { cmd, common } => { let tedge_config_location = tedge_config::TEdgeConfigLocation::from_custom_root(&common.config_dir); @@ -131,6 +129,18 @@ where Args: IntoIterator, Arg: Into + Clone, { + if matches!(executable_name.as_deref(), Some("apt" | "tedge-apt-plugin")) { + // the apt plugin must be treated apart + // as we want to exit 1 and not 2 when the command line cannot be parsed + match AptCli::try_parse() { + Ok(apt) => return TEdgeOptMulticall::Component(Component::TedgeAptPlugin(apt)), + Err(e) => { + eprintln!("{}", RichFormatter::format_error(&e)); + std::process::exit(1); + } + } + } + let cmd = TEdgeOptMulticall::command(); let is_known_subcommand = executable_name diff --git a/plugins/tedge_apt_plugin/src/lib.rs b/plugins/tedge_apt_plugin/src/lib.rs index 07282e97e20..9b4a4889a98 100644 --- a/plugins/tedge_apt_plugin/src/lib.rs +++ b/plugins/tedge_apt_plugin/src/lib.rs @@ -346,16 +346,7 @@ fn get_config(config_dir: &Path) -> Option { } } -pub fn run_and_exit(cli: Result) -> ! { - let mut apt = match cli { - Ok(aptcli) => aptcli, - Err(err) => { - err.print().expect("Failed to print help message"); - // re-write the clap exit_status from 2 to 1, if parse fails - std::process::exit(1) - } - }; - +pub fn run_and_exit(mut apt: AptCli) -> ! { if let PluginOp::List { name, maintainer } = &mut apt.operation { if let Some(config) = get_config(apt.common.config_dir.as_std_path()) { if name.is_none() {