Skip to content
This repository has been archived by the owner on Oct 26, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5 from advancedtelematic/feat/skeleton-for-testing
Browse files Browse the repository at this point in the history
Add skeleton for testing loop and listen to stdin for commands
  • Loading branch information
Jerry Trieu committed Mar 10, 2016
2 parents b8be624 + 290509d commit 00e256f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ hyper = "*"
log = "*"
rustc-serialize = "*"
toml = "*"
getopts = "*"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ extern crate toml;

pub mod config;
pub mod connect;
pub mod read_interpret;
36 changes: 33 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<String> = 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));
}
38 changes: 38 additions & 0 deletions src/read_interpret.rs
Original file line number Diff line number Diff line change
@@ -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<Command, ()> {
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."),
};

}

}

0 comments on commit 00e256f

Please sign in to comment.