Skip to content

Commit c80b3d0

Browse files
committed
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 <didier.wenzek@free.fr>
1 parent db8c920 commit c80b3d0

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

crates/core/tedge/src/cli/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use c8y_firmware_plugin::FirmwarePluginOpt;
77
use c8y_remote_access_plugin::C8yRemoteAccessPluginOpt;
88
pub use connect::*;
99
use tedge_agent::AgentOpt;
10+
use tedge_apt_plugin::AptCli;
1011
use tedge_config::cli::CommonArgs;
1112
use tedge_mapper::MapperOpt;
1213
use tedge_watchdog::WatchdogOpt;
@@ -51,15 +52,18 @@ pub enum TEdgeOptMulticall {
5152

5253
#[derive(clap::Parser, Debug)]
5354
pub enum Component {
54-
TedgeMapper(MapperOpt),
55+
C8yFirmwarePlugin(FirmwarePluginOpt),
56+
57+
C8yRemoteAccessPlugin(C8yRemoteAccessPluginOpt),
5558

5659
TedgeAgent(AgentOpt),
5760

58-
C8yFirmwarePlugin(FirmwarePluginOpt),
61+
#[clap(alias = "apt")]
62+
TedgeAptPlugin(AptCli),
5963

60-
TedgeWatchdog(WatchdogOpt),
64+
TedgeMapper(MapperOpt),
6165

62-
C8yRemoteAccessPlugin(C8yRemoteAccessPluginOpt),
66+
TedgeWatchdog(WatchdogOpt),
6367

6468
TedgeWrite(TedgeWriteOpt),
6569
}

crates/core/tedge/src/main.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::MAX);
3232
fn main() -> anyhow::Result<()> {
3333
let executable_name = executable_name();
3434

35-
if matches!(executable_name.as_deref(), Some("apt" | "tedge-apt-plugin")) {
36-
let try_opt = AptCli::try_parse();
37-
tedge_apt_plugin::run_and_exit(try_opt);
38-
}
39-
4035
let opt = parse_multicall(&executable_name, std::env::args_os());
4136
match opt {
4237
TEdgeOptMulticall::Component(Component::TedgeMapper(opt)) => {
@@ -64,6 +59,9 @@ fn main() -> anyhow::Result<()> {
6459
block_on(tedge_watchdog::run(opt))
6560
}
6661
TEdgeOptMulticall::Component(Component::TedgeWrite(opt)) => tedge_write::bin::run(opt),
62+
TEdgeOptMulticall::Component(Component::TedgeAptPlugin(opt)) => {
63+
tedge_apt_plugin::run_and_exit(opt)
64+
}
6765
TEdgeOptMulticall::Tedge { cmd, common } => {
6866
let tedge_config_location =
6967
tedge_config::TEdgeConfigLocation::from_custom_root(&common.config_dir);
@@ -131,6 +129,18 @@ where
131129
Args: IntoIterator<Item = Arg>,
132130
Arg: Into<OsString> + Clone,
133131
{
132+
if matches!(executable_name.as_deref(), Some("apt" | "tedge-apt-plugin")) {
133+
// the apt plugin must be treated apart
134+
// as we want to exit 1 and not 2 when the command line cannot be parsed
135+
match AptCli::try_parse() {
136+
Ok(apt) => return TEdgeOptMulticall::Component(Component::TedgeAptPlugin(apt)),
137+
Err(e) => {
138+
eprintln!("{}", RichFormatter::format_error(&e));
139+
std::process::exit(1);
140+
}
141+
}
142+
}
143+
134144
let cmd = TEdgeOptMulticall::command();
135145

136146
let is_known_subcommand = executable_name

plugins/tedge_apt_plugin/src/lib.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,16 +346,7 @@ fn get_config(config_dir: &Path) -> Option<TEdgeConfig> {
346346
}
347347
}
348348

349-
pub fn run_and_exit(cli: Result<AptCli, clap::Error>) -> ! {
350-
let mut apt = match cli {
351-
Ok(aptcli) => aptcli,
352-
Err(err) => {
353-
err.print().expect("Failed to print help message");
354-
// re-write the clap exit_status from 2 to 1, if parse fails
355-
std::process::exit(1)
356-
}
357-
};
358-
349+
pub fn run_and_exit(mut apt: AptCli) -> ! {
359350
if let PluginOp::List { name, maintainer } = &mut apt.operation {
360351
if let Some(config) = get_config(apt.common.config_dir.as_std_path()) {
361352
if name.is_none() {

0 commit comments

Comments
 (0)