use crate::error::{Failure, Result};
use secrecy::SecretString;
pub(super) fn read_secret_from_standard_input(refuses_to_ask: bool) -> Result<SecretString> {
use std::io::Read;
crate::settings::refuse_a_read_that_would_wait(refuses_to_ask, "a secret")?;
let mut buffer = String::new();
std::io::stdin()
.read_to_string(&mut buffer)
.map_err(|cause| {
Failure::config(
"secret.unreadable",
"the secret could not be read from the standard input",
)
.with_hint(
"pipe the secret in, as in `printf %s \"$SECRET\" | mariadb-cli conn add ...`",
)
.with_cause(cause.to_string())
})?;
let trimmed = buffer.trim_end_matches(['\n', '\r']);
if trimmed.is_empty() {
return Err(
Failure::config("secret.empty", "the standard input carried no secret")
.with_hint("pipe the secret in, or leave --password-stdin out"),
);
}
Ok(SecretString::from(trimmed.to_owned()))
}