diff --git a/src/cmd.rs b/src/cmd.rs new file mode 100644 index 0000000..a3a2e5c --- /dev/null +++ b/src/cmd.rs @@ -0,0 +1,25 @@ +use argh::FromArgs; + +#[derive(FromArgs)] +#[argh(description = "Tool for gathering location data from Yepzon servers.")] +pub struct EnvOpt { + #[argh(subcommand)] + pub subcommand: Subcommand, +} + +#[derive(FromArgs)] +#[argh(subcommand)] +pub enum Subcommand { + Run(RunOpt), + Init(InitOpt), +} + +#[derive(FromArgs)] +#[argh(subcommand, name = "run")] +#[argh(description = "Run the tool")] +pub struct RunOpt {} + +#[derive(FromArgs)] +#[argh(subcommand, name = "init")] +#[argh(description = "Initialize a config file")] +pub struct InitOpt {} diff --git a/src/main.rs b/src/main.rs index f8b1731..76f09b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,51 @@ mod api; +mod cmd; mod config; use api::API; use chrono::{NaiveDateTime, ParseError}; +use cmd::*; use config::Config; use std::fs::File; use std::io::prelude::*; fn main() { - let config = Config::default(); + let env: EnvOpt = argh::from_env(); - let mut file = File::create("config.toml").unwrap(); - file.write_all(&toml::to_vec(&config).unwrap()).ok(); + match env.subcommand { + Subcommand::Run(opt) => { + let mut file = match File::open("config.toml") { + Ok(file) => file, + Err(e) => { + eprintln!("Could not read config file: {}\nMake sure one exists with init subcommand.", e); + return; + } + }; + let mut string = String::new(); + if let Err(e) = file.read_to_string(&mut string) { + eprintln!("Config file data not valid UTF-8: {}", e); + return; + } + + let config: Config = match toml::from_str(&string) { + Ok(config) => config, + Err(e) => { + eprintln!("Config file could not be parsed: {}", e); + return; + } + }; + + run(&config, None, None).ok(); + } + Subcommand::Init(opt) => { + let config = Config::default(); + + let mut file = File::create("config.toml").unwrap(); + if let Err(e) = file.write_all(&toml::to_vec(&config).unwrap()) { + eprintln!("Could not write to file: {}", e); + } + } + } } fn run(config: &Config, from: Option, to: Option) -> Result<(), ParseError> {