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
57
58
//! CLI interface to view and control running services

use super::comms::{Controllable, CmdFrom};
use std::io;
use std::io::{BufRead, Write};
use std::process::Command;
use std::sync::mpsc::{channel, Sender};

/// Controllable struct for the CLI
pub struct CLI {
    tx: Sender<CmdFrom>,
}

impl Controllable for CLI {
    fn setup(tx: Sender<CmdFrom>) -> CLI {
        CLI { tx: tx }
    }

    fn step(&mut self) -> bool {
        print!("> "); io::stdout().flush();

        let stdin = io::stdin();
        let fat_line = stdin.lock().lines().next().unwrap().unwrap();
        let line = fat_line.trim();

        if line.starts_with("!") {
            Command::new("sh").args(&["-c", &line[1..]]).status();
        } else {
            let mut words = line.split(" ");
            match words.next().unwrap_or("") {
                "" => {},
                "start" => {
                    let dev = words.next().unwrap_or("");
                    if !rpc!(self.tx, CmdFrom::Start, dev.to_string()).unwrap() {
                        errorln!("Failed to start {}", dev);
                    }
                },
                "stop" => {
                    let dev = words.next().unwrap_or("");
                    if !rpc!(self.tx, CmdFrom::Stop, dev.to_string()).unwrap() {
                        errorln!("Failed to stop {}", dev);
                    }
                },
                "quit" => {
                    self.tx.send(CmdFrom::Quit);
                },
                _ => println!("Unknown command!")
            }
        }

        false
    }

    fn teardown(&mut self) {
        // this will never be called
    }
}