cleaning up
This commit is contained in:
+11
-20
@@ -24,8 +24,8 @@ pub struct Cli {
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum Command {
|
||||
/// Show device status rows from neighbor/device data.
|
||||
Status(StatusArgs),
|
||||
/// Show merged device inventory rows.
|
||||
Inventory(InventoryArgs),
|
||||
/// Show DHCP leases, optionally enriched with current neighbor state.
|
||||
Leases(LeasesArgs),
|
||||
/// Send Wake-on-LAN packets from a query or explicit MAC/IP pair.
|
||||
@@ -70,12 +70,12 @@ pub struct WakeArgs {
|
||||
|
||||
#[derive(Args)]
|
||||
#[command(after_long_help = "Examples:
|
||||
wakey status bedroom-pc
|
||||
wakey status --mac aa:bb:cc:dd:ee:ff
|
||||
wakey status --dev br-lan --nud reachable
|
||||
wakey inventory bedroom-pc
|
||||
wakey inventory --mac aa:bb:cc:dd:ee:ff
|
||||
wakey inventory --dev br-lan --nud reachable
|
||||
|
||||
If only the positional query is provided, it is treated as free-form input and resolved through the smart selector path.")]
|
||||
pub struct StatusArgs {
|
||||
pub struct InventoryArgs {
|
||||
/// Free-form device query.
|
||||
pub query: Option<String>,
|
||||
/// Explicit name/text filter.
|
||||
@@ -138,21 +138,12 @@ pub async fn run(cli: Cli) -> Result<()> {
|
||||
init_tracing(cli.verbose);
|
||||
|
||||
match cli.command {
|
||||
Command::Status(args) => {
|
||||
Command::Inventory(args) => {
|
||||
let as_json = args.json;
|
||||
let query = status_args_to_query(args);
|
||||
let query = inventory_args_to_query(args);
|
||||
let selected_name = query.name.clone();
|
||||
debug!(?query, json = as_json, "dispatching status command");
|
||||
let status = if query.name.is_some()
|
||||
&& query.filter.ips.is_empty()
|
||||
&& query.filter.devs.is_empty()
|
||||
&& query.filter.nuds.is_empty()
|
||||
&& query.filter.macs.is_empty()
|
||||
{
|
||||
wakey::get_status_for_input(query.name.clone().unwrap_or_default()).await?
|
||||
} else {
|
||||
wakey::get_status(query).await?
|
||||
};
|
||||
debug!(?query, json = as_json, "dispatching inventory command");
|
||||
let status = wakey::inventory(query).await?;
|
||||
if as_json {
|
||||
println!("{}", serde_json::to_string_pretty(&status)?);
|
||||
} else {
|
||||
@@ -216,7 +207,7 @@ pub async fn run(cli: Cli) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn status_args_to_query(args: StatusArgs) -> DeviceQuery {
|
||||
fn inventory_args_to_query(args: InventoryArgs) -> DeviceQuery {
|
||||
if let Some(query) = args.query.as_ref()
|
||||
&& args.name.is_none()
|
||||
&& args.ips.is_empty()
|
||||
|
||||
+36
-25
@@ -5,31 +5,42 @@ use wakey_core::{DeviceInventory, DhcpLeaseWithState, InterfaceSummary, WakeResu
|
||||
pub fn render_status_table(status: &DeviceInventory) -> Table {
|
||||
let mut table = base_table();
|
||||
table.set_header(vec!["Name", "IP", "MAC", "Presence", "Interfaces"]);
|
||||
for row in &status.devices {
|
||||
table.add_row(vec![
|
||||
Cell::new(
|
||||
row.names
|
||||
.first()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "(unnamed)".into()),
|
||||
),
|
||||
Cell::new(
|
||||
row.ips
|
||||
.iter()
|
||||
.map(ToString::to_string)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", "),
|
||||
),
|
||||
Cell::new(
|
||||
row.macs
|
||||
.iter()
|
||||
.map(ToString::to_string)
|
||||
.collect::<Vec<_>>()
|
||||
.join(", "),
|
||||
),
|
||||
Cell::new(format!("{:?}", row.presence)),
|
||||
Cell::new(row.interfaces.join(", ")),
|
||||
]);
|
||||
for device in &status.devices {
|
||||
let name = device
|
||||
.names
|
||||
.first()
|
||||
.cloned()
|
||||
.unwrap_or_else(|| "(unnamed)".into());
|
||||
let ips: Vec<String> = if device.ips.is_empty() {
|
||||
vec![String::new()]
|
||||
} else {
|
||||
device.ips.iter().map(ToString::to_string).collect()
|
||||
};
|
||||
let macs: Vec<String> = if device.macs.is_empty() {
|
||||
vec![String::new()]
|
||||
} else {
|
||||
device.macs.iter().map(ToString::to_string).collect()
|
||||
};
|
||||
let interfaces: Vec<String> = if device.interfaces.is_empty() {
|
||||
vec![String::new()]
|
||||
} else {
|
||||
device.interfaces.clone()
|
||||
};
|
||||
|
||||
let row_count = ips.len().max(macs.len()).max(interfaces.len());
|
||||
for idx in 0..row_count {
|
||||
table.add_row(vec![
|
||||
Cell::new(if idx == 0 { name.as_str() } else { "" }),
|
||||
Cell::new(ips.get(idx).cloned().unwrap_or_default()),
|
||||
Cell::new(macs.get(idx).cloned().unwrap_or_default()),
|
||||
Cell::new(if idx == 0 {
|
||||
format!("{:?}", device.presence)
|
||||
} else {
|
||||
String::new()
|
||||
}),
|
||||
Cell::new(interfaces.get(idx).cloned().unwrap_or_default()),
|
||||
]);
|
||||
}
|
||||
}
|
||||
table
|
||||
}
|
||||
|
||||
+4
-4
@@ -2,10 +2,10 @@ pub mod service;
|
||||
pub mod utils;
|
||||
|
||||
pub use service::{
|
||||
StatusResponse, broadcast_wake_targets, get_interface_summaries, get_interface_summary,
|
||||
get_ips, get_leases, get_status, get_status_for_input, inventory, leases_without_state,
|
||||
list_interfaces, merge_devices, query_to_device_query, resolve_devices, resolve_query,
|
||||
resolve_selector, resolve_wake_targets, wake_explicit, wake_from_query, wake_targets,
|
||||
broadcast_wake_targets, get_interface_summaries, get_interface_summary, get_ips, get_leases,
|
||||
inventory, leases_without_state, list_interfaces, merge_devices, query_to_device_query,
|
||||
resolve_devices, resolve_query, resolve_selector, resolve_wake_targets, wake_explicit,
|
||||
wake_from_query, wake_targets,
|
||||
};
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -2,14 +2,12 @@ pub mod interfaces;
|
||||
pub mod inventory;
|
||||
pub mod leases;
|
||||
pub mod query;
|
||||
pub mod status;
|
||||
pub mod wake;
|
||||
|
||||
pub use interfaces::{get_interface_summaries, get_interface_summary, get_ips, list_interfaces};
|
||||
pub use inventory::{inventory, merge_devices, resolve_devices};
|
||||
pub use leases::{get_leases, leases_without_state};
|
||||
pub use query::{query_to_device_query, resolve_query, resolve_selector};
|
||||
pub use status::{StatusResponse, get_status, get_status_for_input};
|
||||
pub use wake::{
|
||||
broadcast_wake_targets, resolve_wake_targets, wake_explicit, wake_from_query, wake_targets,
|
||||
};
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
use anyhow::Result;
|
||||
use tracing::{debug, instrument};
|
||||
use wakey_core::{DeviceInventory, DeviceQuery};
|
||||
|
||||
use crate::service::inventory::inventory;
|
||||
use crate::service::query::resolve_query;
|
||||
|
||||
/// Service status payload expressed in terms of merged device inventory.
|
||||
pub type StatusResponse = DeviceInventory;
|
||||
|
||||
/// Return status payload from merged device inventory.
|
||||
#[instrument(skip_all, fields(name = ?query.name))]
|
||||
pub async fn get_status(query: DeviceQuery) -> Result<StatusResponse> {
|
||||
let inventory = inventory(query).await?;
|
||||
debug!(devices = inventory.devices.len(), "built status response");
|
||||
Ok(inventory)
|
||||
}
|
||||
|
||||
/// Convenience wrapper around [`get_status`] for free-form user input.
|
||||
#[instrument(skip_all)]
|
||||
pub async fn get_status_for_input(input: impl Into<String>) -> Result<StatusResponse> {
|
||||
let query = resolve_query(input).await?;
|
||||
get_status(query).await
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::net::IpAddr;
|
||||
|
||||
use wakey::{broadcast_wake_targets, get_interface_summaries, get_status, get_status_for_input};
|
||||
use wakey::{broadcast_wake_targets, get_interface_summaries, inventory, resolve_query};
|
||||
use wakey_core::{DeviceFilters, DeviceQuery};
|
||||
|
||||
#[tokio::test]
|
||||
@@ -17,22 +17,22 @@ async fn interfaces_real_router_prints_interface_summaries() -> anyhow::Result<(
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "runs against live router data; use on-device or via scripts/test_remote.ps1"]
|
||||
async fn status_real_router_default_query_returns_rows_or_empty_cleanly() -> anyhow::Result<()> {
|
||||
let status = get_status(DeviceQuery::default()).await?;
|
||||
println!("{}", serde_json::to_string_pretty(&status)?);
|
||||
async fn inventory_real_router_default_query_returns_rows_or_empty_cleanly() -> anyhow::Result<()> {
|
||||
let inv = inventory(DeviceQuery::default()).await?;
|
||||
println!("{}", serde_json::to_string_pretty(&inv)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "runs against live router data; use on-device or via scripts/test_remote.ps1"]
|
||||
async fn status_real_router_for_interface_filter_succeeds() -> anyhow::Result<()> {
|
||||
async fn inventory_real_router_for_interface_filter_succeeds() -> anyhow::Result<()> {
|
||||
let interfaces = get_interface_summaries().await?;
|
||||
let first = interfaces
|
||||
.first()
|
||||
.map(|iface| iface.ifname.clone())
|
||||
.expect("expected at least one interface");
|
||||
|
||||
let status = get_status(DeviceQuery {
|
||||
let inv = inventory(DeviceQuery {
|
||||
name: None,
|
||||
filter: DeviceFilters {
|
||||
devs: vec![first.clone()],
|
||||
@@ -42,22 +42,22 @@ async fn status_real_router_for_interface_filter_succeeds() -> anyhow::Result<()
|
||||
.await?;
|
||||
|
||||
println!("filtered dev: {first}");
|
||||
println!("{}", serde_json::to_string_pretty(&status)?);
|
||||
println!("{}", serde_json::to_string_pretty(&inv)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "runs against live router data; use on-device or via scripts/test_remote.ps1"]
|
||||
async fn status_real_router_string_input_for_interface_succeeds() -> anyhow::Result<()> {
|
||||
async fn inventory_real_router_string_input_for_interface_succeeds() -> anyhow::Result<()> {
|
||||
let interfaces = get_interface_summaries().await?;
|
||||
let first = interfaces
|
||||
.first()
|
||||
.map(|iface| iface.ifname.clone())
|
||||
.expect("expected at least one interface");
|
||||
|
||||
let status = get_status_for_input(first.clone()).await?;
|
||||
let inv = inventory(resolve_query(first.clone()).await?).await?;
|
||||
println!("selector: {first}");
|
||||
println!("{}", serde_json::to_string_pretty(&status)?);
|
||||
println!("{}", serde_json::to_string_pretty(&inv)?);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
+1
-14
@@ -39,7 +39,7 @@ export type AuditEvent = {
|
||||
metadata: Record<string, unknown>;
|
||||
};
|
||||
|
||||
export type CommandKind = "status" | "devs" | "leases" | "inventory" | "wake";
|
||||
export type CommandKind = "devs" | "leases" | "inventory" | "wake";
|
||||
|
||||
export type EnrollTokenStatus = {
|
||||
enroll_token: string;
|
||||
@@ -127,19 +127,6 @@ function buildCommandPayload(kind: CommandKind, query: string): { command: Recor
|
||||
if (kind === "devs") {
|
||||
return { command: { kind: "devs", dev: null, up_only: false } };
|
||||
}
|
||||
if (kind === "status") {
|
||||
return {
|
||||
command: {
|
||||
kind: "status",
|
||||
query: query || null,
|
||||
name: null,
|
||||
ips: [],
|
||||
devs: [],
|
||||
nuds: [],
|
||||
macs: [],
|
||||
},
|
||||
};
|
||||
}
|
||||
if (kind === "leases") {
|
||||
return { command: { kind: "leases", include_state: true } };
|
||||
}
|
||||
|
||||
@@ -50,7 +50,6 @@ export function CommandsPage({ agents, selectedAgentId, onSelectAgent, onAfterCo
|
||||
Command
|
||||
<select value={kind} onChange={(e) => setKind(e.target.value as CommandKind)}>
|
||||
<option value="devs">devs</option>
|
||||
<option value="status">status</option>
|
||||
<option value="leases">leases</option>
|
||||
<option value="inventory">inventory</option>
|
||||
<option value="wake">wake</option>
|
||||
|
||||
@@ -2,8 +2,7 @@ use anyhow::Result;
|
||||
use tracing::{debug, info, instrument};
|
||||
|
||||
use crate::protocol::{
|
||||
AgentCommand, CommandResult, DevsRequest, InventoryRequest, LeasesRequest, StatusRequest,
|
||||
WakeRequest,
|
||||
AgentCommand, CommandResult, DevsRequest, InventoryRequest, LeasesRequest, WakeRequest,
|
||||
};
|
||||
|
||||
#[instrument(skip_all)]
|
||||
@@ -11,7 +10,6 @@ pub async fn dispatch_command(command: AgentCommand) -> Result<CommandResult> {
|
||||
let kind = command_kind(&command);
|
||||
info!(command = %kind, "dispatching command into local wakey services");
|
||||
match command {
|
||||
AgentCommand::Status(req) => dispatch_status(req).await,
|
||||
AgentCommand::Leases(req) => dispatch_leases(req).await,
|
||||
AgentCommand::Devs(req) => dispatch_devs(req).await,
|
||||
AgentCommand::Inventory(req) => dispatch_inventory(req).await,
|
||||
@@ -19,22 +17,6 @@ pub async fn dispatch_command(command: AgentCommand) -> Result<CommandResult> {
|
||||
}
|
||||
}
|
||||
|
||||
async fn dispatch_status(req: StatusRequest) -> Result<CommandResult> {
|
||||
let query = req.into_device_query();
|
||||
let status = if query.name.is_some()
|
||||
&& query.filter.ips.is_empty()
|
||||
&& query.filter.devs.is_empty()
|
||||
&& query.filter.nuds.is_empty()
|
||||
&& query.filter.macs.is_empty()
|
||||
{
|
||||
wakey::get_status_for_input(query.name.clone().unwrap_or_default()).await?
|
||||
} else {
|
||||
wakey::get_status(query).await?
|
||||
};
|
||||
debug!(devices = status.devices.len(), "dispatched status command");
|
||||
Ok(CommandResult::Status(status))
|
||||
}
|
||||
|
||||
async fn dispatch_leases(req: LeasesRequest) -> Result<CommandResult> {
|
||||
let leases = wakey::get_leases(wakey_core::LeaseQuery {
|
||||
include_state: req.include_state,
|
||||
@@ -90,7 +72,6 @@ async fn dispatch_wake(req: WakeRequest) -> Result<CommandResult> {
|
||||
|
||||
fn command_kind(command: &AgentCommand) -> &'static str {
|
||||
match command {
|
||||
AgentCommand::Status(_) => "status",
|
||||
AgentCommand::Leases(_) => "leases",
|
||||
AgentCommand::Devs(_) => "devs",
|
||||
AgentCommand::Inventory(_) => "inventory",
|
||||
|
||||
+35
-53
@@ -66,48 +66,6 @@ pub struct ErrorPayload {
|
||||
pub retryable: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct StatusRequest {
|
||||
pub query: Option<String>,
|
||||
pub name: Option<String>,
|
||||
#[serde(default)]
|
||||
pub ips: Vec<IpAddr>,
|
||||
#[serde(default)]
|
||||
pub devs: Vec<String>,
|
||||
#[serde(default)]
|
||||
pub nuds: Vec<wakey_core::NeighborState>,
|
||||
#[serde(default)]
|
||||
#[serde(with = "mac::vec_mac")]
|
||||
pub macs: Vec<MacAddr>,
|
||||
}
|
||||
|
||||
impl StatusRequest {
|
||||
pub fn into_device_query(self) -> DeviceQuery {
|
||||
if let Some(query) = self.query.as_ref()
|
||||
&& self.name.is_none()
|
||||
&& self.ips.is_empty()
|
||||
&& self.devs.is_empty()
|
||||
&& self.nuds.is_empty()
|
||||
&& self.macs.is_empty()
|
||||
{
|
||||
return DeviceQuery {
|
||||
name: Some(query.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
DeviceQuery {
|
||||
name: self.name.or(self.query),
|
||||
filter: DeviceFilters {
|
||||
ips: self.ips,
|
||||
devs: self.devs,
|
||||
nuds: self.nuds,
|
||||
macs: self.macs,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct LeasesRequest {
|
||||
#[serde(default)]
|
||||
@@ -138,15 +96,41 @@ pub struct InventoryRequest {
|
||||
|
||||
impl InventoryRequest {
|
||||
pub fn into_device_query(self) -> DeviceQuery {
|
||||
StatusRequest {
|
||||
query: self.query,
|
||||
name: self.name,
|
||||
ips: self.ips,
|
||||
devs: self.devs,
|
||||
nuds: self.nuds,
|
||||
macs: self.macs,
|
||||
}
|
||||
.into_device_query()
|
||||
into_device_query(
|
||||
self.query, self.name, self.ips, self.devs, self.nuds, self.macs,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fn into_device_query(
|
||||
query: Option<String>,
|
||||
name: Option<String>,
|
||||
ips: Vec<IpAddr>,
|
||||
devs: Vec<String>,
|
||||
nuds: Vec<wakey_core::NeighborState>,
|
||||
macs: Vec<MacAddr>,
|
||||
) -> DeviceQuery {
|
||||
if let Some(q) = query.as_ref()
|
||||
&& name.is_none()
|
||||
&& ips.is_empty()
|
||||
&& devs.is_empty()
|
||||
&& nuds.is_empty()
|
||||
&& macs.is_empty()
|
||||
{
|
||||
return DeviceQuery {
|
||||
name: Some(q.clone()),
|
||||
..Default::default()
|
||||
};
|
||||
}
|
||||
|
||||
DeviceQuery {
|
||||
name: name.or(query),
|
||||
filter: DeviceFilters {
|
||||
ips,
|
||||
devs,
|
||||
nuds,
|
||||
macs,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,7 +147,6 @@ pub struct WakeRequest {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum AgentCommand {
|
||||
Status(StatusRequest),
|
||||
Leases(LeasesRequest),
|
||||
Devs(DevsRequest),
|
||||
Inventory(InventoryRequest),
|
||||
@@ -173,7 +156,6 @@ pub enum AgentCommand {
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum CommandResult {
|
||||
Status(DeviceInventory),
|
||||
Leases { rows: Vec<DhcpLeaseWithState> },
|
||||
Devs { rows: Vec<InterfaceSummary> },
|
||||
Inventory(DeviceInventory),
|
||||
|
||||
@@ -237,7 +237,6 @@ where
|
||||
|
||||
fn command_kind(command: &AgentCommand) -> &'static str {
|
||||
match command {
|
||||
AgentCommand::Status(_) => "status",
|
||||
AgentCommand::Leases(_) => "leases",
|
||||
AgentCommand::Devs(_) => "devs",
|
||||
AgentCommand::Inventory(_) => "inventory",
|
||||
|
||||
@@ -264,7 +264,6 @@ pub async fn run_command(
|
||||
|
||||
fn command_kind(command: &AgentCommand) -> &'static str {
|
||||
match command {
|
||||
AgentCommand::Status(_) => "status",
|
||||
AgentCommand::Leases(_) => "leases",
|
||||
AgentCommand::Devs(_) => "devs",
|
||||
AgentCommand::Inventory(_) => "inventory",
|
||||
|
||||
Reference in New Issue
Block a user