Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facts: Added json output #15

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, PartialOrd, Ord, ValueEnum)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all of these derive arguments necessary for clap?

Copy link
Contributor Author

@andrewjj20 andrewjj20 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a new suggestion from rust-analyzer. I actually think the Ord derives probably do not make sense. Though some might consider certain types better than others.

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
Loading