Use log and beautylog

This commit is contained in:
2019-02-27 10:57:01 +01:00
parent a2767ee117
commit f7e8a96550
7 changed files with 66 additions and 70 deletions

View File

@@ -98,10 +98,7 @@ impl Cache {
1 => Ok(matches[0].clone()),
len => {
eprintln!(
"{}",
"info: multiple entries found, please select one".bold()
);
info!("{}", "multiple entries found, please select one");
for (i, j) in matches.iter().enumerate() {
eprintln!(

View File

@@ -1,8 +1,9 @@
#[macro_use]
extern crate log;
use std::path::PathBuf;
use std::process::exit;
use colored::*;
use gclone::git::{clone, parse_url, GCLONE_PATH};
use gclone::{first_arg, Cache, Result};
@@ -11,6 +12,10 @@ fn help() {
}
fn main_result() -> Result<()> {
// Init beautylog
beautylog::init(log::LevelFilter::Info).ok();
// Parse args
let url = first_arg()?;
@@ -33,9 +38,9 @@ fn main_result() -> Result<()> {
path.push(&repo);
// Clone repository
eprintln!("{} {} {}{}", "info:".bold(), "cloning", url, "...");
clone(&url, &path.display().to_string())?;
eprintln!("{} {}", "info:".bold(), "done!");
info!("{} {}{}", "cloning", url, "...");
clone(&url, &path)?;
info!("{}", "done!");
// Append to cache
let mut cache = Cache::read()?;
@@ -47,7 +52,7 @@ fn main_result() -> Result<()> {
fn main() {
if let Err(e) = main_result() {
eprintln!("{}", e);
error!("{}", e);
exit(1);
}
}

View File

@@ -16,7 +16,7 @@ lazy_static! {
Ok(d) => d,
Err(e) => {
let e: Error = e.into();
eprintln!("{}", e);
error!("couldn't read environnement variable GCLONE_PATH: {}", e);
exit(1);
},
}

View File

@@ -12,11 +12,12 @@
#![warn(missing_docs)]
#[macro_use]
extern crate log;
use std::io;
use std::{env, error, fmt, num, result};
use colored::*;
mod cache;
pub use cache::Cache;
@@ -71,68 +72,34 @@ impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::NoGclonePath(e) => write!(
fmt,
"{} {} {}",
"error:".bold().red(),
"couldn't read environment variable GCLONE_PATH".bold(),
e
fmt, "couldn't read environment variable GCLONE_PATH {}", e
),
Error::IoError(e) => write!(
fmt,
"{} {} {}",
"error:".bold().red(),
"couldn't read or write cache:".bold(),
e
fmt, "couldn't read or write cache: {}", e
),
Error::GitUrlParseError => write!(
fmt,
"{} {}",
"error:".bold().red(),
"couldn't guess server, owner and repo from url".bold()
fmt, "couldn't guess server, owner and repo from url"
),
Error::GitCloneError(Some(e)) => write!(
fmt,
"{} {} {}",
"error:".bold().red(),
"couldn't clone git repository:".bold(),
e
fmt, "couldn't clone git repository: {}", e
),
Error::GitCloneError(None) => write!(
fmt,
"{} {}",
"error:".bold().red(),
"couldn't clone git repository".bold()
fmt, "couldn't clone git repository"
),
Error::PathAlreadyExists => write!(
fmt,
"{} {}",
"error:".bold().red(),
"the path corresponding to the repository already exists".bold()
fmt, "the path corresponding to the repository already exists"
),
Error::WrongArgument => write!(
fmt,
"{} {}",
"error:".bold().red(),
"this program expects exactly one argument".bold()
fmt, "this program expects exactly one argument"
),
Error::ParseIntError(e) => write!(
fmt,
"{} {} {}",
"error:".bold().red(),
"couldn't parse integer:".bold(),
e
fmt, "couldn't parse integer: {}", e
),
Error::OutsideRange => write!(
fmt,
"{} {}",
"error:".bold().red(),
"integer is outside the range".bold()
fmt, "integer is outside the range"
),
Error::NoSuchDirectory => write!(
fmt,
"{} {}",
"error:".bold().red(),
"no such directory was found".bold()
fmt, "no such directory was found"
),
}
}

View File

@@ -1,12 +1,14 @@
#[macro_use]
extern crate log;
use std::process::exit;
use colored::*;
use gclone::{first_arg, Cache, Error, Result};
fn main() {
beautylog::init(log::LevelFilter::Info).ok();
if let Err(e) = main_result() {
eprintln!("{}", e);
error!("{}", e);
exit(1);
}
}
@@ -28,11 +30,7 @@ fn main_with_cache(request: &str, regen: bool) -> Result<()> {
let only_match = match cache.find_interactive(&request) {
Err(Error::NoSuchDirectory) if regen && !just_generated => {
eprintln!(
"{} {}",
"warning:".bold().yellow(),
"directory not found, regenerating cache...".bold()
);
warn!("directory not found, regenerating cache...");
let cache = Cache::generate();
cache.write()?;
return main_with_cache(request, false);