Skip to content

Commit

Permalink
facts: Added json output
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjj20 committed Jun 11, 2024
1 parent eb8b71f commit fab944b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
28 changes: 23 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -119,11 +119,19 @@ impl Command for Disp {
}
}

#[derive(Clone, PartialEq, Eq, 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(
Expand Down Expand Up @@ -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(())
}
Expand Down

0 comments on commit fab944b

Please sign in to comment.