85 lines
2.4 KiB
Rust
85 lines
2.4 KiB
Rust
#[macro_use] extern crate log;
|
|
//#[macro_use] extern crate lazy_static;
|
|
use log::LevelFilter;
|
|
use clap::{ App, ArgMatches };
|
|
|
|
mod cmd;
|
|
mod cmd_default;
|
|
mod cmd_new;
|
|
mod git;
|
|
mod script;
|
|
|
|
use cmd::{ Command, CommandError };
|
|
use cmd_default::CommandDefault;
|
|
use cmd_new::CommandNew;
|
|
|
|
#[macro_export] macro_rules! information {
|
|
($($arg:tt)+) => ( crate::print_in(&format!($($arg)+)); )
|
|
}
|
|
#[macro_export] macro_rules! success {
|
|
($($arg:tt)+) => ( crate::print_ok(&format!($($arg)+)); )
|
|
}
|
|
#[macro_export] macro_rules! warning {
|
|
($($arg:tt)+) => ( crate::print_wa(&format!($($arg)+)); )
|
|
}
|
|
|
|
const GREEN: &str = "\x1B[92m";
|
|
const YELLOW: &str = "\x1B[93m";
|
|
const BOLD: &str = "\x1B[1m";
|
|
const END: &str = "\x1B[0m";
|
|
|
|
#[inline]
|
|
fn print_in (msg: &str) {
|
|
println!("{b}[INFO]{e} {m}", b = BOLD, e = END, m = msg);
|
|
}
|
|
#[inline]
|
|
fn print_ok (msg: &str) {
|
|
println!("{g}{b}[ OK ]{e} {g}{m}{e}", g = GREEN, b = BOLD, e = END, m = msg);
|
|
}
|
|
#[inline]
|
|
fn print_wa (msg: &str) {
|
|
println!("{g}{b}[WARN]{e} {g}{m}{e}", g = YELLOW, b = BOLD, e = END, m = msg);
|
|
}
|
|
|
|
fn main() -> CommandError {
|
|
let commands: Vec<Box<dyn Command>> = vec![
|
|
Box::new(CommandNew{}),
|
|
];
|
|
let mut app = App::new(env!("CARGO_PKG_NAME"))
|
|
.version(env!("CARGO_PKG_VERSION"))
|
|
.about(env!("CARGO_PKG_DESCRIPTION"));
|
|
app = CommandDefault::process_command(app);
|
|
for command in &commands {
|
|
app = app.subcommand(command.subcommand());
|
|
}
|
|
|
|
let matches = app.get_matches();
|
|
init_log(&matches);
|
|
info!("rust-script-tool started");
|
|
|
|
for command in &commands {
|
|
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
|
|
info!("matched subcommand: {}", command.name());
|
|
return command.run(&matches, sub_cmd_matches);
|
|
}
|
|
}
|
|
|
|
info!("matched default subcommand");
|
|
CommandDefault::run(&matches)
|
|
}
|
|
|
|
fn init_log(arg_matches: &ArgMatches) {
|
|
let verbose_count = arg_matches.occurrences_of("verbose");
|
|
if verbose_count == 0 {
|
|
pretty_env_logger::init();
|
|
} else {
|
|
let mut builder = pretty_env_logger::formatted_builder();
|
|
match verbose_count {
|
|
1 => builder.filter_level(LevelFilter::Warn),
|
|
2 => builder.filter_level(LevelFilter::Info),
|
|
3 => builder.filter_level(LevelFilter::Debug),
|
|
_ => builder.filter_level(LevelFilter::Trace),
|
|
};
|
|
builder.init();
|
|
}
|
|
} |