From 920e04ba23868e5b25c919c35e849befa762a894 Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 21 Dec 2023 15:44:57 -0500 Subject: [PATCH] keyfork-prompt: add DefaultPromptManager --- keyfork-prompt/src/lib.rs | 28 ++++++++++++++++---------- keyfork-shard/src/openpgp/keyring.rs | 19 +++-------------- keyfork-shard/src/openpgp/smartcard.rs | 19 ++++------------- 3 files changed, 24 insertions(+), 42 deletions(-) diff --git a/keyfork-prompt/src/lib.rs b/keyfork-prompt/src/lib.rs index 4ab5521..c221aa9 100644 --- a/keyfork-prompt/src/lib.rs +++ b/keyfork-prompt/src/lib.rs @@ -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, }; @@ -57,9 +57,9 @@ where while let Some(line) = lines.next() { terminal.queue(Print(line))?; if lines.peek().is_some() { - terminal - .queue(cursor::MoveDown(1))? - .queue(cursor::MoveToColumn(0))?; + terminal + .queue(cursor::MoveDown(1))? + .queue(cursor::MoveToColumn(0))?; } } terminal.flush()?; @@ -81,9 +81,9 @@ where while let Some(line) = lines.next() { terminal.queue(Print(line))?; if lines.peek().is_some() { - terminal - .queue(cursor::MoveDown(1))? - .queue(cursor::MoveToColumn(0))?; + terminal + .queue(cursor::MoveDown(1))? + .queue(cursor::MoveToColumn(0))?; } } terminal.flush()?; @@ -128,9 +128,9 @@ where while let Some(line) = lines.next() { terminal.queue(Print(line))?; if lines.peek().is_some() { - terminal - .queue(cursor::MoveDown(1))? - .queue(cursor::MoveToColumn(0))?; + terminal + .queue(cursor::MoveDown(1))? + .queue(cursor::MoveToColumn(0))?; } } terminal @@ -153,3 +153,9 @@ where Ok(()) } } + +pub type DefaultPromptManager = PromptManager; + +pub fn default_prompt_manager() -> Result { + PromptManager::new(stdin(), stderr()) +} diff --git a/keyfork-shard/src/openpgp/keyring.rs b/keyfork-shard/src/openpgp/keyring.rs index 69a9732..9ade88a 100644 --- a/keyfork-shard/src/openpgp/keyring.rs +++ b/keyfork-shard/src/openpgp/keyring.rs @@ -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 = std::result::Result; pub struct Keyring { full_certs: Vec, root: Option, - pm: PromptManager, + pm: DefaultPromptManager, } impl Keyring { pub fn new(certs: impl AsRef<[Cert]>) -> Result { - 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()?, }) } diff --git a/keyfork-shard/src/openpgp/smartcard.rs b/keyfork-shard/src/openpgp/smartcard.rs index fac0df6..9de6c27 100644 --- a/keyfork-shard/src/openpgp/smartcard.rs +++ b/keyfork-shard/src/openpgp/smartcard.rs @@ -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) -> String { pub struct SmartcardManager { current_card: Option>, root: Option, - pm: PromptManager, + pm: DefaultPromptManager, } impl SmartcardManager { pub fn new() -> Result { - 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()?, }) }