use std::io::{self, BufRead, IsTerminal, Read, Write};
use artifactize_core::platform::HiddenInput;
const SECRET_STDIN_BYTES: u64 = 8192;
pub(super) fn read(what: &str) -> Result<String, String> {
let stdin = io::stdin().lock();
let hidden = if stdin.is_terminal() {
let hidden = match HiddenInput::new() {
Ok(hidden) => Some(hidden),
Err(error) if artifactize_core::platform::VISIBLE_INPUT_FALLBACK => {
writeln!(
io::stderr().lock(),
"Warning: this terminal cannot hide input ({error}); the {what} will be visible."
)
.map_err(|e| e.to_string())?;
None
}
Err(error) => return Err(error.to_string()),
};
write!(io::stderr().lock(), "Paste the {what}, then press Enter: ")
.map_err(|e| e.to_string())?;
hidden
} else {
None
};
let mut secret = String::new();
let read = stdin.take(SECRET_STDIN_BYTES).read_line(&mut secret);
if hidden.is_some() {
drop(hidden);
let _ = writeln!(io::stderr().lock());
}
read.map_err(|_| format!("Cannot read the {what} from stdin."))?;
Ok(secret.trim().to_owned())
}