From eaa1af90c0f9b1be408fa0a713deaea9c7ec27d3 Mon Sep 17 00:00:00 2001 From: Joshua Job Date: Mon, 10 Jun 2024 15:40:48 -0700 Subject: [PATCH 1/2] facts: Added json output --- Cargo.lock | 28 +++++++++++++++++++++++----- Cargo.toml | 1 + src/main.rs | 16 ++++++++++++++-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 545f53d..d4f4db7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -124,6 +124,7 @@ dependencies = [ "kvm-bindings", "kvm-ioctls", "serde", + "serde_json", "serde_yaml", ] @@ -167,6 +168,12 @@ version = "1.70.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "kvm-bindings" version = "0.8.1" @@ -232,22 +239,33 @@ checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" [[package]] name = "serde" -version = "1.0.138" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.138" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", - "syn 1.0.98", + "syn 2.0.66", +] + +[[package]] +name = "serde_json" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3" +dependencies = [ + "itoa", + "ryu", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index ee365ba..da032ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ clap = { version = "4.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } serde_yaml = "0.8" enum_dispatch = "0.3.8" +serde_json = "1.0.117" [target.'cfg(target_os = "linux")'.dependencies] kvm-ioctls = { version = "0.17", optional = true } diff --git a/src/main.rs b/src/main.rs index 475568e..16c74ee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ // better performance but is not always intuitive behaviour. // use std::io::BufWriter; -use clap::{self, Args, Parser, Subcommand}; +use clap::{self, Args, Parser, Subcommand, ValueEnum}; use cpuinfo::facts::{FactSet, Facter, GenericFact}; use cpuinfo::layout::LeafDesc; use cpuinfo::msr::MsrStore; @@ -119,11 +119,19 @@ impl Command for Disp { } } +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] +enum FactsOutput { + Yaml, + Json, +} + #[derive(Clone, Args)] struct Facts { #[cfg(all(target_os = "linux", feature = "kvm"))] #[arg(short, long)] use_kvm: bool, + #[arg(short, long, value_enum, default_value = "yaml")] + out_type: FactsOutput, } fn collect_facts( @@ -203,9 +211,13 @@ impl Command for Facts { ) } }; + let facts = collect_facts(config, cpuid_source, msr_source)?; println!( "{}", - serde_yaml::to_string(&collect_facts(config, cpuid_source, msr_source)?)? + match self.out_type { + FactsOutput::Yaml => serde_yaml::to_string(&facts)?, + FactsOutput::Json => serde_json::to_string(&facts)?, + } ); Ok(()) } From 37251d8ec57483a42b4cdbbf8ae7a8148b8ebc44 Mon Sep 17 00:00:00 2001 From: Joshua Job Date: Mon, 10 Jun 2024 16:27:40 -0700 Subject: [PATCH 2/2] fixup! facts: Added json output --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 16c74ee..0a5bc85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,7 +119,7 @@ impl Command for Disp { } } -#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)] +#[derive(Clone, PartialEq, Eq, ValueEnum)] enum FactsOutput { Yaml, Json,