pub fn ask_yn(prompt: String, default_yes: Option<bool>) -> Result<bool>
Expand description

Prompt the user to answer Y or N.

prompt will be printed as the question to answer. if default_yes is Some(true), entering a blank line equates to Y if default_yes is Some(false), entering a blank line equates to N if default_yes is None, Y or N must be explicitly entered, anything else is invalid

Returns true for Y, false for N

Examples found in repository?
src/conductor/interactive.rs (line 52)
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
pub fn prompt_for_database_dir(path: &Path) -> std::io::Result<()> {
    let prompt = format!(
        "There is no database at the path specified ({})\nWould you like to create one now?",
        path.display()
    );
    if ask_yn(prompt, Some(true))? {
        std::fs::create_dir_all(path)?;
        Ok(())
    } else {
        Err(std::io::Error::new(
            std::io::ErrorKind::Other,
            "Cannot continue without database.",
        ))
    }
}

/// If config_path is Some, attempt to load the config from that path, and return error if file not found
/// If config_path is None, attempt to load config from default path, and offer to create config if file not found
pub fn load_config_or_prompt_for_default(
    config_path: ConfigFilePath,
) -> ConductorResult<Option<ConductorConfig>> {
    ConductorConfig::load_yaml(config_path.as_ref()).map(Some).or_else(|err| {
        if let ConductorConfigError::ConfigMissing(_) = err {
            let prompt = format!(
                "There is no conductor config YAML file at the path specified ({})\nWould you like to create a default config file at this location?",
                config_path
            );
            if ask_yn(prompt, Some(true))? {
                let config = save_default_config_yaml(config_path.as_ref())?;
                println!("Conductor config written.");
                Ok(Some(config))
            } else {
                Ok(None)
            }
        } else {
            Err(err.into())
        }
    })
}