use super::*;
#[derive(Debug, clap::Args)]
pub struct CommandBytes {
#[arg(value_name = "BYTE-COUNT")]
len: usize,
#[arg(short, long, value_name = "FORMAT", default_value = "hex")]
format: BinFormat,
#[arg(short, long, value_name = "FILENAME")]
output: Option<std::path::PathBuf>,
}
impl CommandBytes {
pub fn process<T: AsMut<S>, S: ToolState, W: Write>(
&self,
mut tool_state: T,
out: &mut W,
) -> Result<(), Error> {
let tool_state = tool_state.as_mut();
let secret = tool_state.current_secret()?;
let bytes = secret.extract_bytes(self.len)?;
let keypath = tool_state.get_keypath()?;
tool_state
.key_map_mut()
.update(keypath)
.unwrap()
.add_primitive(format!("bytes-{}", self.len));
if let Some(path) = self.output.as_ref() {
std::fs::write(path, self.format.to_string(&bytes)?)?;
} else {
self.format.write(out, &bytes)?;
}
Ok(())
}
}