Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ clap = { version = "4.5.40", features = ["cargo"] }
futures-util = "0.3.31"
indexmap = { version = "2.14.0", features = ["serde"] }
log = { version = "0.4.29", features = ["std"] }
nix = { version = "0.31.3", features = ["feature", "sched"] }
rtnetlink = { git = "https://github.com/rust-netlink/rtnetlink" }
serde = { version = "1.0", default-features = false, features = ["derive"] }
serde_json = "1.0.140"
Expand Down
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ impl From<&str> for CliError {
}
}

impl From<String> for CliError {
fn from(msg: String) -> Self {
Self {
code: DEFAULT_ERROR_CODE,
msg,
}
}
}

impl std::fmt::Display for CliError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "error {}: {}", self.code, self.msg)
Expand Down
17 changes: 16 additions & 1 deletion src/ip/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod address;
mod link;
mod neighbour;

#[cfg(test)]
mod tests;
Expand All @@ -10,6 +11,8 @@ use std::io::IsTerminal;

use iproute_rs::{CliColor, CliError, OutputFormat, print_result_and_exit};

use crate::neighbour::NeighbourCommand;

use self::{address::AddressCommand, link::LinkCommand};

#[tokio::main(flavor = "current_thread")]
Expand Down Expand Up @@ -55,9 +58,17 @@ async fn main() -> Result<(), CliError> {
.action(clap::ArgAction::SetTrue)
.global(true),
)
.arg(
clap::Arg::new("STATISTICS")
.short('s')
.help("Show object statistics")
.action(clap::ArgAction::SetTrue)
.global(true),
)
.subcommand_required(true)
.subcommand(LinkCommand::gen_command())
.subcommand(AddressCommand::gen_command());
.subcommand(AddressCommand::gen_command())
.subcommand(NeighbourCommand::gen_command());

let matches = app.get_matches_mut();

Expand All @@ -84,6 +95,10 @@ async fn main() -> Result<(), CliError> {
matches.subcommand_matches(AddressCommand::CMD)
{
print_result_and_exit(AddressCommand::handle(matches).await, fmt);
} else if let Some(matches) =
matches.subcommand_matches(NeighbourCommand::CMD)
{
print_result_and_exit(NeighbourCommand::handle(matches).await, fmt);
} else {
app.print_help()?;
println!();
Expand Down
50 changes: 50 additions & 0 deletions src/ip/neighbour/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MIT

use iproute_rs::CliError;

use super::show::{CliNeighbourInfo, handle_show};

pub(crate) struct NeighbourCommand;

impl NeighbourCommand {
pub(crate) const CMD: &'static str = "neighbour";

pub(crate) fn gen_command() -> clap::Command {
clap::Command::new(Self::CMD)
.about("arp/ndp table management")
.alias("neigh")
.alias("neig")
.alias("nei")
.alias("ne")
.alias("n")
.subcommand_required(false)
.subcommand(
clap::Command::new("show")
.about("list neighbour entries")
.alias("list")
.alias("lst")
.alias("ls")
.alias("li")
.alias("l")
.arg(
clap::Arg::new("options")
.action(clap::ArgAction::Append)
.trailing_var_arg(true),
),
)
}

pub(crate) async fn handle(
matches: &clap::ArgMatches,
) -> Result<Vec<CliNeighbourInfo>, CliError> {
if let Some(matches) = matches.subcommand_matches("show") {
let opts = matches
.get_many::<String>("options")
.unwrap_or_default()
.map(String::as_str);
handle_show(opts, matches.get_flag("STATISTICS")).await
} else {
handle_show([].into_iter(), matches.get_flag("STATISTICS")).await
}
}
}
9 changes: 9 additions & 0 deletions src/ip/neighbour/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT

mod cli;
mod show;

#[cfg(test)]
mod tests;

pub(crate) use self::cli::NeighbourCommand;
Loading