From 494742673e26628f9b98185321db737b936af954 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Aug 2016 07:29:17 -0400 Subject: Call `pass show` with the given argument and run Passextract on result Temporarily comment out code that deals with STDIN to get this feature working. It complicated things to leave it in right now. Given an argument that isn't "-" (STDIN), we pass that argument to `pass show`. Users correctly get a password prompt from `pass`, and the output from the command gets sent to the Passextract TUI for display in the menu and copying. --- src/main.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 4a18223..ebe1683 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,9 @@ use clipboard::ClipboardContext; use rustty::{Terminal, Event, Cell, Color, Attr}; use rustty::ui::Painter; +use std::env; use std::io::{self, BufRead}; -use std::process; +use std::process::{self, Command}; use std::time::Duration; struct Point { @@ -38,11 +39,24 @@ fn move_selection(term: &mut Terminal, selection: &mut Point, style: Cell, amoun } fn main() { + let args: Vec = env::args().collect(); + + let input = if args.len() > 1 { + &args[1] + } else { + "-" + }; + + let file = Command::new("pass") + .arg("show") + .arg(input) + .output() + .expect("Error executing `pass`") + .stdout; + + let output = String::from_utf8_lossy(&file); let mut options = Vec::new(); - let stdin = io::stdin(); - for line in stdin.lock().lines() { - let line = line.expect("Error reading from STDIN"); - + for line in output.lines() { if line.starts_with("e: ") || line.starts_with("u: ") || line.starts_with("p: ") { @@ -50,6 +64,18 @@ fn main() { } } + // let mut options = Vec::new(); + // let stdin = io::stdin(); + // for line in stdin.lock().lines() { + // let line = line.expect("Error reading from STDIN"); + // + // if line.starts_with("e: ") || + // line.starts_with("u: ") || + // line.starts_with("p: ") { + // options.push(line); + // } + // } + if options.is_empty() { process::exit(1); } -- cgit v1.2.3 From b9d53190baee46674f01b41f099dfebb85ae22fa Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Aug 2016 08:06:54 -0400 Subject: Extract options from both `pass` and STDIN Get option extraction/parsing working both for STDIN and for calls to `pass show`. If no argument is passed to the executable or the first argument is "-", options are read from STDIN. Otherwise, the first argument is passed to `pass show`, the output of which is parsed as options. --- src/main.rs | 63 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ebe1683..6410ac5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,40 @@ fn move_selection(term: &mut Terminal, selection: &mut Point, style: Cell, amoun term.printline_with_cell(selection.x, selection.y, "->", style); } +fn parse_options(filename: &str) -> Vec { + fn push_option(options: &mut Vec, line: String) { + if line.starts_with("e: ") || + line.starts_with("u: ") || + line.starts_with("p: ") { + options.push(line); + } + } + + let mut options = Vec::new(); + + if filename == "-" { + let stdin = io::stdin(); + + for line in stdin.lock().lines() { + let line = line.expect("Error reading from STDIN"); + push_option(&mut options, line.to_owned()); + } + } else { + let file = Command::new("pass") + .arg("show") + .arg(filename) + .output() + .expect("Error executing `pass`") + .stdout; + + for line in String::from_utf8_lossy(&file).lines() { + push_option(&mut options, line.to_owned()); + } + } + + options +} + fn main() { let args: Vec = env::args().collect(); @@ -47,34 +81,7 @@ fn main() { "-" }; - let file = Command::new("pass") - .arg("show") - .arg(input) - .output() - .expect("Error executing `pass`") - .stdout; - - let output = String::from_utf8_lossy(&file); - let mut options = Vec::new(); - for line in output.lines() { - if line.starts_with("e: ") || - line.starts_with("u: ") || - line.starts_with("p: ") { - options.push(line); - } - } - - // let mut options = Vec::new(); - // let stdin = io::stdin(); - // for line in stdin.lock().lines() { - // let line = line.expect("Error reading from STDIN"); - // - // if line.starts_with("e: ") || - // line.starts_with("u: ") || - // line.starts_with("p: ") { - // options.push(line); - // } - // } + let options = parse_options(input); if options.is_empty() { process::exit(1); -- cgit v1.2.3 From 9481309d1d118afe97f161cba00625ac68ab2b62 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Aug 2016 08:11:51 -0400 Subject: parse_options: Add a short sentence of documentation --- src/main.rs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 6410ac5..ca58055 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,8 @@ fn move_selection(term: &mut Terminal, selection: &mut Point, style: Cell, amoun term.printline_with_cell(selection.x, selection.y, "->", style); } +/// Given a filename, either parse options from STDIN or send the file to +/// `pass show` and parse the result as options. fn parse_options(filename: &str) -> Vec { fn push_option(options: &mut Vec, line: String) { if line.starts_with("e: ") || -- cgit v1.2.3