This repository has been archived by the owner on Oct 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from advancedtelematic/feat/skeleton-for-testing
Add skeleton for testing loop and listen to stdin for commands
- Loading branch information
Showing
4 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,4 @@ hyper = "*" | |
log = "*" | ||
rustc-serialize = "*" | ||
toml = "*" | ||
getopts = "*" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ extern crate toml; | |
|
||
pub mod config; | ||
pub mod connect; | ||
pub mod read_interpret; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."), | ||
}; | ||
|
||
} | ||
|
||
} |