From 1a10f620b448b2a503aed405b9782f43d51e1d94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20B=C3=B6hme?= Date: Sat, 26 Aug 2023 11:06:51 +0200 Subject: [PATCH] feat: add argument to output answers as json --- Cargo.lock | 34 +++++++++++++++++++++++++++++----- Cargo.toml | 1 + src/cli.rs | 4 ++++ src/main.rs | 6 +++++- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78b6613..0335743 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -651,6 +651,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "itoa" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" + [[package]] name = "libc" version = "0.2.147" @@ -976,10 +982,16 @@ dependencies = [ ] [[package]] -name = "serde" -version = "1.0.164" +name = "ryu" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e8c8cf938e98f769bc164923b06dce91cea1751522f46f8466461af04c9027d" +checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741" + +[[package]] +name = "serde" +version = "1.0.179" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a5bf42b8d227d4abf38a1ddb08602e229108a517cd4e5bb28f9c7eaafdce5c0" dependencies = [ "serde_derive", ] @@ -996,15 +1008,26 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.164" +version = "1.0.179" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9735b638ccc51c28bf6914d90a2e9725b377144fc612c49a611fddd1b631d68" +checksum = "741e124f5485c7e60c03b043f79f320bff3527f4bbf12cf3831750dc46a0ec2c" dependencies = [ "proc-macro2", "quote", "syn 2.0.27", ] +[[package]] +name = "serde_json" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693151e1ac27563d6dbcec9dee9fbd5da8539b20fa14ad3752b2e6d363ace360" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "serde_repr" version = "0.1.16" @@ -1170,6 +1193,7 @@ dependencies = [ "notify-rust", "serde", "serde_cbor", + "serde_json", "signal-hook", "thiserror", ] diff --git a/Cargo.toml b/Cargo.toml index 8c2f0e2..e8603cb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,5 +14,6 @@ libc = "0.2.147" notify-rust = "4.8.0" serde = { version = "1.0.164", features = ["derive"] } serde_cbor = "0.11.2" +serde_json = "1.0.105" signal-hook = "0.3.17" thiserror = "1.0.44" diff --git a/src/cli.rs b/src/cli.rs index 08daac2..549024c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -12,9 +12,13 @@ use std::time::Duration; pub struct Cli { #[command(subcommand)] pub command: Command, + #[arg(short, long)] #[clap(default_value_t = format!("{}/timers.socket", run_path()))] pub socket: String, + + #[arg(long)] + pub json: bool } #[derive(Debug, Subcommand)] diff --git a/src/main.rs b/src/main.rs index 2249f05..07a5b27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,10 @@ fn main() -> Result<(), anyhow::Error> { } }; let answer = send_command(&args.socket, daemon_command)?; - print!("{}", answer); + if args.json { + println!("{}", serde_json::to_string(&answer)?) + } else { + print!("{}", answer); + }; Ok(()) }