diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-04-06 07:20:11 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-04-06 07:20:11 -0400 |
commit | 1c40a0f9ff0ced652ee8b74f2333000ca47a0692 (patch) | |
tree | a0be3e77060d7f267596d70e4ce14b4604f9bb25 /src/pinentry.rs | |
parent | bc18bca5c67b4a678a31198877e39d57d97b1e0c (diff) | |
download | rbw-1c40a0f9ff0ced652ee8b74f2333000ca47a0692.tar.gz rbw-1c40a0f9ff0ced652ee8b74f2333000ca47a0692.zip |
a bit more cleanup
Diffstat (limited to 'src/pinentry.rs')
-rw-r--r-- | src/pinentry.rs | 55 |
1 files changed, 32 insertions, 23 deletions
diff --git a/src/pinentry.rs b/src/pinentry.rs index 610f52d..8a25759 100644 --- a/src/pinentry.rs +++ b/src/pinentry.rs @@ -1,7 +1,12 @@ -use tokio::io::{AsyncBufReadExt as _, AsyncWriteExt as _}; +use crate::prelude::*; -// TODO result -pub async fn pinentry(prompt: &str, desc: &str, tty: Option<&str>) -> String { +use tokio::io::AsyncWriteExt as _; + +pub async fn pinentry( + prompt: &str, + desc: &str, + tty: Option<&str>, +) -> Result<String> { let mut opts = tokio::process::Command::new("pinentry"); let opts = opts .stdin(std::process::Stdio::piped()) @@ -11,39 +16,43 @@ pub async fn pinentry(prompt: &str, desc: &str, tty: Option<&str>) -> String { } else { opts }; - let mut child = opts.spawn().unwrap(); + let mut child = opts.spawn().context(crate::error::Spawn)?; { let stdin = child.stdin.as_mut().unwrap(); - let mut stdout = - tokio::io::BufReader::new(child.stdout.as_mut().unwrap()); - let mut buf = String::new(); - - stdin.write_all(b"SETTITLE rbw\n").await.unwrap(); - stdout.read_line(&mut buf).await.unwrap(); stdin + .write_all(b"SETTITLE rbw\n") + .await + .context(crate::error::WriteStdin)?; + stdin .write_all(format!("SETPROMPT {}\n", prompt).as_bytes()) .await - .unwrap(); - stdout.read_line(&mut buf).await.unwrap(); - + .context(crate::error::WriteStdin)?; stdin .write_all(format!("SETDESC {}\n", desc).as_bytes()) .await - .unwrap(); - stdout.read_line(&mut buf).await.unwrap(); - - stdin.write_all(b"GETPIN\n").await.unwrap(); + .context(crate::error::WriteStdin)?; + stdin + .write_all(b"GETPIN\n") + .await + .context(crate::error::WriteStdin)?; } - let res = - String::from_utf8(child.wait_with_output().await.unwrap().stdout) - .unwrap(); - for line in res.lines() { + + let out = child + .wait_with_output() + .await + .context(crate::error::ProcessWaitOutput)? + .stdout; + let out_str = String::from_utf8(out.clone()).context( + crate::error::FailedToParsePinentryUtf8 { out: out.clone() }, + )?; + for line in out_str.lines() { if line.starts_with("OK") { continue; } else if line.starts_with("D ") { - return line[2..line.len()].to_string(); + return Ok(line[2..line.len()].to_string()); } } - panic!("failed to parse pinentry output: {:?}", res) + + Err(Error::FailedToParsePinentry { out }) } |