* cli to import a given csv configured text file Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
95 lines
2.7 KiB
Rust
95 lines
2.7 KiB
Rust
/* advotracker infrastructure.
|
|
*
|
|
* Copyright 2020 Ralf Zerres <ralf.zerres@networkx.de>
|
|
* SPDX-License-Identifier: (0BSD or MIT)
|
|
*/
|
|
|
|
// Rust nightly supports procedural macros!
|
|
//#![feature(proc_macro)]
|
|
//#![deny(rust_2018_idioms)]
|
|
|
|
use dotenv::dotenv;
|
|
use locales::t;
|
|
use serde::Deserialize;
|
|
|
|
use std::env;
|
|
use std::error::Error;
|
|
use std::io;
|
|
use std::process;
|
|
|
|
use tracing::{debug, trace, Level};
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
struct Environment {
|
|
test_lang: String,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct CsvImportRecord {
|
|
// dion => Allianz Dion: 1
|
|
// policy_code => Policy Typ: "AS"
|
|
// policy_number => Versicherungsscheinnummer: "1515735810"
|
|
pub dion: String,
|
|
pub policy_code: String,
|
|
pub policy_number: u32,
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
// the YAML file is found relative to the current file
|
|
//use clap_v3::{load_yaml, App};
|
|
use clap::{load_yaml, App};
|
|
|
|
let yaml = load_yaml!("cli.yml");
|
|
//let matches = App::from_yaml(yaml).get_matches();
|
|
let matches = App::from_yaml(yaml)
|
|
.name(clap::crate_name!())
|
|
.version(clap::crate_version!())
|
|
.author(clap::crate_authors!())
|
|
.about(clap::crate_description!())
|
|
.get_matches();
|
|
|
|
// Gets the option value if supplied by user, otherwise set defaults
|
|
let config = matches.value_of("config").unwrap_or("advotracker.conf");
|
|
let csv_import = matches.value_of("import").unwrap_or("allianz.txt");
|
|
//let username = matches.value_of("username").unwrap_or("nctalkbot");
|
|
//let password = matches.value_of("password").unwrap_or("botpassword");
|
|
let verbose = matches.occurrences_of("verbose");
|
|
|
|
if verbose > 0 {
|
|
println!("{}: runtime parameters", clap::crate_name!());
|
|
println!("config_file: '{}'", config);
|
|
println!("csv_import: '{}'", csv_import);
|
|
println!("verbosity level: {}", matches.occurrences_of("verbose"));
|
|
}
|
|
|
|
if verbose > 1 {
|
|
println!("\nEnvironment:");
|
|
for (key, value) in env::vars() {
|
|
println!("{}={}", key, value);
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
/// import from csv format (Standard in)
|
|
fn import() -> Result<(), Box<dyn Error>> {
|
|
// Build the CSV reader and iterate over each record.
|
|
let mut csv_reader = csv::Reader::from_reader(io::stdin());
|
|
for result in csv_reader.deserialize() {
|
|
// The iterator yields Result<StringRecord, Error>, so we check the
|
|
// error here.
|
|
let record: CsvImportRecord = result?;
|
|
println!("{:?}", record);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn main() {
|
|
if let Err(err) = import() {
|
|
println!("error running CSV-Import: {}", err);
|
|
process::exit(1);
|
|
}
|
|
}
|