keyfork-prompt: add DefaultPromptManager

This commit is contained in:
Ryan Heywood 2023-12-21 15:44:57 -05:00
parent bfb44292f4
commit 920e04ba23
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
3 changed files with 24 additions and 42 deletions

View File

@ -1,13 +1,13 @@
use std::{
io::{BufRead, BufReader, Read, Write},
io::{stderr, stdin, BufRead, BufReader, Read, Stderr, Stdin, Write},
os::fd::AsRawFd,
};
use crossterm::{
cursor,
event::{read, Event, KeyCode},
style::{Print, PrintStyledContent, Stylize},
terminal,
cursor,
tty::IsTty,
QueueableCommand,
};
@ -153,3 +153,9 @@ where
Ok(())
}
}
pub type DefaultPromptManager = PromptManager<Stdin, Stderr>;
pub fn default_prompt_manager() -> Result<DefaultPromptManager> {
PromptManager::new(stdin(), stderr())
}

View File

@ -1,6 +1,4 @@
use std::fs::File;
use keyfork_prompt::{Error as PromptError, PromptManager};
use keyfork_prompt::{Error as PromptError, DefaultPromptManager, default_prompt_manager};
use super::openpgp::{
self,
@ -18,12 +16,6 @@ pub enum Error {
#[error("Secret key was not found")]
SecretKeyNotFound,
#[error("Could not find TTY when prompting")]
NoTTY,
#[error("Could not open TTY: {0}")]
Io(#[from] std::io::Error),
#[error("Prompt failed: {0}")]
Prompt(#[from] PromptError),
}
@ -33,20 +25,15 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
pub struct Keyring {
full_certs: Vec<Cert>,
root: Option<Cert>,
pm: PromptManager<File, File>,
pm: DefaultPromptManager,
}
impl Keyring {
pub fn new(certs: impl AsRef<[Cert]>) -> Result<Self> {
let tty = std::env::vars()
.filter(|(k, _v)| k.as_str() == "GPG_TTY")
.next()
.ok_or(Error::NoTTY)?
.1;
Ok(Self {
full_certs: certs.as_ref().to_vec(),
root: Default::default(),
pm: PromptManager::new(File::open(&tty)?, File::options().write(true).open(&tty)?)?,
pm: default_prompt_manager()?,
})
}

View File

@ -1,6 +1,6 @@
use std::{collections::HashSet, fs::File};
use std::{collections::HashSet};
use keyfork_prompt::{Error as PromptError, PromptManager};
use keyfork_prompt::{Error as PromptError, DefaultPromptManager, default_prompt_manager};
use super::openpgp::{
self,
@ -44,12 +44,6 @@ pub enum Error {
#[error("Invalid PIN entered too many times")]
InvalidPIN,
#[error("Could not find TTY when prompting")]
NoTTY,
#[error("Could not open TTY: {0}")]
Io(#[from] std::io::Error),
#[error("Prompt failed: {0}")]
Prompt(#[from] PromptError),
}
@ -70,20 +64,15 @@ fn format_name(input: impl AsRef<str>) -> String {
pub struct SmartcardManager {
current_card: Option<Card<Open>>,
root: Option<Cert>,
pm: PromptManager<File, File>,
pm: DefaultPromptManager,
}
impl SmartcardManager {
pub fn new() -> Result<Self> {
let tty = std::env::vars()
.filter(|(k, _v)| k.as_str() == "GPG_TTY")
.next()
.ok_or(Error::NoTTY)?
.1;
Ok(Self {
current_card: None,
root: None,
pm: PromptManager::new(File::open(&tty)?, File::options().write(true).open(&tty)?)?,
pm: default_prompt_manager()?,
})
}