hardcopy 1.0.1

Generate hard copies of keys, certs and secrets.
Documentation
use crate::*;
use anyhow::Context;
use std::io::{stdin, Read};

/// Abstracts the interaction with one or more input buffers.
pub struct HardCopyInput {
    raw: Vec<u8>,
}

impl HardCopyInput {
    /// Load input according to config: from a file if `--input` was given,
    /// otherwise from stdin.
    pub fn from_config(config: &HardCopyConfig) -> Result<HardCopyInput> {
        match config.input_file.as_deref() {
            Some(path) => HardCopyInput::from_file(path),
            None => HardCopyInput::from_stdin(),
        }
    }

    fn from_stdin() -> Result<HardCopyInput> {
        let mut raw = Vec::new();
        stdin()
            .lock()
            .read_to_end(&mut raw)
            .context("Failed to read from stdin.")?;
        Ok(HardCopyInput { raw })
    }

    fn from_file(path: &str) -> Result<HardCopyInput> {
        let raw = std::fs::read(path)
            .with_context(|| format!("Failed to read input file '{}'.", path))?;
        Ok(HardCopyInput { raw })
    }

    /// Raw bytes from the input stream, suitable for encoding.
    pub fn as_bytes(&self) -> &[u8] {
        &self.raw
    }
}