use std::sync::atomic::{AtomicBool, Ordering};
static NON_INTERACTIVE: AtomicBool = AtomicBool::new(false);
static YES: AtomicBool = AtomicBool::new(false);
pub fn init(non_interactive: bool, yes: bool) {
NON_INTERACTIVE.store(non_interactive, Ordering::Relaxed);
YES.store(yes, Ordering::Relaxed);
}
pub fn is_non_interactive() -> bool {
NON_INTERACTIVE.load(Ordering::Relaxed)
}
#[allow(dead_code)] pub fn is_yes() -> bool {
YES.load(Ordering::Relaxed)
}
#[allow(dead_code)] pub fn require_value<T>(value: Option<T>, name: &str) -> Result<T, anyhow::Error> {
match value {
Some(v) => Ok(v),
None => {
if is_non_interactive() {
anyhow::bail!(
"{} is required in non-interactive mode. Use --{} flag or set environment variable.",
name,
name.replace('_', "-")
);
}
anyhow::bail!("{} is required", name);
}
}
}
#[allow(dead_code)] pub fn should_proceed_destructive(_action: &str) -> bool {
if is_yes() {
return true;
}
if is_non_interactive() {
return false; }
false
}