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

Additional config files specified on commandline #19

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub trait Facter<T: From<u32> + From<bool>> {
}

///Wraps a bit flag, usually representing if a feature is present or not
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

at what point do these fields get cloned in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pub struct Flag {
pub name: String,
pub bit: u8,
Expand All @@ -44,7 +44,7 @@ impl Bindable for Flag {
}

///Wraps an integer value from a bit field
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Int {
pub name: String,
pub bounds: ops::Range<u8>,
Expand All @@ -69,7 +69,7 @@ impl Bindable for Int {

/// Wraps an X86Model representation
/// These can have a number of weird conditions and are always going to be a part of a bit field
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct X86Model {
pub name: String,
}
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Bindable for X86Model {
}
}

#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct X86Family {
pub name: String,
}
Expand Down Expand Up @@ -198,7 +198,7 @@ impl<'a> fmt::Display for Bound<'a, X86Family> {
}
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(tag = "type")]
pub enum Field {
Int(Int),
Expand Down
48 changes: 42 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::error::Error;
use std::fmt;
use std::path::{Path, PathBuf};

type YAMLFact = GenericFact<serde_yaml::Value>;
type YAMLFactSet = FactSet<serde_yaml::Value>;
Expand All @@ -37,6 +38,8 @@ enum CommandOpts {
struct Disp {
#[arg(short, long)]
raw: bool,
#[arg(long)]
skip_cpu: bool,
#[cfg(all(target_os = "linux", feature = "kvm"))]
#[arg(long)]
skip_kvm: bool,
Expand All @@ -50,11 +53,13 @@ impl Command for Disp {
if self.raw {
display_raw()
} else {
println!("CPUID:");
let cpuid_db = cpuinfo::RunningCpuidDB::new();
for (leaf, desc) in &config.cpuids {
if let Some(bound) = desc.bind_leaf(*leaf, &cpuid_db) {
println!("{:#010x}: {}", leaf, bound);
if !self.skip_cpu {
cmouzaoui marked this conversation as resolved.
Show resolved Hide resolved
println!("CPUID:");
let cpuid_db = cpuinfo::RunningCpuidDB::new();
for (leaf, desc) in &config.cpuids {
if let Some(bound) = desc.bind_leaf(*leaf, &cpuid_db) {
println!("{:#010x}: {}", leaf, bound);
}
}
}

Expand Down Expand Up @@ -307,6 +312,17 @@ struct Definition {
pub msrs: Vec<MSRDesc>,
}

impl Definition {
pub fn union(&mut self, b: Definition) {
let Definition {
mut cpuids,
mut msrs,
} = b;
self.cpuids.append(&mut cpuids);
self.msrs.append(&mut msrs);
cmouzaoui marked this conversation as resolved.
Show resolved Hide resolved
}
}

fn find_read_config() -> Result<Definition, Box<dyn std::error::Error>> {
let file = include_str!("config.yaml");
Ok(serde_yaml::from_str(file)?)
Expand All @@ -332,15 +348,35 @@ fn display_raw() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}

fn read_additional_configs<Paths, P>(
def: &mut Definition,
paths: Paths,
) -> Result<(), Box<dyn std::error::Error>>
where
Paths: Iterator<Item = P>,
P: AsRef<Path> + Sized,
{
for path in paths {
let file = std::fs::read(path)?;
let definition = serde_yaml::from_slice(&file)?;
def.union(definition);
}
Ok(())
}

#[derive(Clone, Parser)]
struct CmdLine {
#[arg(short, long)]
add_config: Vec<PathBuf>,
#[command(subcommand)]
command: CommandOpts,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = CmdLine::parse();

let config = find_read_config()?;
let mut config = find_read_config()?;

read_additional_configs(&mut config, args.add_config.iter())?;

args.command.run(&config)
}
2 changes: 1 addition & 1 deletion src/msr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub mod linux {
}

/// Wraps a general description of an MSR
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MSRDesc {
pub name: String,
pub address: u32,
Expand Down
Loading