104 lines
3.5 KiB
Rust
104 lines
3.5 KiB
Rust
pub mod args;
|
|
|
|
use super::errors::GenericError;
|
|
use super::{config_from_path, gpx, write_to};
|
|
use args::*;
|
|
use thingy_lib::api::API;
|
|
use thingy_lib::chrono::NaiveDateTime;
|
|
use thingy_lib::{try_get_datetime, MessagedError};
|
|
|
|
pub fn from_env(env: EnvOpt) -> Result<(), GenericError> {
|
|
match env.subcommand.unwrap() {
|
|
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 (tag, locs) = thingy_lib::between(&mut api, opt.device, since, until)?;
|
|
|
|
let gpx = gpx::generate_gpx(&tag, &locs, &api.config)?;
|
|
gpx::write_gpx(&gpx)?;
|
|
|
|
dbg!(
|
|
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
|
|
locs.len(),
|
|
);
|
|
Ok(())
|
|
}
|
|
Subcommand::Init(opt) => {
|
|
let config = thingy_lib::init(opt.api_key)?;
|
|
write_to(&config, &env.config)?;
|
|
Ok(())
|
|
}
|
|
Subcommand::Api(_) => {
|
|
let config = config_from_path(&env.config)?;
|
|
let api = API::new(config.clone());
|
|
match thingy_lib::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.into())),
|
|
)),
|
|
}
|
|
}
|
|
Subcommand::ShareToken(opt) => {
|
|
let config = config_from_path(&env.config)?;
|
|
let api = API::new(config.clone());
|
|
println!("{}", thingy_lib::sharetoken(&api, opt.device)?);
|
|
Ok(())
|
|
}
|
|
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 thingy_lib::nick_list(&api)? {
|
|
println!("{}. {}\n{}", idx, nick, tag);
|
|
}
|
|
}
|
|
NickSub::Set(opt) => {
|
|
thingy_lib::nick_set(&mut api, opt.device, opt.nickname)?;
|
|
write_to(&api.config, &env.config)?;
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(debug_assertions)]
|
|
Subcommand::Get(opt) => {
|
|
let config = config_from_path(&env.config)?;
|
|
let api = API::new(config.clone());
|
|
match opt.subcommand {
|
|
GetSub::Tag(opt) => {
|
|
let tags = api.get_tag(&opt.device);
|
|
dbg!(&tags);
|
|
}
|
|
GetSub::Tags(_) => {
|
|
let tags = api.get_tags()?;
|
|
dbg!(&tags);
|
|
}
|
|
GetSub::States(opt) => {
|
|
API::print_list(thingy_lib::get_states(&api, opt.device));
|
|
}
|
|
GetSub::Locations(opt) => {
|
|
API::print_list(thingy_lib::get_locations(&api, opt.device, opt.state))
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|