From f488a088514303a05ea1957f977c14a35ebb6f69 Mon Sep 17 00:00:00 2001 From: clerie Date: Sat, 24 Aug 2024 19:16:02 +0200 Subject: [PATCH] Add option to format output as json --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 1 + src/main.rs | 17 ++++++++++++++--- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e494ac..b3594b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index d97ebe4..c82b012 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/main.rs b/src/main.rs index 953da5e..4d0ef09 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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); - let message_map_yaml = serde_yaml::to_string(&message_map) - .context("Unable to format message to yaml")?; - println!("{}", message_map_yaml); + + 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(()) }