This commit is contained in:
Sofia 2020-08-23 02:18:31 +03:00
parent 4a6922c2c1
commit fa77f315a1
2 changed files with 62 additions and 3 deletions

25
src/cmd.rs Normal file
View File

@ -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 {}

View File

@ -1,17 +1,51 @@
mod api; mod api;
mod cmd;
mod config; mod config;
use api::API; use api::API;
use chrono::{NaiveDateTime, ParseError}; use chrono::{NaiveDateTime, ParseError};
use cmd::*;
use config::Config; use config::Config;
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
fn main() { fn main() {
let config = Config::default(); let env: EnvOpt = argh::from_env();
let mut file = File::create("config.toml").unwrap(); match env.subcommand {
file.write_all(&toml::to_vec(&config).unwrap()).ok(); 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<String>, to: Option<String>) -> Result<(), ParseError> { fn run(config: &Config, from: Option<String>, to: Option<String>) -> Result<(), ParseError> {