From 438fcd66a6f60c694ea40b5f661e9106c561139e Mon Sep 17 00:00:00 2001 From: "Hatter Jiang@Pixelbook" Date: Thu, 8 Aug 2019 00:41:57 +0800 Subject: [PATCH] add verbose --- src/http.rs | 16 ++++++++++++++++ src/misc.rs | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/src/http.rs b/src/http.rs index 8bb7f53..dffcd2a 100644 --- a/src/http.rs +++ b/src/http.rs @@ -4,19 +4,35 @@ use std::{ use rust_util::{ util_io::copy_io, + util_msg::{ + print_message, + MessageType, + }, XResult, }; +use super::misc::*; + pub fn download_url(url: &str, dest: &mut File) -> XResult<()> { + let verbose = is_verbose(); + if verbose { + print_message(MessageType::INFO, &format!("Download URL: {}", url)); + } let mut response = reqwest::get(url)?; let header_content_length: i64 = match response.headers().get("content-length") { None => -1, Some(len_value) => len_value.to_str().unwrap().parse::().unwrap(), }; + if verbose { + print_message(MessageType::INFO, &format!("Content-Length: {}", header_content_length)); + } copy_io(&mut response, dest, header_content_length)?; Ok(()) } pub fn get_url(url: &str) -> XResult { + if is_verbose() { + print_message(MessageType::INFO, &format!("Get URL: {}", url)); + } Ok(reqwest::get(url)?.text()?) } diff --git a/src/misc.rs b/src/misc.rs index 24af9aa..ef58f30 100644 --- a/src/misc.rs +++ b/src/misc.rs @@ -1,4 +1,6 @@ +use std::env; + pub fn print_usage() { print!(r#" buildj ::: - print this message @@ -12,3 +14,9 @@ buildj - run build, run assigned "#); } +pub fn is_verbose() -> bool { + match env::var("BUILDJ_VERBOSE") { + Err(_) => false, + Ok(v) => (v == "TRUE" || v == "true" || v == "1"), + } +}