Add config file path selection

This commit is contained in:
Sofia 2020-08-24 02:51:59 +03:00
parent 18cddbab28
commit 3d78b9b108
4 changed files with 21 additions and 17 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/target /target
secret.toml

View File

@ -1,7 +0,0 @@
api_key = "E3GrOiQAnY61BP623XXzt9Fo87A1IQrS1FFzD57P"
tags_url = "https://platform.yepzon.com/tags"
states_url = "https://platform.yepzon.com/tags/{tag}/states"
locations_url = "https://platform.yepzon.com/tags/{tag}/locations/{state}"
timestamp_format = "%Y-%m-%dT%H:%M:%S%.fZ"
between_format = "%d.%m.%Y %H:%M:%S"
throttle = 9

View File

@ -1,4 +1,5 @@
use argh::FromArgs; use argh::FromArgs;
use std::path::PathBuf;
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(description = "Tool for gathering location data from Yepzon servers.")] #[argh(description = "Tool for gathering location data from Yepzon servers.")]
@ -15,11 +16,17 @@ pub enum Subcommand {
} }
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand, name = "run")] #[argh(subcommand, name = "run", description = "Run the tool")]
#[argh(description = "Run the tool")] pub struct RunOpt {
pub struct RunOpt {} #[argh(
option,
short = 'c',
description = "otus",
default = "PathBuf::from(\"config.toml\")"
)]
pub config: PathBuf,
}
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand, name = "init")] #[argh(subcommand, name = "init", description = "Initialize a config file")]
#[argh(description = "Initialize a config file")]
pub struct InitOpt {} pub struct InitOpt {}

View File

@ -23,14 +23,17 @@ fn main() {
fn from_env(env: EnvOpt) -> Result<(), GenericError> { fn from_env(env: EnvOpt) -> Result<(), GenericError> {
match env.subcommand { match env.subcommand {
Subcommand::Run(_) => { Subcommand::Run(opt) => {
let mut file = File::open("config.toml").with_msg("Could not find config.toml")?; let mut file = File::open(&opt.config).with_msg(format!(
"Could not find {}",
opt.config.to_str().unwrap_or("")
))?;
let mut string = String::new(); let mut string = String::new();
file.read_to_string(&mut string) file.read_to_string(&mut string)
.with_msg("config.toml is not valid UTF-8")?; .with_msg("config file is not valid UTF-8")?;
let config: Config = let config: Config =
toml::from_str(&string).with_msg("config.toml is not a valid config file")?; toml::from_str(&string).with_msg("given config file is not a valid config file")?;
run( run(
&config, &config,
@ -100,7 +103,7 @@ fn run(config: &Config, from: Option<String>, to: Option<String>) -> Result<(),
println!("{:<100}", "Done!"); println!("{:<100}", "Done!");
locations.sort_by(|loc1, loc2| loc2.timestamp.cmp(&loc1.timestamp)); locations.sort_by(|loc1, loc2| loc2.timestamp.cmp(&loc1.timestamp));
let locs = API::get_between(&locations, from, to, false, &config); let _locs = API::get_between(&locations, from, to, false, &config);
/*dbg!( /*dbg!(
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(), &locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
locs.len(), locs.len(),