use std::io;
use std::process::Command;

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct Report {
    pub blockdevices: Vec<BlockDev>,
}

#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct BlockDev {
    pub name: String,
    #[serde(rename = "maj:min")]
    pub maj_min: String,
    pub rm: bool,
    pub size: String,
    pub ro: bool,
    #[serde(rename = "type")]
    pub dev_type: String,
    pub mountpoints: Vec<Option<String>>,
    #[serde(default)]
    pub children: Vec<BlockDev>,
}

pub fn report() -> io::Result<Report> {
    let output = Command::new("lsblk").arg("--json").output()?;
    Ok(serde_json::from_slice(output.stdout.as_slice()).unwrap())
}
