Add option to format output as json

This commit is contained in:
clerie 2024-08-24 19:16:02 +02:00
parent b9ce034241
commit f488a08851
3 changed files with 28 additions and 3 deletions

13
Cargo.lock generated
View File

@ -220,6 +220,7 @@ dependencies = [
"log",
"pnet",
"serde",
"serde_json",
"serde_yaml",
"tokio",
]
@ -730,6 +731,18 @@ dependencies = [
"syn 2.0.72",
]
[[package]]
name = "serde_json"
version = "1.0.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"

View File

@ -13,5 +13,6 @@ ipnetwork = "0.20.0"
log = "0.4.22"
pnet = "0.35.0"
serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.127"
serde_yaml = "0.9.34"
tokio = { version = "1.39.3", features = ["macros", "net", "rt-multi-thread"] }

View File

@ -38,6 +38,9 @@ const DHCP_RELAY_AGENT_AND_SERVER_PORT: u16 = 547;
struct Cli {
/// Network interface name used for DHCPv6
interface: String,
/// Format output as json
#[arg(long)]
json: bool,
}
#[derive(Serialize)]
@ -154,9 +157,17 @@ async fn main() -> Result<()>{
};
let message_map = message_to_map(&response_msg);
if cli.json {
let message_map_json = serde_json::to_string(&message_map)
.context("Unable to format message to json")?;
println!("{}", message_map_json);
}
else {
let message_map_yaml = serde_yaml::to_string(&message_map)
.context("Unable to format message to yaml")?;
println!("{}", message_map_yaml);
}
Ok(())
}