Skip to content

Commit

Permalink
[#98] Make CLIs consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Sep 15, 2024
1 parent 9cfc3e4 commit 0e109d3
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 100 deletions.
18 changes: 6 additions & 12 deletions iceoryx2-cli/iox2-nodes/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,36 +77,30 @@ pub enum StateFilter {
}

#[derive(Debug, Clone, Args)]
pub struct ListFilter {
pub struct OutputFilter {
#[clap(short, long, value_enum, default_value_t = StateFilter::All)]
pub state: StateFilter,
}

#[derive(Args)]
pub struct ListOptions {
#[command(flatten)]
pub filter: ListFilter,
}

#[derive(Debug, Clone, Args)]
pub struct DetailsFilter {
#[clap(short, long, value_enum, default_value_t = StateFilter::All)]
pub state: StateFilter,
pub filter: OutputFilter,
}

#[derive(Args)]
pub struct DetailsOptions {
#[clap(help = "Name, ID or PID")]
#[clap(help = "Name, ID or PID of the node")]
pub node: NodeIdentifier,

#[command(flatten)]
pub filter: DetailsFilter,
pub filter: OutputFilter,
}

#[derive(Subcommand)]
pub enum Action {
#[clap(about = "List all existing nodes")]
#[clap(about = "List all nodes")]
List(ListOptions),
#[clap(about = "Show details of an existing node")]
#[clap(about = "Show details of a node")]
Details(DetailsOptions),
}
7 changes: 3 additions & 4 deletions iceoryx2-cli/iox2-nodes/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ use iceoryx2_cli_utils::output::NodeList;
use iceoryx2_cli_utils::Filter;
use iceoryx2_cli_utils::Format;

use crate::cli::DetailsFilter;
use crate::cli::ListFilter;
use crate::cli::NodeIdentifier;
use crate::cli::OutputFilter;

pub fn list(filter: ListFilter, format: Format) -> Result<()> {
pub fn list(filter: OutputFilter, format: Format) -> Result<()> {
let mut nodes = Vec::<NodeDescriptor>::new();
Node::<ipc::Service>::list(Config::global_config(), |node| {
if filter.matches(&node) {
Expand All @@ -43,7 +42,7 @@ pub fn list(filter: ListFilter, format: Format) -> Result<()> {
Ok(())
}

pub fn details(identifier: NodeIdentifier, filter: DetailsFilter, format: Format) -> Result<()> {
pub fn details(identifier: NodeIdentifier, filter: OutputFilter, format: Format) -> Result<()> {
let mut error: Option<Error> = None;

Node::<ipc::Service>::list(Config::global_config(), |node| {
Expand Down
11 changes: 2 additions & 9 deletions iceoryx2-cli/iox2-nodes/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::cli::DetailsFilter;
use crate::cli::ListFilter;
use crate::cli::NodeIdentifier;
use crate::cli::OutputFilter;
use crate::cli::StateFilter;
use iceoryx2::node::NodeState;
use iceoryx2::node::NodeView;
Expand Down Expand Up @@ -65,13 +64,7 @@ impl Filter<NodeState<Service>> for StateFilter {
}
}

impl Filter<NodeState<Service>> for ListFilter {
fn matches(&self, node: &NodeState<Service>) -> bool {
self.state.matches(node)
}
}

impl Filter<NodeState<Service>> for DetailsFilter {
impl Filter<NodeState<Service>> for OutputFilter {
fn matches(&self, node: &NodeState<Service>) -> bool {
self.state.matches(node)
}
Expand Down
5 changes: 3 additions & 2 deletions iceoryx2-cli/iox2-nodes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ mod filter;

use clap::CommandFactory;
use clap::Parser;
use cli::Action;
use cli::Cli;
use iceoryx2_bb_log::{set_log_level, LogLevel};

Expand Down Expand Up @@ -44,12 +45,12 @@ fn main() {
Ok(cli) => {
if let Some(action) = cli.action {
match action {
cli::Action::List(options) => {
Action::List(options) => {
if let Err(e) = commands::list(options.filter, cli.format) {
eprintln!("Failed to list nodes: {}", e);
}
}
cli::Action::Details(options) => {
Action::Details(options) => {
if let Err(e) = commands::details(options.node, options.filter, cli.format)
{
eprintln!("Failed to retrieve node details: {}", e);
Expand Down
20 changes: 13 additions & 7 deletions iceoryx2-cli/iox2-services/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub struct Cli {
#[clap(subcommand)]
pub action: Option<Action>,

#[clap(long, short = 'f', value_enum, global = true)]
pub format: Option<Format>,
#[clap(long, short = 'f', value_enum, global = true, value_enum, default_value_t = Format::Ron)]
pub format: Format,
}

#[derive(Debug, Clone, ValueEnum)]
Expand All @@ -47,24 +47,30 @@ pub enum MessagingPatternFilter {
}

#[derive(Debug, Clone, Args)]
pub struct DetailsFilter {
pub struct OutputFilter {
#[clap(short, long, value_enum, default_value_t = MessagingPatternFilter::All)]
pub pattern: MessagingPatternFilter,
}

#[derive(Args)]
pub struct ListOptions {
#[command(flatten)]
pub filter: OutputFilter,
}

#[derive(Parser)]
pub struct DetailsOptions {
#[clap(help = "Name of the service e.g. \"My Service\"")]
pub service: String,

#[command(flatten)]
pub filter: DetailsFilter,
pub filter: OutputFilter,
}

#[derive(Subcommand)]
pub enum Action {
#[clap(about = "List all existing services")]
List,
#[clap(about = "Show details of an existing service")]
#[clap(about = "List all services")]
List(ListOptions),
#[clap(about = "Show service details")]
Details(DetailsOptions),
}
36 changes: 15 additions & 21 deletions iceoryx2-cli/iox2-services/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ use iceoryx2_cli_utils::output::ServiceList;
use iceoryx2_cli_utils::Filter;
use iceoryx2_cli_utils::Format;

use crate::cli::DetailsFilter;
use crate::cli::OutputFilter;

pub fn list(format: Format) -> Result<()> {
pub fn list(filter: OutputFilter, format: Format) -> Result<()> {
let mut services = ServiceList::new();

ipc::Service::list(Config::global_config(), |service| {
services.push(ServiceDescriptor::from(service));
if filter.matches(&service) {
services.push(ServiceDescriptor::from(service));
}
CallbackProgression::Continue
})
.context("failed to retrieve services")?;
Expand All @@ -40,30 +42,22 @@ pub fn list(format: Format) -> Result<()> {
Ok(())
}

pub fn details(service_name: String, filter: DetailsFilter, format: Format) -> Result<()> {
pub fn details(service_name: String, filter: OutputFilter, 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
}
if service_name == service.static_details.name().to_string() && filter.matches(&service) {
match format.as_string(&ServiceDescription::from(&service)) {
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
}
})?;
Expand Down
17 changes: 9 additions & 8 deletions iceoryx2-cli/iox2-services/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

use crate::cli::DetailsFilter;
use crate::cli::MessagingPatternFilter;
use crate::cli::OutputFilter;
use iceoryx2::service::ipc::Service;
use iceoryx2::service::static_config::messaging_pattern::MessagingPattern;
use iceoryx2_cli_utils::output::ServiceDescription;
use iceoryx2::service::ServiceDetails;
use iceoryx2_cli_utils::Filter;

impl Filter<ServiceDescription> for MessagingPatternFilter {
fn matches(&self, description: &ServiceDescription) -> bool {
impl Filter<ServiceDetails<Service>> for MessagingPatternFilter {
fn matches(&self, service: &ServiceDetails<Service>) -> bool {
matches!(
(self, &description.pattern),
(self, &service.static_details.messaging_pattern()),
(
MessagingPatternFilter::PublishSubscribe,
MessagingPattern::PublishSubscribe(_)
Expand All @@ -29,8 +30,8 @@ impl Filter<ServiceDescription> for MessagingPatternFilter {
}
}

impl Filter<ServiceDescription> for DetailsFilter {
fn matches(&self, description: &ServiceDescription) -> bool {
self.pattern.matches(description)
impl Filter<ServiceDetails<Service>> for OutputFilter {
fn matches(&self, service: &ServiceDetails<Service>) -> bool {
self.pattern.matches(service)
}
}
13 changes: 5 additions & 8 deletions iceoryx2-cli/iox2-services/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use clap::Parser;
use cli::Action;
use cli::Cli;
use iceoryx2_bb_log::{set_log_level, LogLevel};
use iceoryx2_cli_utils::Format;

fn main() {
#[cfg(not(debug_assertions))]
Expand All @@ -46,17 +45,15 @@ fn main() {
Ok(cli) => {
if let Some(action) = cli.action {
match action {
Action::List => {
if let Err(e) = commands::list(cli.format.unwrap_or(Format::Ron)) {
Action::List(options) => {
if let Err(e) = commands::list(options.filter, cli.format) {
eprintln!("Failed to list services: {}", e);
}
}
Action::Details(options) => {
if let Err(e) = commands::details(
options.service,
options.filter,
cli.format.unwrap_or(Format::Ron),
) {
if let Err(e) =
commands::details(options.service, options.filter, cli.format)
{
eprintln!("Failed to retrieve service details: {}", e);
}
}
Expand Down
7 changes: 0 additions & 7 deletions iceoryx2-cli/iox2/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ pub struct Cli {
)]
pub paths: bool,

#[arg(
short,
long,
help = "Specify to execute development versions of commands if they exist"
)]
pub dev: bool,

#[arg(
hide = true,
required = false,
Expand Down
48 changes: 26 additions & 22 deletions iceoryx2-cli/iox2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod commands;

use clap::CommandFactory;
use clap::Parser;
use cli::Cli;

fn main() {
#[cfg(not(debug_assertions))]
Expand All @@ -36,29 +37,32 @@ fn main() {
.install();
}

let cli = cli::Cli::parse();

if cli.list {
if let Err(e) = commands::list() {
eprintln!("Failed to list commands: {}", e);
}
} else if cli.paths {
if let Err(e) = commands::paths() {
eprintln!("Failed to list search paths: {}", e);
match Cli::try_parse() {
Ok(cli) => {
if cli.list {
if let Err(e) = commands::list() {
eprintln!("Failed to list commands: {}", e);
}
} else if cli.paths {
if let Err(e) = commands::paths() {
eprintln!("Failed to list search paths: {}", e);
}
} else if !cli.external_command.is_empty() {
let command_name = &cli.external_command[0];
let command_args = if cli.external_command.len() > 1 {
Some(&cli.external_command[1..])
} else {
None
};
if let Err(e) = commands::execute(command_name, command_args) {
eprintln!("Failed to execute command: {}", e);
}
} else {
Cli::command().print_help().expect("Failed to print help");
}
}
} else if !cli.external_command.is_empty() {
let command_name = &cli.external_command[0];
let command_args = if cli.external_command.len() > 1 {
Some(&cli.external_command[1..])
} else {
None
};
if let Err(e) = commands::execute(command_name, command_args) {
eprintln!("Failed to execute command: {}", e);
Err(e) => {
eprintln!("{}", e);
}
} else {
cli::Cli::command()
.print_help()
.expect("Unrecognized CLI input. Exiting.");
}
}

0 comments on commit 0e109d3

Please sign in to comment.