Use log and beautylog

This commit is contained in:
Thomas Forgione 2019-02-27 10:57:01 +01:00
parent a2767ee117
commit f7e8a96550
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
7 changed files with 66 additions and 70 deletions

35
Cargo.lock generated
View File

@ -1,25 +1,49 @@
[[package]]
name = "beautylog"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "colored" name = "colored"
version = "1.7.0" version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "gclone" name = "gclone"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"beautylog 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.2.0" version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "log"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "same-file" name = "same-file"
version = "1.0.4" version = "1.0.4"
@ -66,8 +90,11 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata] [metadata]
"checksum beautylog 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1457a99b95a92f1de6f3a1fe08324f200cd68e0f43b13d4289752ead6ed531a2"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e9a455e156a4271e12fd0246238c380b1e223e3736663c7a18ed8b6362028a9" "checksum colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e9a455e156a4271e12fd0246238c380b1e223e3736663c7a18ed8b6362028a9"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14"
"checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6"
"checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267" "checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267"
"checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1" "checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1"
"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0"

View File

@ -8,6 +8,8 @@ edition = "2018"
colored = "1.7.0" colored = "1.7.0"
walkdir = "2.2.7" walkdir = "2.2.7"
lazy_static = "1.2.0" lazy_static = "1.2.0"
log = "0.4.6"
beautylog = "0.1.0"
[[bin]] [[bin]]
name = "gclone" name = "gclone"

View File

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

View File

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

View File

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

View File

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

View File

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