Skip to content

Commit d9dd585

Browse files
committed
Add ip neigh show
1 parent 21f5d8c commit d9dd585

4 files changed

Lines changed: 499 additions & 1 deletion

File tree

src/ip/main.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
mod address;
44
mod link;
5+
mod neighbour;
56

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

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

14+
use crate::neighbour::NeighbourCommand;
15+
1316
use self::{address::AddressCommand, link::LinkCommand};
1417

1518
#[tokio::main(flavor = "current_thread")]
@@ -55,9 +58,17 @@ async fn main() -> Result<(), CliError> {
5558
.action(clap::ArgAction::SetTrue)
5659
.global(true),
5760
)
61+
.arg(
62+
clap::Arg::new("STATISTICS")
63+
.short('s')
64+
.help("Show object statistics")
65+
.action(clap::ArgAction::SetTrue)
66+
.global(true),
67+
)
5868
.subcommand_required(true)
5969
.subcommand(LinkCommand::gen_command())
60-
.subcommand(AddressCommand::gen_command());
70+
.subcommand(AddressCommand::gen_command())
71+
.subcommand(NeighbourCommand::gen_command());
6172

6273
let matches = app.get_matches_mut();
6374

@@ -84,6 +95,10 @@ async fn main() -> Result<(), CliError> {
8495
matches.subcommand_matches(AddressCommand::CMD)
8596
{
8697
print_result_and_exit(AddressCommand::handle(matches).await, fmt);
98+
} else if let Some(matches) =
99+
matches.subcommand_matches(NeighbourCommand::CMD)
100+
{
101+
print_result_and_exit(NeighbourCommand::handle(matches).await, fmt);
87102
} else {
88103
app.print_help()?;
89104
println!();

src/ip/neighbour/cli.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
use iproute_rs::CliError;
4+
5+
use super::show::{CliNeighbourInfo, handle_show};
6+
7+
pub(crate) struct NeighbourCommand;
8+
9+
impl NeighbourCommand {
10+
pub(crate) const CMD: &'static str = "neighbour";
11+
12+
pub(crate) fn gen_command() -> clap::Command {
13+
clap::Command::new(Self::CMD)
14+
.about("arp/ndp table management")
15+
.alias("neigh")
16+
.alias("neig")
17+
.alias("nei")
18+
.alias("ne")
19+
.alias("n")
20+
.subcommand_required(false)
21+
.subcommand(
22+
clap::Command::new("show")
23+
.about("list neighbour entries")
24+
.alias("list")
25+
.alias("lst")
26+
.alias("ls")
27+
.alias("li")
28+
.alias("l")
29+
.arg(
30+
clap::Arg::new("options")
31+
.action(clap::ArgAction::Append)
32+
.trailing_var_arg(true),
33+
),
34+
)
35+
}
36+
37+
pub(crate) async fn handle(
38+
matches: &clap::ArgMatches,
39+
) -> Result<Vec<CliNeighbourInfo>, CliError> {
40+
if let Some(matches) = matches.subcommand_matches("show") {
41+
let opts = matches
42+
.get_many::<String>("options")
43+
.unwrap_or_default()
44+
.map(String::as_str);
45+
handle_show(opts, matches.get_flag("STATISTICS")).await
46+
} else {
47+
handle_show([].into_iter(), matches.get_flag("STATISTICS")).await
48+
}
49+
}
50+
}

src/ip/neighbour/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
mod cli;
4+
mod show;
5+
6+
#[cfg(test)]
7+
mod tests;
8+
9+
pub(crate) use self::cli::NeighbourCommand;

0 commit comments

Comments
 (0)