Skip to content

Commit

Permalink
Align tedge-apt-plugin with tedge services/plugins
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
didier-wenzek committed Jan 8, 2025
1 parent db8c920 commit c80b3d0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
12 changes: 8 additions & 4 deletions crates/core/tedge/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
}
Expand Down
20 changes: 15 additions & 5 deletions crates/core/tedge/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ static ALLOCATOR: Cap<alloc::System> = 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)) => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -131,6 +129,18 @@ where
Args: IntoIterator<Item = Arg>,
Arg: Into<OsString> + 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
Expand Down
11 changes: 1 addition & 10 deletions plugins/tedge_apt_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,16 +346,7 @@ fn get_config(config_dir: &Path) -> Option<TEdgeConfig> {
}
}

pub fn run_and_exit(cli: Result<AptCli, clap::Error>) -> ! {
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() {
Expand Down

0 comments on commit c80b3d0

Please sign in to comment.