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

View File

@ -23,14 +23,17 @@ fn main() {
fn from_env(env: EnvOpt) -> Result<(), GenericError> {
match env.subcommand {
Subcommand::Run(_) => {
let mut file = File::open("config.toml").with_msg("Could not find config.toml")?;
Subcommand::Run(opt) => {
let mut file = File::open(&opt.config).with_msg(format!(
"Could not find {}",
opt.config.to_str().unwrap_or("")
))?;
let mut string = String::new();
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 =
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(
&config,
@ -100,7 +103,7 @@ fn run(config: &Config, from: Option<String>, to: Option<String>) -> Result<(),
println!("{:<100}", "Done!");
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!(
&locs.iter().map(|loc| loc.0).collect::<Vec<NaiveDateTime>>(),
locs.len(),