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:
294
src/main.rs
294
src/main.rs
@@ -8,56 +8,50 @@
|
||||
//#![feature(proc_macro)]
|
||||
//#![deny(rust_2018_idioms)]
|
||||
|
||||
//use clap::{ArgMatches, Arg, App, SubCommand};
|
||||
use clap::ArgMatches;
|
||||
|
||||
use serde::Deserialize;
|
||||
use std::{env, process};
|
||||
//use log::info; //substituted with tracing
|
||||
use dotenv::dotenv;
|
||||
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, Level, Metadata, span::{Id, Attributes, Record}, trace};
|
||||
//use tracing::{debug, instrument, span::*, trace, Level};
|
||||
use tracing::{debug, instrument, trace, Level};
|
||||
use tracing::{debug, trace, Level};
|
||||
|
||||
//use diesel::prelude::*;
|
||||
//use diesel::sqlite::SqliteConnection;
|
||||
|
||||
mod parse_args;
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Environment {
|
||||
test_lang: String,
|
||||
}
|
||||
|
||||
/*
|
||||
struct Args {
|
||||
my_conifg: String,
|
||||
verbose: String,
|
||||
}
|
||||
|
||||
pub struct AdvoTrackerSubscriber;
|
||||
|
||||
impl tracing::Subscriber for AdvoTrackerSubscriber{
|
||||
fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) }
|
||||
fn record(&self, _: &Id, _: &Record) {}
|
||||
fn event(&self, _: &tracing::Event) {}
|
||||
fn record_follows_from(&self, _: &Id, _: &Id) {}
|
||||
fn enabled(&self, _: &Metadata) -> bool { false }
|
||||
fn enter(&self, _: &Id) {}
|
||||
fn exit(&self, _: &Id) {}
|
||||
fn new_span(&self, _: &Attributes) -> Id { Id::from_u64(0) }
|
||||
fn record(&self, _: &Id, _: &Record) {}
|
||||
fn event(&self, _: &tracing::Event) {}
|
||||
fn record_follows_from(&self, _: &Id, _: &Id) {}
|
||||
fn enabled(&self, _: &Metadata) -> bool { false }
|
||||
fn enter(&self, _: &Id) {}
|
||||
fn exit(&self, _: &Id) {}
|
||||
}
|
||||
|
||||
impl AdvoTrackerSubscriber {
|
||||
fn new() -> Self { AdvoTrackerSubscriber }
|
||||
fn new() -> Self { AdvoTrackerSubscriber }
|
||||
}
|
||||
*/
|
||||
|
||||
#[instrument]
|
||||
fn run_subcommand(matches: &ArgMatches) -> Result<(), String> {
|
||||
//info!("inside run_subcommand");
|
||||
match matches.subcommand() {
|
||||
("config", Some(cmd)) => run_config(cmd),
|
||||
("test", Some(cmd)) => run_test(cmd),
|
||||
_ => Ok(()),
|
||||
("config", Some(cmd)) => run_config(cmd),
|
||||
("test", Some(cmd)) => run_test(cmd),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,185 +66,141 @@ fn run_test(matches: &ArgMatches) -> Result<(), String> {
|
||||
//info!("inside run_test");
|
||||
|
||||
if let Some(matches) = matches.subcommand_matches("test") {
|
||||
if matches.is_present("debug") {
|
||||
println!("test: Printing debug info...");
|
||||
} else {
|
||||
println!("test: Printing normally...");
|
||||
}
|
||||
if matches.is_present("debug") {
|
||||
println!("test: Printing debug info...");
|
||||
} else {
|
||||
println!("test: Printing normally...");
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
*/
|
||||
|
||||
//#[clap::main]
|
||||
//#[runtime::main]
|
||||
//[context_attribute::context]
|
||||
//#[throw]
|
||||
//fn main(args: Args) throw {
|
||||
fn main() -> std::io::Result<()> {
|
||||
//initialize tracing eco-system
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
use parse_args::parse_args;
|
||||
use tracing_subscriber::fmt;
|
||||
use viperus::Viperus;
|
||||
|
||||
//let advotracker_subscriber = AdvoTrackerSubscriber::new();
|
||||
//tracing::subscriber::set_global_default(advotracker_subscriber)
|
||||
// .expect("setting tracing default failed");
|
||||
|
||||
// initialize the tracing subsystem
|
||||
let span = tracing::span!(Level::TRACE, "advotracker_main");
|
||||
let _enter = span.enter();
|
||||
let subscriber = fmt::Subscriber::builder()
|
||||
.with_env_filter("advotracker=trace")
|
||||
.finish();
|
||||
.with_env_filter("advotracker=trace")
|
||||
.finish();
|
||||
|
||||
// initialize logger
|
||||
// TODO: exchange with tracing!
|
||||
//env_logger::init();
|
||||
//info!("Commencing the proxy!");
|
||||
|
||||
tracing::subscriber::with_default(subscriber, || {
|
||||
// get system environment
|
||||
let mut lang = env::var("LANG").unwrap_or("en".to_string());
|
||||
let mut res = t!("parse.environment", lang);
|
||||
let mut state = t!("state.started", lang);
|
||||
debug!(
|
||||
message = ?res,
|
||||
state = ?state
|
||||
);
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
environment = "system",
|
||||
lang = ?lang
|
||||
);
|
||||
// get system environment
|
||||
let mut lang = env::var("LANG").unwrap_or("en".to_string());
|
||||
let mut res = t!("parse.environment", lang);
|
||||
let mut state = t!("state.started", lang);
|
||||
trace!(target: "advotracker", message = ?res, state = ?state);
|
||||
//debug!(message = ?res, state = ?state);
|
||||
trace!(target: "advotracker", environment = "system", lang = ?lang);
|
||||
|
||||
// get testing environment (.env)
|
||||
dotenv().ok();
|
||||
match envy::from_env::<Environment>() {
|
||||
Ok(environment) => {
|
||||
if environment.test_lang != lang { lang = environment.test_lang; }
|
||||
},
|
||||
//Err(e) => { debug!( message = "No Lang", "{}", e); }
|
||||
Err(e) => { debug!(target: "advotracker", "{}", e); }
|
||||
//error = "{:#?}", e"
|
||||
}
|
||||
res = t!("parse.environment", lang);
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
environment = "envy",
|
||||
lang = ?lang
|
||||
);
|
||||
//info!(target: "advotracker", "{}", res);
|
||||
//info!(target: "advotracker", "{}", lang);
|
||||
state = t!("state.finished", lang);
|
||||
debug!(
|
||||
message = ?res,
|
||||
state = ?state
|
||||
);
|
||||
// get testing environment (.env)
|
||||
dotenv().ok();
|
||||
match envy::from_env::<Environment>() {
|
||||
Ok(environment) => {
|
||||
if environment.test_lang != lang { lang = environment.test_lang; }
|
||||
},
|
||||
Err(e) => { debug!(target: "advotracker", "{}", e); }
|
||||
}
|
||||
res = t!("parse.environment", lang);
|
||||
trace!(target: "advotracker", environment = "envy", lang = ?lang);
|
||||
state = t!("state.finished", lang);
|
||||
trace!(target: "advotracker", message = ?res, state = ?state);
|
||||
//debug!(message = ?res, state = ?state);
|
||||
|
||||
// handle commandline arguments with clap (relative path to cli.yml)
|
||||
res = t!("parse.arguments", lang);
|
||||
state = t!("state.started", lang);
|
||||
//info!(target: "advotracker", "{}", res);
|
||||
debug!(
|
||||
message = ?res,
|
||||
state = ?state,
|
||||
);
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
process = ?res,
|
||||
state = ?state
|
||||
);
|
||||
// initialize viperus structure
|
||||
let mut v = Viperus::new();
|
||||
|
||||
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();
|
||||
// parse commandline arguments
|
||||
res = t!("parse.arguments", lang);
|
||||
state = t!("state.started", lang);
|
||||
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||
//info!(target: "advotracker", "{}", res);
|
||||
//debug!(message = ?res, state = ?state);
|
||||
//info!(target: "advotracker", "parsing commandline args");
|
||||
let _ = parse_args(&mut v);
|
||||
state = t!("state.finished", lang);
|
||||
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||
//trace!(target: "Viperus", "Config results: {:?}", v);
|
||||
|
||||
// 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 > 1 {
|
||||
for (key, value) in env::vars() {
|
||||
println!("{}={}", key, value);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
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"));
|
||||
}
|
||||
//state = t!("state.finished", lang);
|
||||
//trace!(target: "advotracker", process = ?res, state = ?state);
|
||||
//debug!(message = ?res, state = ?state);
|
||||
|
||||
if verbose > 1 {
|
||||
for (key, value) in env::vars() {
|
||||
println!("{}={}", key, value);
|
||||
}
|
||||
}
|
||||
state = t!("state.finished", lang);
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
process = ?res,
|
||||
state = ?state
|
||||
);
|
||||
debug!(
|
||||
message = ?res,
|
||||
state = ?state
|
||||
);
|
||||
/*
|
||||
// handle subcommands
|
||||
if let Err(e) = run_subcommand(&matches) {
|
||||
println!("Subcommand error: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
*/
|
||||
|
||||
// handle subcommands
|
||||
if let Err(e) = run_subcommand(&matches) {
|
||||
println!("Subcommand error: {}", e);
|
||||
process::exit(1);
|
||||
}
|
||||
// Starting the program logic
|
||||
res = t!("main.started", lang);
|
||||
state = t!("state.started", lang);
|
||||
//info!(target: "advotracker", "{}", res);
|
||||
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||
|
||||
// Starting the program logic
|
||||
res = t!("main.started", lang);
|
||||
//info!(target: "advotracker", "{}", res);
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
process = ?res,
|
||||
state = ?state
|
||||
);
|
||||
//use advotracker_backend::*;
|
||||
//use advotracker_backend::schema::users::dsl::*;
|
||||
|
||||
//use advotracker_backend::*;
|
||||
//use advotracker_backend::schema::users::dsl::*;
|
||||
/*
|
||||
let connection = establish_connection();
|
||||
|
||||
/*
|
||||
let connection = establish_connection();
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
process = "Sqlite3",
|
||||
status = "connected",
|
||||
);
|
||||
*/
|
||||
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
process = "Sqlite3",
|
||||
status = "connected",
|
||||
);
|
||||
*/
|
||||
|
||||
/*
|
||||
//User::table.load(&connection);
|
||||
//user::belonging_to(users).load(&connection);
|
||||
use advotracker_backend::models::*;
|
||||
/*
|
||||
//User::table.load(&connection);
|
||||
//user::belonging_to(users).load(&connection);
|
||||
use advotracker_backend::models::*;
|
||||
|
||||
|
||||
let results = users
|
||||
//.filter(published.eq(true))
|
||||
.limit(5)
|
||||
.load::<User>(&connection)
|
||||
.expect("Error loading users");
|
||||
let results = users
|
||||
//.filter(published.eq(true))
|
||||
.limit(5)
|
||||
.load::<User>(&connection)
|
||||
.expect("Error loading users");
|
||||
|
||||
println!("Displaying {} users", results.len());
|
||||
for user in results {
|
||||
println!("{}", user.user_id);
|
||||
println!("----------\n");
|
||||
println!("{}", user.first_name);
|
||||
println!("{}", user.last_name);
|
||||
println!("{}", user.alias);
|
||||
println!("{}", user.email);
|
||||
}
|
||||
println!("Displaying {} users", results.len());
|
||||
for user in results {
|
||||
println!("{}", user.user_id);
|
||||
println!("----------\n");
|
||||
println!("{}", user.first_name);
|
||||
println!("{}", user.last_name);
|
||||
println!("{}", user.alias);
|
||||
println!("{}", user.email);
|
||||
}
|
||||
*/
|
||||
|
||||
state = t!("state.finished", lang);
|
||||
res = t!("main.finished", lang);
|
||||
//info!(target: "advotracker", "{}", res);
|
||||
trace!(
|
||||
target: "advotracker",
|
||||
process = ?res,
|
||||
state = ?state
|
||||
);
|
||||
*/
|
||||
|
||||
state = t!("state.finished", lang);
|
||||
res = t!("main.finished", lang);
|
||||
//info!(target: "advotracker", "{}", res);
|
||||
trace!(target: "advotracker", process = ?res, state = ?state);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in New Issue
Block a user