use std::io::{self, Read};
use crate::error::CliError;
pub(crate) const DEFAULT_MAX_BYTES: u64 = 10 * 1024 * 1024;
pub(crate) fn read_stdin_bytes(max_bytes: u64) -> std::result::Result<Vec<u8>, CliError> {
let mut buf = Vec::new();
let limit = max_bytes.saturating_add(1);
io::stdin()
.take(limit)
.read_to_end(&mut buf)
.map_err(|_| CliError::Io)?;
if buf.len() as u64 > max_bytes {
return Err(CliError::InputTooLarge);
}
Ok(buf)
}
pub(crate) fn read_stdin_text(max_bytes: u64) -> std::result::Result<String, CliError> {
let bytes = read_stdin_bytes(max_bytes)?;
if bytes.is_empty() {
return Err(CliError::EmptyInput);
}
String::from_utf8(bytes).map_err(|_| CliError::InvalidEncoding)
}
pub(crate) fn require_json_format(format: &str) -> std::result::Result<(), CliError> {
if format == "json" {
Ok(())
} else {
Err(CliError::PolicyConfigDetail(format!(
"--format must be 'json', got '{format}'"
)))
}
}