diff options
| author | Teddy Wing | 2016-08-06 08:13:20 -0400 | 
|---|---|---|
| committer | Teddy Wing | 2016-08-06 08:13:20 -0400 | 
| commit | 9a1caad4c934630ded171bfbf8a56ae1474b2a25 (patch) | |
| tree | 945d75ad99a27a64b07129b22a61b149b1a434b6 /src | |
| parent | 97feec40b57031156196af7c61ccbc2aba6ec8d7 (diff) | |
| parent | 9481309d1d118afe97f161cba00625ac68ab2b62 (diff) | |
| download | Passextract-9a1caad4c934630ded171bfbf8a56ae1474b2a25.tar.bz2 | |
Merge branch 'call-pass'
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 49 | 
1 files changed, 42 insertions, 7 deletions
| diff --git a/src/main.rs b/src/main.rs index 4a18223..ca58055 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 { @@ -37,12 +38,10 @@ fn move_selection(term: &mut Terminal, selection: &mut Point, style: Cell, amoun      term.printline_with_cell(selection.x, selection.y, "->", style);  } -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"); - +/// 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<String> { +    fn push_option(options: &mut Vec<String>, line: String) {          if line.starts_with("e: ") ||              line.starts_with("u: ") ||              line.starts_with("p: ") { @@ -50,6 +49,42 @@ fn main() {          }      } +    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<String> = env::args().collect(); + +    let input = if args.len() > 1 { +        &args[1] +    } else { +        "-" +    }; + +    let options = parse_options(input); +      if options.is_empty() {          process::exit(1);      } | 
