1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
use workflow::Work; use workflow::Instruction::{Display, ExitCode}; use model::Value; use dto::{Request, Response}; use config::Context; use std::fs::File; use std::io::prelude::*; pub fn execute(request: Request, context: &Context) -> Work { Work::instruction( request.next() .current .and_then(|rc| context.find(&rc, true)) .map(|command| { if let Some(ref a) = command.value { return match *a { Value::Script(ref b) => { let file_path = context.directory.join(b); let mut file = File::open(&file_path).unwrap(); let mut content = s!(); file.read_to_string(&mut content).unwrap(); Display(format!("{}", content), Response::Ok) } Value::Shell(ref b) => Display(format!("{}", b), Response::Ok), _ => ExitCode(Response::Err(1)) } } ExitCode(Response::Err(1)) }) .unwrap_or_else(|| ExitCode(Response::Err(1)))) } pub fn auto_complete(request: Request, context: &Context) -> Work { Work::instruction( request.next() .current .and_then(|rc| context.find(&rc, false)) .map(|_| ExitCode(Response::Ok)) .unwrap_or_else(|| { let s = format!("{}", context.get_commands().iter() .filter(|c| { if let Some(ref a) = c.value { return match *a { Value::Script(_) => true, Value::Shell(_) => true, _ => false } } false }) .fold(s!(), |a, b| format!("{}{}\n", a, &b.name))); Display(s, Response::Ok) })) }