use crate::{
command::{CommandError, OutputEncoding},
key::TpmKey,
};
use std::{
fs,
io::{self, Read, Write},
path::Path,
};
pub fn read_file_input(input: Option<&Path>) -> io::Result<Vec<u8>> {
let mut input_bytes = Vec::new();
match input {
Some(path) => {
input_bytes = std::fs::read(path)?;
}
None => {
io::stdin().read_to_end(&mut input_bytes)?;
}
}
Ok(input_bytes)
}
pub fn write_key_data(
writer: &mut dyn Write,
tpm_key: &TpmKey,
output: Option<&Path>,
encoding: OutputEncoding,
) -> Result<(), CommandError> {
let output_bytes = match encoding {
OutputEncoding::Der => tpm_key.to_der().map_err(CommandError::Key)?,
OutputEncoding::Pem => tpm_key.to_pem().map_err(CommandError::Key)?.into_bytes(),
};
write_data(writer, output, &output_bytes)
}
fn write_data(
writer: &mut dyn Write,
output_path: Option<&Path>,
data: &[u8],
) -> Result<(), CommandError> {
if let Some(path) = output_path {
if path.to_str() == Some("-") {
writer.write_all(data)?;
} else {
fs::write(path, data)?;
writeln!(writer, "file:{}", path.to_string_lossy())?;
}
} else {
writer.write_all(data)?;
}
Ok(())
}