~gpanders/passage

ref: 849cef24e44e6b1979a78f9244c02a329a8ed634 passage/src/input.rs -rw-r--r-- 738 bytes
849cef24Gregory Anders Add uninstall target to Makefile 11 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::io;
use std::io::prelude::*;

use crate::error::Error;

pub fn read_secret(prompt: &str, confirm: Option<&str>) -> Result<String, Error> {
    let input = rpassword::prompt_password_stdout(&format!("{}: ", prompt))?;

    match confirm {
        Some(prompt) => {
            if rpassword::prompt_password_stdout(&format!("{}: ", prompt))? != input {
                Err(Error::PasswordsDoNotMatch)
            } else {
                Ok(input)
            }
        }
        None => Ok(input),
    }
}

pub fn read_input(prompt: &str) -> Result<String, Error> {
    print!("{} ", prompt);
    io::stdout().flush()?;

    let mut ans = String::new();
    io::stdin().read_line(&mut ans)?;

    Ok(ans.trim_end().to_string())
}