batman_robin/cli/interface.rs
1use crate::Interface;
2
3use clap::{Arg, Command};
4
5/// Creates the CLI command for displaying or modifying batman-adv interfaces.
6///
7/// # Returns
8/// - A `clap::Command` configured with:
9/// - Name: `"interface"`
10/// - Alias: `"if"`
11/// - Short and long description: `"Display or modify the batman-adv interface settings."`
12/// - Usage override:
13/// ```text
14/// robctl [options] interface|if [options] [add|del iface(s)]
15/// robctl [options] interface|if [options] create [routing_algo|ra RA_NAME]
16/// robctl [options] interface|if [options] destroy
17/// ```
18/// - Optional flags and arguments:
19/// - `-M, --manual`: Disable automatic creation/destruction of batman-adv interface
20/// - `action`: Command name, one of `add`, `a`, `del`, `d`, `create`, `c`, `destroy`, `D`
21/// - `params`: Additional parameters, either interface names (for add/del) or routing algorithm name (for create)
22/// - Version flag disabled
23pub fn cmd_interfaces() -> Command {
24 Command::new("interface")
25 .alias("if")
26 .about("Display or modify the batman-adv interface settings.")
27 .long_about("Display or modify the batman-adv interface settings.")
28 .override_usage(
29 "\trobctl [options] interface|if [options] [add|del iface(s)]\n\
30 \trobctl [options] interface|if [options] create [routing_algo|ra RA_NAME]\n\
31 \trobctl [options] interface|if [options] destroy\n",
32 )
33 .arg(
34 Arg::new("manual")
35 .short('M')
36 .help("Disable automatic creation/destruction of batman-adv interface")
37 .action(clap::ArgAction::SetTrue),
38 )
39 .arg(
40 Arg::new("action")
41 .index(1)
42 .value_name("command")
43 .value_parser(["add", "a", "del", "d", "create", "c", "destroy", "D"])
44 .help("Command name:"),
45 )
46 .arg(
47 Arg::new("params")
48 .index(2)
49 .value_name("parameters")
50 .num_args(0..)
51 .help("Interfaces (add/del) or routing algorithm (create)"),
52 )
53 .disable_version_flag(true)
54}
55
56/// Prints the list of batman-adv interfaces with their current status.
57///
58/// # Arguments
59/// - `interfaces`: Slice of `Interface` structs, each containing:
60/// - `ifname`: Name of the interface
61/// - `active`: Boolean indicating whether the interface is active
62///
63/// # Behavior
64/// - Prints each interface in the format: `"iface_name: active"` or `"iface_name: inactive"`.
65pub fn print_interfaces(interfaces: &[Interface]) {
66 for iface in interfaces {
67 let status = if iface.active { "active" } else { "inactive" };
68
69 println!("{}: {}", iface.ifname, status);
70 }
71}