diff options
| author | Teddy Wing | 2016-08-06 08:06:54 -0400 | 
|---|---|---|
| committer | Teddy Wing | 2016-08-06 08:06:54 -0400 | 
| commit | b9d53190baee46674f01b41f099dfebb85ae22fa (patch) | |
| tree | 509180f7dad216e93a61f62383aa96c049a0b19d | |
| parent | 494742673e26628f9b98185321db737b936af954 (diff) | |
| download | Passextract-b9d53190baee46674f01b41f099dfebb85ae22fa.tar.bz2 | |
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.
| -rw-r--r-- | src/main.rs | 63 | 
1 files changed, 35 insertions, 28 deletions
| 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<String> { +    fn push_option(options: &mut Vec<String>, 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<String> = 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); | 
