main.rs: introduce function parse_args
- commandline parsing: handle in an exclusive function - use Viperius - update tracing messages Signed-off-by: Ralf Zerres <ralf.zerres@networkx.de>
This commit is contained in:
140
src/main.rs
140
src/main.rs
@@ -8,32 +8,27 @@
|
|||||||
//#![feature(proc_macro)]
|
//#![feature(proc_macro)]
|
||||||
//#![deny(rust_2018_idioms)]
|
//#![deny(rust_2018_idioms)]
|
||||||
|
|
||||||
//use clap::{ArgMatches, Arg, App, SubCommand};
|
//use log::info; //substituted with tracing
|
||||||
use clap::ArgMatches;
|
|
||||||
|
|
||||||
use serde::Deserialize;
|
|
||||||
use std::{env, process};
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use locales::t;
|
use locales::t;
|
||||||
|
use serde::Deserialize;
|
||||||
|
//use std::{env, process};
|
||||||
|
use std::env;
|
||||||
//use tracing::{debug, info, instrument, span, Level};
|
//use tracing::{debug, info, instrument, span, Level};
|
||||||
//use tracing::{debug, info, instrument, Level, Metadata, span::{Id, Attributes, Record}, trace};
|
//use tracing::{debug, info, instrument, Level, Metadata, span::{Id, Attributes, Record}, trace};
|
||||||
//use tracing::{debug, instrument, span::*, trace, Level};
|
//use tracing::{debug, instrument, span::*, trace, Level};
|
||||||
use tracing::{debug, instrument, trace, Level};
|
use tracing::{debug, trace, Level};
|
||||||
|
|
||||||
//use diesel::prelude::*;
|
//use diesel::prelude::*;
|
||||||
//use diesel::sqlite::SqliteConnection;
|
//use diesel::sqlite::SqliteConnection;
|
||||||
|
|
||||||
|
mod parse_args;
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct Environment {
|
struct Environment {
|
||||||
test_lang: String,
|
test_lang: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
struct Args {
|
|
||||||
my_conifg: String,
|
|
||||||
verbose: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AdvoTrackerSubscriber;
|
pub struct AdvoTrackerSubscriber;
|
||||||
|
|
||||||
impl tracing::Subscriber for AdvoTrackerSubscriber{
|
impl tracing::Subscriber for AdvoTrackerSubscriber{
|
||||||
@@ -49,7 +44,6 @@ impl tracing::Subscriber for AdvoTrackerSubscriber{
|
|||||||
impl AdvoTrackerSubscriber {
|
impl AdvoTrackerSubscriber {
|
||||||
fn new() -> Self { AdvoTrackerSubscriber }
|
fn new() -> Self { AdvoTrackerSubscriber }
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
#[instrument]
|
#[instrument]
|
||||||
fn run_subcommand(matches: &ArgMatches) -> Result<(), String> {
|
fn run_subcommand(matches: &ArgMatches) -> Result<(), String> {
|
||||||
@@ -80,40 +74,37 @@ fn run_test(matches: &ArgMatches) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
//#[clap::main]
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
//#[runtime::main]
|
use parse_args::parse_args;
|
||||||
//[context_attribute::context]
|
|
||||||
//#[throw]
|
|
||||||
//fn main(args: Args) throw {
|
|
||||||
fn main() -> std::io::Result<()> {
|
|
||||||
//initialize tracing eco-system
|
|
||||||
use tracing_subscriber::fmt;
|
use tracing_subscriber::fmt;
|
||||||
|
use viperus::Viperus;
|
||||||
|
|
||||||
//let advotracker_subscriber = AdvoTrackerSubscriber::new();
|
//let advotracker_subscriber = AdvoTrackerSubscriber::new();
|
||||||
//tracing::subscriber::set_global_default(advotracker_subscriber)
|
//tracing::subscriber::set_global_default(advotracker_subscriber)
|
||||||
// .expect("setting tracing default failed");
|
// .expect("setting tracing default failed");
|
||||||
|
|
||||||
|
// initialize the tracing subsystem
|
||||||
let span = tracing::span!(Level::TRACE, "advotracker_main");
|
let span = tracing::span!(Level::TRACE, "advotracker_main");
|
||||||
let _enter = span.enter();
|
let _enter = span.enter();
|
||||||
let subscriber = fmt::Subscriber::builder()
|
let subscriber = fmt::Subscriber::builder()
|
||||||
.with_env_filter("advotracker=trace")
|
.with_env_filter("advotracker=trace")
|
||||||
.finish();
|
.finish();
|
||||||
|
|
||||||
|
// initialize logger
|
||||||
|
// TODO: exchange with tracing!
|
||||||
|
//env_logger::init();
|
||||||
|
//info!("Commencing the proxy!");
|
||||||
|
|
||||||
tracing::subscriber::with_default(subscriber, || {
|
tracing::subscriber::with_default(subscriber, || {
|
||||||
// get system environment
|
// get system environment
|
||||||
let mut lang = env::var("LANG").unwrap_or("en".to_string());
|
let mut lang = env::var("LANG").unwrap_or("en".to_string());
|
||||||
let mut res = t!("parse.environment", lang);
|
let mut res = t!("parse.environment", lang);
|
||||||
let mut state = t!("state.started", lang);
|
let mut state = t!("state.started", lang);
|
||||||
debug!(
|
trace!(target: "advotracker", message = ?res, state = ?state);
|
||||||
message = ?res,
|
//debug!(message = ?res, state = ?state);
|
||||||
state = ?state
|
trace!(target: "advotracker", environment = "system", lang = ?lang);
|
||||||
);
|
|
||||||
trace!(
|
|
||||||
target: "advotracker",
|
|
||||||
environment = "system",
|
|
||||||
lang = ?lang
|
|
||||||
);
|
|
||||||
|
|
||||||
// get testing environment (.env)
|
// get testing environment (.env)
|
||||||
dotenv().ok();
|
dotenv().ok();
|
||||||
@@ -121,90 +112,54 @@ fn main() -> std::io::Result<()> {
|
|||||||
Ok(environment) => {
|
Ok(environment) => {
|
||||||
if environment.test_lang != lang { lang = environment.test_lang; }
|
if environment.test_lang != lang { lang = environment.test_lang; }
|
||||||
},
|
},
|
||||||
//Err(e) => { debug!( message = "No Lang", "{}", e); }
|
|
||||||
Err(e) => { debug!(target: "advotracker", "{}", e); }
|
Err(e) => { debug!(target: "advotracker", "{}", e); }
|
||||||
//error = "{:#?}", e"
|
|
||||||
}
|
}
|
||||||
res = t!("parse.environment", lang);
|
res = t!("parse.environment", lang);
|
||||||
trace!(
|
trace!(target: "advotracker", environment = "envy", lang = ?lang);
|
||||||
target: "advotracker",
|
|
||||||
environment = "envy",
|
|
||||||
lang = ?lang
|
|
||||||
);
|
|
||||||
//info!(target: "advotracker", "{}", res);
|
|
||||||
//info!(target: "advotracker", "{}", lang);
|
|
||||||
state = t!("state.finished", lang);
|
state = t!("state.finished", lang);
|
||||||
debug!(
|
trace!(target: "advotracker", message = ?res, state = ?state);
|
||||||
message = ?res,
|
//debug!(message = ?res, state = ?state);
|
||||||
state = ?state
|
|
||||||
);
|
|
||||||
|
|
||||||
// handle commandline arguments with clap (relative path to cli.yml)
|
// initialize viperus structure
|
||||||
|
let mut v = Viperus::new();
|
||||||
|
|
||||||
|
// parse commandline arguments
|
||||||
res = t!("parse.arguments", lang);
|
res = t!("parse.arguments", lang);
|
||||||
state = t!("state.started", lang);
|
state = t!("state.started", lang);
|
||||||
|
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||||
//info!(target: "advotracker", "{}", res);
|
//info!(target: "advotracker", "{}", res);
|
||||||
debug!(
|
//debug!(message = ?res, state = ?state);
|
||||||
message = ?res,
|
//info!(target: "advotracker", "parsing commandline args");
|
||||||
state = ?state,
|
let _ = parse_args(&mut v);
|
||||||
);
|
state = t!("state.finished", lang);
|
||||||
trace!(
|
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||||
target: "advotracker",
|
//trace!(target: "Viperus", "Config results: {:?}", v);
|
||||||
process = ?res,
|
|
||||||
state = ?state
|
|
||||||
);
|
|
||||||
|
|
||||||
let yaml = clap::load_yaml!("cli.yml");
|
|
||||||
let matches = clap::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("default.conf");
|
|
||||||
let dbdriver = matches.value_of("dbdriver").unwrap_or("sqlite3");
|
|
||||||
let verbose = matches.occurrences_of("verbose");
|
|
||||||
|
|
||||||
if verbose > 0 {
|
|
||||||
let mut res_name = t!("config.name.configfile", lang);
|
|
||||||
println!("{}: {}", res_name, config);
|
|
||||||
res_name = t!("config.name.dbdriver", lang);
|
|
||||||
println!("{}: {}", res_name, dbdriver);
|
|
||||||
res_name = t!("config.name.verbositylevel", lang);
|
|
||||||
println!("{}: {}", res_name, matches.occurrences_of("verbose"));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/*
|
||||||
if verbose > 1 {
|
if verbose > 1 {
|
||||||
for (key, value) in env::vars() {
|
for (key, value) in env::vars() {
|
||||||
println!("{}={}", key, value);
|
println!("{}={}", key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
state = t!("state.finished", lang);
|
*/
|
||||||
trace!(
|
|
||||||
target: "advotracker",
|
|
||||||
process = ?res,
|
|
||||||
state = ?state
|
|
||||||
);
|
|
||||||
debug!(
|
|
||||||
message = ?res,
|
|
||||||
state = ?state
|
|
||||||
);
|
|
||||||
|
|
||||||
|
//state = t!("state.finished", lang);
|
||||||
|
//trace!(target: "advotracker", process = ?res, state = ?state);
|
||||||
|
//debug!(message = ?res, state = ?state);
|
||||||
|
|
||||||
|
/*
|
||||||
// handle subcommands
|
// handle subcommands
|
||||||
if let Err(e) = run_subcommand(&matches) {
|
if let Err(e) = run_subcommand(&matches) {
|
||||||
println!("Subcommand error: {}", e);
|
println!("Subcommand error: {}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Starting the program logic
|
// Starting the program logic
|
||||||
res = t!("main.started", lang);
|
res = t!("main.started", lang);
|
||||||
|
state = t!("state.started", lang);
|
||||||
//info!(target: "advotracker", "{}", res);
|
//info!(target: "advotracker", "{}", res);
|
||||||
trace!(
|
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||||
target: "advotracker",
|
|
||||||
process = ?res,
|
|
||||||
state = ?state
|
|
||||||
);
|
|
||||||
|
|
||||||
//use advotracker_backend::*;
|
//use advotracker_backend::*;
|
||||||
//use advotracker_backend::schema::users::dsl::*;
|
//use advotracker_backend::schema::users::dsl::*;
|
||||||
@@ -240,17 +195,12 @@ fn main() -> std::io::Result<()> {
|
|||||||
println!("{}", user.alias);
|
println!("{}", user.alias);
|
||||||
println!("{}", user.email);
|
println!("{}", user.email);
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
state = t!("state.finished", lang);
|
state = t!("state.finished", lang);
|
||||||
res = t!("main.finished", lang);
|
res = t!("main.finished", lang);
|
||||||
//info!(target: "advotracker", "{}", res);
|
//info!(target: "advotracker", "{}", res);
|
||||||
trace!(
|
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||||
target: "advotracker",
|
|
||||||
process = ?res,
|
|
||||||
state = ?state
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user