1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use clap::{Parser, Subcommand};
use std::path::PathBuf;
/// Profile applied when `apply` is invoked without a profile name.
const DEFAULT_APPLY_PROFILE: &str = "lum";
#[derive(Parser)]
#[command(
name = "vlanctl",
about = "Apply named VLAN profiles (macOS and Linux)"
)]
pub struct Cli {
/// Directory containing profile .toml files.
#[arg(long, default_value = "profiles", global = true)]
pub profiles_dir: PathBuf,
#[command(subcommand)]
pub command: Command,
}
#[derive(Subcommand)]
pub enum Command {
/// List available profiles.
List,
/// Print the commands a profile would run.
Show {
profile: String,
/// Parent device to attach VLANs to, overriding the profile's
/// `device` field. The parent is host-local — macOS numbers adapters
/// `enN` per machine and Linux uses `eth0`/`enp*`/`enx*` — so it
/// belongs on the command line, not in a committed profile.
#[arg(long)]
device: Option<String>,
},
/// Validate a profile without applying it.
Validate { profile: String },
/// Bring up a profile (requires root). Defaults to the `lum` profile.
Apply {
/// Profile to apply (defaults to "lum").
#[arg(default_value = DEFAULT_APPLY_PROFILE)]
profile: String,
/// Print commands without executing them.
#[arg(long)]
dry_run: bool,
/// Parent device to attach VLANs to, overriding the profile's
/// `device` field. See `show --device`.
#[arg(long)]
device: Option<String>,
},
/// Tear down the active profile (requires root).
Down {
/// Print commands without executing them.
#[arg(long)]
dry_run: bool,
},
/// Show what is currently up.
Status,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn apply_without_profile_defaults_to_lum() {
let cli = Cli::try_parse_from(["vlanctl", "apply"]).unwrap();
match cli.command {
Command::Apply {
profile, dry_run, ..
} => {
assert_eq!(profile, "lum");
assert!(!dry_run);
}
_ => panic!("expected Apply"),
}
}
#[test]
fn apply_accepts_a_device_override_and_defaults_to_none() {
// The parent device is host-local: macOS `enN` numbering is per
// machine and Linux uses `eth0`/`enp*` entirely, so it cannot live
// in a committed profile. `--device` makes it a runtime argument.
let cli = Cli::try_parse_from(["vlanctl", "apply", "lum", "--device", "eth0"]).unwrap();
match cli.command {
Command::Apply { device, .. } => assert_eq!(device.as_deref(), Some("eth0")),
_ => panic!("expected Apply"),
}
let cli = Cli::try_parse_from(["vlanctl", "apply"]).unwrap();
match cli.command {
Command::Apply { device, .. } => assert_eq!(device, None),
_ => panic!("expected Apply"),
}
}
#[test]
fn show_accepts_a_device_override() {
let cli =
Cli::try_parse_from(["vlanctl", "show", "halo", "--device", "enp0s31f6"]).unwrap();
match cli.command {
Command::Show { device, .. } => assert_eq!(device.as_deref(), Some("enp0s31f6")),
_ => panic!("expected Show"),
}
}
#[test]
fn apply_with_profile_overrides_default() {
let cli = Cli::try_parse_from(["vlanctl", "apply", "lum_legacy", "--dry-run"]).unwrap();
match cli.command {
Command::Apply {
profile, dry_run, ..
} => {
assert_eq!(profile, "lum_legacy");
assert!(dry_run);
}
_ => panic!("expected Apply"),
}
}
}