Skip to content

Commit

Permalink
[eclipse-iceoryx#98] Implement iox2 nodes list
Browse files Browse the repository at this point in the history
  • Loading branch information
orecham committed Sep 19, 2024
1 parent 8dd9af0 commit 7489fe4
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [

"iceoryx2-cli/iox2",
"iceoryx2-cli/iox2-introspect",
"iceoryx2-cli/iox2-nodes",
"iceoryx2-cli/iox2-processes",
"iceoryx2-cli/iox2-pub",
"iceoryx2-cli/iox2-rpc",
Expand Down
22 changes: 22 additions & 0 deletions iceoryx2-cli/iox2-nodes/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "iox2-nodes"
description = "CLI for managing iceoryx2 nodes"
categories = { workspace = true }
edition = { workspace = true }
homepage = { workspace = true }
keywords = { workspace = true }
license = { workspace = true }
repository = { workspace = true }
rust-version = { workspace = true }
version = { workspace = true }

[dependencies]
iceoryx2 = { workspace = true }
iceoryx2-bb-log = { workspace = true }
iceoryx2-cli-utils = { workspace = true }

anyhow = { workspace = true }
better-panic = { workspace = true }
clap = { workspace = true }
human-panic = { workspace = true }
serde = { workspace = true }
49 changes: 49 additions & 0 deletions iceoryx2-cli/iox2-nodes/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// 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::Parser;
use clap::Subcommand;

use iceoryx2_cli_utils::help_template;
use iceoryx2_cli_utils::Format;

#[derive(Parser)]
#[command(
name = "iox2-nodes",
about = "Query information about iceoryx2 nodes",
long_about = None,
version = env!("CARGO_PKG_VERSION"),
disable_help_subcommand = true,
arg_required_else_help = false,
help_template = help_template("iox2-nodes", false),
)]
pub struct Cli {
#[clap(subcommand)]
pub action: Option<Action>,

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

#[derive(Parser)]
pub struct DetailsOptions {
#[clap(help = "")]
pub node: String,
}

#[derive(Subcommand)]
pub enum Action {
#[clap(about = "")]
List,
#[clap(about = "")]
Details(DetailsOptions),
}
36 changes: 36 additions & 0 deletions iceoryx2-cli/iox2-nodes/src/commands.rs
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 anyhow::{Context, Result};
use iceoryx2::prelude::*;
use iceoryx2_cli_utils::output::NodeDescriptor;
use iceoryx2_cli_utils::output::NodeList;
use iceoryx2_cli_utils::Format;

pub fn list(format: Format) -> Result<()> {
let mut nodes = Vec::<NodeDescriptor>::new();
Node::<ipc::Service>::list(Config::global_config(), |node_state| {
nodes.push(NodeDescriptor::from(&node_state));
CallbackProgression::Continue
})
.context("failed to retrieve nodes")?;

print!(
"{}",
format.as_string(&NodeList {
num: nodes.len(),
details: nodes
})?
);

Ok(())
}
62 changes: 62 additions & 0 deletions iceoryx2-cli/iox2-nodes/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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

mod cli;
mod commands;

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

#[cfg(not(debug_assertions))]
use human_panic::setup_panic;
#[cfg(debug_assertions)]
extern crate better_panic;

fn main() {
#[cfg(not(debug_assertions))]
{
setup_panic!();
}
#[cfg(debug_assertions)]
{
better_panic::Settings::debug()
.most_recent_first(false)
.lineno_suffix(true)
.verbosity(better_panic::Verbosity::Full)
.install();
}

set_log_level(LogLevel::Warn);

match Cli::try_parse() {
Ok(cli) => {
if let Some(action) = cli.action {
match action {
cli::Action::List => {
if let Err(e) = commands::list(cli.format.unwrap_or(Format::Ron)) {
eprintln!("Failed to list nodes: {}", e);
}
}
cli::Action::Details(_) => todo!(),
}
} else {
Cli::command().print_help().expect("Failed to print help");
}
}
Err(e) => {
eprintln!("{}", e);
}
}
}

0 comments on commit 7489fe4

Please sign in to comment.