use std::fs::File;
use ferrosys::{DetectOptions, Filesystem};
use crate::json::Obj;
use crate::{Error, emit};
pub fn run(args: crate::args::DetectArgs) -> Result<(), Error> {
let file = File::open(&args.image).map_err(|e| Error::io(&args.image, e))?;
let found = ferrosys::detect_with(file, &DetectOptions::new().base(args.offset));
let (family, profile) = match &found {
Ok(Filesystem::Ext(profile)) => ("ext", Some(profile.name())),
Ok(_) => ("unknown", None),
Err(_) => ("unrecognized", None),
};
let text = if args.json {
let mut out = String::new();
let mut o = Obj::new(&mut out);
o.u64("schema", crate::json::SCHEMA_VERSION);
o.str("family", family);
match profile {
Some(name) => o.str("profile", name),
None => o.raw("profile", "null"),
}
o.u64("offset", args.offset);
o.end();
out.push('\n');
out
} else {
match profile {
Some(name) => format!("{name}\n"),
None => format!("{family}\n"),
}
};
emit(text.as_bytes())?;
match found {
Ok(_) => Ok(()),
Err(source) => Err(Error::NotDetected {
path: args.image.display().to_string(),
source,
}),
}
}