From 290509df9b0df37f92332c82e7606bd28a60d0d7 Mon Sep 17 00:00:00 2001 From: Stevan Andjelkovic Date: Thu, 10 Mar 2016 12:11:45 +0100 Subject: [PATCH] Add skeleton for testing (loop and listen to stdin for commands). This will be useful when we spawn processes. --- Cargo.toml | 1 + src/lib.rs | 1 + src/main.rs | 36 +++++++++++++++++++++++++++++++++--- src/read_interpret.rs | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 src/read_interpret.rs diff --git a/Cargo.toml b/Cargo.toml index 87bd38a..2af0ab8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,3 +9,4 @@ hyper = "*" log = "*" rustc-serialize = "*" toml = "*" +getopts = "*" \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index d0bdd25..ebc867d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,3 +7,4 @@ extern crate toml; pub mod config; pub mod connect; +pub mod read_interpret; diff --git a/src/main.rs b/src/main.rs index e6f31ad..855947b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,44 @@ -extern crate ota_plus_client; extern crate env_logger; +extern crate getopts; +extern crate ota_plus_client; +use getopts::Options; use std::env; -use ota_plus_client::{config, connect}; +use ota_plus_client::{config, connect, read_interpret}; + fn main() { + let args: Vec = env::args().collect(); + let program = args[0].clone(); + + let mut opts = Options::new(); + opts.optflag("l", "loop", "enter testing loop"); + opts.optflag("h", "help", "print this help menu"); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(e) => panic!(e.to_string()) + }; + + if matches.opt_present("h") { + print_usage(&program, opts); + return; + } + env_logger::init().unwrap(); - let cfg_file = env::var("OTA_PLUS_CLIENT_CFG").unwrap_or("/opt/ats/ota/etc/ota.toml".to_string()); + let cfg_file = env::var("OTA_PLUS_CLIENT_CFG") + .unwrap_or("/opt/ats/ota/etc/ota.toml".to_string()); let client = connect::OtaClient::new(config::parse_config(&cfg_file)); client.check_for_update(); + + if matches.opt_present("l") { + read_interpret::read_interpret_loop(); + } +} + +fn print_usage(program: &str, opts: Options) { + let brief = format!("Usage: {} [options]", program); + print!("{}", opts.usage(&brief)); } diff --git a/src/read_interpret.rs b/src/read_interpret.rs new file mode 100644 index 0000000..f7c85d9 --- /dev/null +++ b/src/read_interpret.rs @@ -0,0 +1,38 @@ +use std::io; +use std::str::FromStr; + +enum Command { + ListPackages, +} + +impl FromStr for Command { + type Err = (); + fn from_str(s: &str) -> Result { + match s { + "ListPackages" => Ok(Command::ListPackages), + _ => Err(()), + } + } +} + +fn interpret(cmd: Command) { + match cmd { + Command::ListPackages => info!("ok"), + }; +} + +pub fn read_interpret_loop() { + + loop { + + let mut input = String::new(); + let _ = io::stdin().read_line(&mut input); + + match input.trim().parse() { + Ok(cmd) => interpret(cmd), + Err(_) => error!("Parse error."), + }; + + } + +}