extern crate log;
extern crate clap;
extern crate changelog;
extern crate env_logger;
use std::fs::File;
use clap::{Arg, App};
use std::error::Error;
use std::process::exit;
use env_logger::LogBuilder;
use log::{LogRecord, LogLevelFilter};
fn main() {
let cli = App::new(env!("CARGO_PKG_NAME"))
.author(env!("CARGO_PKG_AUTHORS"))
.about(env!("CARGO_PKG_DESCRIPTION"))
.version(&format!("(v{})", env!("CARGO_PKG_VERSION"))[..])
.arg(Arg::with_name("revision-range").multiple(true).help(
"The revision range, defaults to HEAD...<last-tag>",
))
.arg(
Arg::with_name("config")
.short("c")
.long("config")
.takes_value(true)
.value_name("FILE")
.validator(|s| valid_path(&s))
.help("Configuration file"),
)
.arg(
Arg::with_name("debug")
.short("d")
.long("debug")
.multiple(true)
.help("Prints debug logs"),
)
.get_matches();
init_logging(cli.occurrences_of("debug"));
let filename = changelog::config::find_file(cli.value_of("config"));
let result = match changelog::config::from(&filename) {
Ok(c) => changelog::tool::run(&c, cli.values_of_lossy("revision-range")),
_ => -1,
};
exit(result);
}
fn init_logging(verbosity: u64) {
let level = match verbosity {
1 => LogLevelFilter::Info,
2 => LogLevelFilter::Debug,
n if n > 2 => LogLevelFilter::Trace,
_ => LogLevelFilter::Warn,
};
let format = |r: &LogRecord| format!("{}: {}", r.level(), r.args());
let mut builder = LogBuilder::new();
builder.format(format).filter(None, level);
builder.init().unwrap();
}
fn valid_path(path: &str) -> Result<(), String> {
File::open(path).map(|_| ()).map_err(|e| {
let mut reason = String::from("Invalid file: ");
reason.push_str(path);
reason.push_str(" Reason: ");
reason.push_str(e.description());
reason
})
}