Add argh
This commit is contained in:
parent
4a6922c2c1
commit
fa77f315a1
25
src/cmd.rs
Normal file
25
src/cmd.rs
Normal 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 {}
|
36
src/main.rs
36
src/main.rs
@ -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 env: EnvOpt = argh::from_env();
|
||||||
|
|
||||||
|
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 config = Config::default();
|
||||||
|
|
||||||
let mut file = File::create("config.toml").unwrap();
|
let mut file = File::create("config.toml").unwrap();
|
||||||
file.write_all(&toml::to_vec(&config).unwrap()).ok();
|
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> {
|
||||||
|
Loading…
Reference in New Issue
Block a user