forked from eclipse-iceoryx/iceoryx2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request eclipse-iceoryx#371 from orecham/iox2-98-add-servi…
…ces-list-command [eclipse-iceoryx#98] add `iox2 services` cli
- Loading branch information
Showing
15 changed files
with
564 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Apache Software License 2.0 which is available at | ||
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
// which is available at https://opensource.org/licenses/MIT. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use clap::Args; | ||
use clap::Parser; | ||
use clap::Subcommand; | ||
use clap::ValueEnum; | ||
|
||
use iceoryx2_cli_utils::help_template; | ||
|
||
use crate::Format; | ||
|
||
#[derive(Parser)] | ||
#[command( | ||
name = "iox2-services", | ||
about = "Query information about iceoryx2 services", | ||
long_about = None, | ||
version = env!("CARGO_PKG_VERSION"), | ||
disable_help_subcommand = true, | ||
arg_required_else_help = false, | ||
help_template = help_template("iox2-services", false), | ||
)] | ||
pub struct Cli { | ||
#[clap(subcommand)] | ||
pub action: Option<Action>, | ||
|
||
#[clap(long, short = 'f', value_enum, global = true)] | ||
pub format: Option<Format>, | ||
} | ||
|
||
#[derive(Debug, Clone, ValueEnum)] | ||
#[clap(rename_all = "PascalCase")] | ||
#[derive(Default)] | ||
pub enum MessagingPatternFilter { | ||
PublishSubscribe, | ||
Event, | ||
#[default] | ||
All, | ||
} | ||
|
||
#[derive(Debug, Clone, Args)] | ||
pub struct DetailsFilter { | ||
#[clap(short, long, value_enum, default_value_t = MessagingPatternFilter::All)] | ||
pub pattern: MessagingPatternFilter, | ||
} | ||
|
||
#[derive(Parser)] | ||
pub struct DetailsOptions { | ||
#[clap(help = "Name of the service e.g. \"My Service\"")] | ||
pub service: String, | ||
|
||
#[command(flatten)] | ||
pub filter: DetailsFilter, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
pub enum Action { | ||
#[clap(about = "List all existing services")] | ||
List, | ||
#[clap(about = "Show details of an existing service")] | ||
Details(DetailsOptions), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Apache Software License 2.0 which is available at | ||
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
// which is available at https://opensource.org/licenses/MIT. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use crate::cli::DetailsFilter; | ||
use crate::output::*; | ||
use anyhow::{Context, Error, Result}; | ||
use iceoryx2::prelude::*; | ||
use iceoryx2_cli_utils::Filter; | ||
use iceoryx2_cli_utils::Format; | ||
|
||
pub fn list(format: Format) -> Result<()> { | ||
let mut services = ServiceList::new(); | ||
|
||
ipc::Service::list(Config::global_config(), |service| { | ||
services.push(ServiceDescriptor::from(service)); | ||
CallbackProgression::Continue | ||
}) | ||
.context("failed to retrieve services")?; | ||
|
||
services.sort_by_key(|pattern| match pattern { | ||
ServiceDescriptor::PublishSubscribe(name) => (name.clone(), 0), | ||
ServiceDescriptor::Event(name) => (name.clone(), 1), | ||
ServiceDescriptor::Undefined(name) => (name.to_string(), 2), | ||
}); | ||
|
||
print!("{}", format.as_string(&services)?); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn details(service_name: String, filter: DetailsFilter, format: Format) -> Result<()> { | ||
let mut error: Option<Error> = None; | ||
|
||
ipc::Service::list(Config::global_config(), |service| { | ||
if service_name == service.static_details.name().to_string() { | ||
let description = ServiceDescription::from(&service); | ||
|
||
if filter.matches(&description) { | ||
match format.as_string(&description) { | ||
Ok(output) => { | ||
print!("{}", output); | ||
CallbackProgression::Continue | ||
} | ||
Err(e) => { | ||
error = Some(e); | ||
CallbackProgression::Stop | ||
} | ||
} | ||
} else { | ||
// Filter did not match | ||
CallbackProgression::Continue | ||
} | ||
} else { | ||
// Service name did not match | ||
CallbackProgression::Continue | ||
} | ||
})?; | ||
|
||
if let Some(err) = error { | ||
return Err(err); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) 2024 Contributors to the Eclipse Foundation | ||
// | ||
// See the NOTICE file(s) distributed with this work for additional | ||
// information regarding copyright ownership. | ||
// | ||
// This program and the accompanying materials are made available under the | ||
// terms of the Apache Software License 2.0 which is available at | ||
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license | ||
// which is available at https://opensource.org/licenses/MIT. | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
||
use crate::cli::DetailsFilter; | ||
use crate::cli::MessagingPatternFilter; | ||
use crate::output::ServiceDescription; | ||
use iceoryx2::service::static_config::messaging_pattern::MessagingPattern; | ||
use iceoryx2_cli_utils::Filter; | ||
|
||
impl Filter<ServiceDescription> for MessagingPatternFilter { | ||
fn matches(&self, description: &ServiceDescription) -> bool { | ||
matches!( | ||
(self, &description.pattern), | ||
( | ||
MessagingPatternFilter::PublishSubscribe, | ||
MessagingPattern::PublishSubscribe(_) | ||
) | (MessagingPatternFilter::Event, MessagingPattern::Event(_)) | ||
| (MessagingPatternFilter::All, _) | ||
) | ||
} | ||
} | ||
|
||
impl Filter<ServiceDescription> for DetailsFilter { | ||
fn matches(&self, description: &ServiceDescription) -> bool { | ||
self.pattern.matches(description) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.