114 lines
3.7 KiB
Rust
114 lines
3.7 KiB
Rust
mod api;
|
|
mod args;
|
|
mod commands;
|
|
mod config;
|
|
mod errors;
|
|
mod gpx;
|
|
|
|
use api::{LocationModel, TagModel, API};
|
|
use args::*;
|
|
use chrono::offset::Local;
|
|
use chrono::{NaiveDate, NaiveDateTime, NaiveTime, ParseError};
|
|
use config::Config;
|
|
use errors::{GenericError, MessagedError};
|
|
|
|
fn main() {
|
|
let env: EnvOpt = argh::from_env();
|
|
if let Err(e) = from_env(env) {
|
|
eprintln!("Critical Error: {}", e);
|
|
std::process::exit(1);
|
|
}
|
|
}
|
|
|
|
fn from_env(env: EnvOpt) -> Result<(), GenericError> {
|
|
match env.subcommand {
|
|
Subcommand::Between(opt) => {
|
|
let config = Config::from_path(&env.config)?;
|
|
let mut api = API::new(config.clone());
|
|
|
|
let since =
|
|
Some(try_get_datetime(opt.since, &config).with_msg("Failed to parse since")?);
|
|
|
|
let until = match opt.until {
|
|
Some(until) => {
|
|
Some(try_get_datetime(until, &config).with_msg("Failed to parse until")?)
|
|
}
|
|
None => None,
|
|
};
|
|
|
|
let (_, locs) = commands::between(&mut api, opt.device, since, until)?;
|
|
dbg!(
|
|
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
|
|
locs.len(),
|
|
);
|
|
Ok(())
|
|
}
|
|
Subcommand::Init(opt) => {
|
|
commands::init(&env.config, opt.api_key)?;
|
|
Ok(())
|
|
}
|
|
Subcommand::Api(_) => {
|
|
let config = Config::from_path(&env.config)?;
|
|
let api = API::new(config.clone());
|
|
match commands::check_api(&api) {
|
|
Ok(_) => {
|
|
println!("API verified, no issues");
|
|
Ok(())
|
|
}
|
|
Err(e) => Err(GenericError::MessagedError(
|
|
"API integrity failed, or API-key is not valid. ".to_owned(),
|
|
Some(Box::new(e)),
|
|
)),
|
|
}
|
|
}
|
|
Subcommand::Nick(opt) => {
|
|
let config = Config::from_path(&env.config)?;
|
|
let mut api = API::new(config.clone());
|
|
|
|
match opt.subcommand {
|
|
NickSub::List(_) => {
|
|
for ((idx, nick), tag) in commands::nick_list(&api)? {
|
|
println!("{}. {}\n{}", idx, nick, tag);
|
|
}
|
|
}
|
|
NickSub::Set(opt) => {
|
|
commands::nick_set(&mut api, &env.config, opt.device, opt.nickname)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(debug_assertions)]
|
|
Subcommand::Get(opt) => {
|
|
let config = Config::from_path(&env.config)?;
|
|
let api = API::new(config.clone());
|
|
match opt.subcommand {
|
|
GetSub::Tags(_) => {
|
|
let tags = api.get_tags()?;
|
|
dbg!(&tags);
|
|
}
|
|
GetSub::States(opt) => {
|
|
API::print_list(commands::get_states(&api, opt.device));
|
|
}
|
|
GetSub::Locations(opt) => {
|
|
API::print_list(commands::get_locations(&api, opt.device, opt.state))
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
fn try_get_datetime(parsed: String, config: &Config) -> Result<NaiveDateTime, ParseError> {
|
|
match NaiveDateTime::parse_from_str(&parsed, &config.timedate_format) {
|
|
Ok(timedate) => Ok(timedate),
|
|
Err(_) => match NaiveDate::parse_from_str(&parsed, &config.date_format) {
|
|
Ok(date) => Ok(date.and_hms(0, 0, 0)),
|
|
Err(_) => match NaiveTime::parse_from_str(&parsed, &config.time_format) {
|
|
Ok(time) => Ok(Local::today().naive_local().and_time(time)),
|
|
Err(e) => Err(e),
|
|
},
|
|
},
|
|
}
|
|
}
|