use std::{
env,
fs::File,
io::{self, BufRead, BufReader},
path::Path,
};
use anyhow::Context;
use dialoguer::{Password, theme::ColorfulTheme};
use crate::utils::StringExt;
pub fn read_passphrase_from_tty() -> anyhow::Result<String> {
Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter passphrase")
.with_confirmation("Confirm passphrase", "Passphrases mismatch, try again")
.allow_empty_password(true)
.interact()
.context("could not read passphrase")
}
pub fn read_passphrase_from_stdin() -> anyhow::Result<String> {
let mut buf = String::new();
io::stdin()
.read_line(&mut buf)
.context("could not read passphrase from standard input")?;
buf.remove_newline();
Ok(buf)
}
pub fn read_passphrase_from_tty_once() -> anyhow::Result<String> {
Password::with_theme(&ColorfulTheme::default())
.with_prompt("Enter passphrase")
.allow_empty_password(true)
.interact()
.context("could not read passphrase")
}
pub fn read_passphrase_from_env(key: &str) -> anyhow::Result<String> {
env::var(key).context("could not read passphrase from environment variable")
}
pub fn read_passphrase_from_file(path: &Path) -> anyhow::Result<String> {
let file = File::open(path).with_context(|| format!("could not open {}", path.display()))?;
let mut reader = BufReader::new(file);
let mut buf = String::new();
reader
.read_line(&mut buf)
.with_context(|| format!("could not read passphrase from {}", path.display()))?;
buf.remove_newline();
Ok(buf)
}