Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Jan 5, 2022
2 parents d4e8858 + 47329a0 commit 58348e8
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 141 deletions.
75 changes: 5 additions & 70 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate crossbeam_channel as channel;
use self_update::cargo_crate_version;

use std::borrow::ToOwned;
use std::env;
Expand All @@ -8,9 +7,9 @@ use std::io;
use std::process;
use std::time::Instant;

use serde::Deserialize;

use docopt::Docopt;
use log::{error, info, log_enabled, Level};
use serde::Deserialize;

#[cfg(feature = "python")]
use pyo3::Python;
Expand Down Expand Up @@ -122,77 +121,13 @@ struct Args {
flag_skip_update_check: bool,
}

fn qsv_update(verbose: bool) -> Result<(), Box<dyn ::std::error::Error>> {
if env::var("QSV_NO_UPDATE").is_ok() {
return Ok(());
}

let curr_version = cargo_crate_version!();
let releases = self_update::backends::github::ReleaseList::configure()
.repo_owner("jqnatividad")
.repo_name("qsv")
.build()?
.fetch()?;
let latest_release = &releases[0].version;
if latest_release > &curr_version.to_string() {
println!(
"Update {} available. Current version is {}.",
latest_release, curr_version
);
let status = self_update::backends::github::Update::configure()
.repo_owner("jqnatividad")
.repo_name("qsv")
.bin_name("qsv")
.show_download_progress(true)
.show_output(verbose)
.no_confirm(false)
.current_version(curr_version)
.build()?
.update()?;
let exe_full_path = format!("{:?}", std::env::current_exe().unwrap());
if verbose {
println!(
"Update successful for {}: `{}`!",
exe_full_path,
status.version()
);
}
}
Ok(())
}

use flexi_logger::{Cleanup, Criterion, FileSpec, Logger, Naming};
use log::{error, info, log_enabled, Level};

fn init_logger() {
let qsv_log_env = env::var("QSV_LOG_LEVEL").unwrap_or_else(|_| "off".to_string());
let qsv_log_dir = env::var("QSV_LOG_DIR").unwrap_or_else(|_| ".".to_string());

Logger::try_with_env_or_str(qsv_log_env)
.unwrap()
.log_to_file(
FileSpec::default()
.directory(qsv_log_dir)
.suppress_timestamp(),
)
.format_for_files(flexi_logger::detailed_format)
.o_append(true)
.rotate(
Criterion::Size(1_000_000),
Naming::Numbers,
Cleanup::KeepLogAndCompressedFiles(10, 100),
)
.start()
.unwrap();
}

#[cfg(feature = "python")]
fn check_python() -> bool {
Python::with_gil(|py| py.version_info() >= (3, 7))
}

fn main() {
init_logger();
util::init_logger();

#[cfg(feature = "python")]
if !check_python() {
Expand Down Expand Up @@ -226,7 +161,7 @@ fn main() {
return;
}
if args.flag_update {
if let Err(err) = qsv_update(true) {
if let Err(err) = util::qsv_update(true) {
werr!("{}", err);
::std::process::exit(1);
}
Expand All @@ -241,7 +176,7 @@ Please choose one of the following commands:",
command_list!()
));
if !args.flag_skip_update_check {
if let Err(err) = qsv_update(false) {
if let Err(err) = util::qsv_update(false) {
werr!("{}", err);
::std::process::exit(1);
} else {
Expand Down
76 changes: 5 additions & 71 deletions src/mainlite.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![allow(dead_code)]

extern crate crossbeam_channel as channel;
use self_update::cargo_crate_version;

use std::borrow::ToOwned;
use std::env;
Expand All @@ -10,9 +8,9 @@ use std::io;
use std::process;
use std::time::Instant;

use serde::Deserialize;

use docopt::Docopt;
use log::{error, info, log_enabled, Level};
use serde::Deserialize;

#[cfg(feature = "mimalloc")]
#[global_allocator]
Expand Down Expand Up @@ -114,72 +112,8 @@ struct Args {
flag_skip_update_check: bool,
}

fn qsv_update(verbose: bool) -> Result<(), Box<dyn ::std::error::Error>> {
if env::var("QSV_NO_UPDATE").is_ok() {
return Ok(());
}

let curr_version = cargo_crate_version!();
let releases = self_update::backends::github::ReleaseList::configure()
.repo_owner("jqnatividad")
.repo_name("qsv")
.build()?
.fetch()?;
let latest_release = &releases[0].version;
if latest_release > &curr_version.to_string() {
println!(
"Update {} available. Current version is {}.",
latest_release, curr_version
);
let status = self_update::backends::github::Update::configure()
.repo_owner("jqnatividad")
.repo_name("qsv")
.bin_name("qsvlite")
.show_download_progress(true)
.show_output(verbose)
.no_confirm(false)
.current_version(curr_version)
.build()?
.update()?;
let exe_full_path = format!("{:?}", std::env::current_exe().unwrap());
if verbose {
println!(
"Update successful for {}: `{}`!",
exe_full_path,
status.version()
);
}
}
Ok(())
}

use flexi_logger::{Cleanup, Criterion, FileSpec, Logger, Naming};
use log::{error, info, log_enabled, Level};

fn init_logger() {
let qsv_log_env = env::var("QSV_LOG_LEVEL").unwrap_or_else(|_| "off".to_string());
let qsv_log_dir = env::var("QSV_LOG_DIR").unwrap_or_else(|_| ".".to_string());

Logger::try_with_env_or_str(qsv_log_env)
.unwrap()
.log_to_file(
FileSpec::default()
.directory(qsv_log_dir)
.suppress_timestamp(),
)
.format_for_files(flexi_logger::detailed_format)
.o_append(true)
.rotate(
Criterion::Size(1_000_000),
Naming::Numbers,
Cleanup::KeepLogAndCompressedFiles(10, 100),
)
.start()
.unwrap();
}

fn main() {
init_logger();
util::init_logger();

let now = Instant::now();

Expand All @@ -203,7 +137,7 @@ fn main() {
return;
}
if args.flag_update {
if let Err(err) = qsv_update(true) {
if let Err(err) = util::qsv_update(true) {
werr!("{}", err);
::std::process::exit(1);
}
Expand All @@ -218,7 +152,7 @@ Please choose one of the following commands:",
command_list!()
));
if !args.flag_skip_update_check {
if let Err(err) = qsv_update(false) {
if let Err(err) = util::qsv_update(false) {
werr!("{}", err);
::std::process::exit(1);
} else {
Expand Down
92 changes: 92 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::time;

use ::num_cpus;
use docopt::Docopt;
use log::{info, log_enabled, Level};
use serde::de::{Deserialize, DeserializeOwned, Deserializer, Error};

use crate::config::{Config, Delimiter};
Expand Down Expand Up @@ -146,6 +147,13 @@ pub fn prep_progress(progress: &ProgressBar, record_count: u64) {
record_count.separate_with_commas()
));
progress.set_draw_delta(record_count / 100);

if log_enabled!(Level::Info) {
info!(
"Progress started... {} records",
record_count
);
}
}

pub fn finish_progress(progress: &ProgressBar) {
Expand All @@ -162,6 +170,13 @@ pub fn finish_progress(progress: &ProgressBar) {
.progress_chars("=>-"),
);
progress.finish();

if log_enabled!(Level::Info) {
info!(
"Progress done... {} records/sec",
per_sec_rate
);
}
}

pub fn get_args<T>(usage: &str, argv: &[&str]) -> CliResult<T>
Expand Down Expand Up @@ -373,3 +388,80 @@ impl<'de> Deserialize<'de> for FilenameTemplate {
}
}
}

pub fn init_logger() {
use flexi_logger::{Cleanup, Criterion, FileSpec, Logger, Naming};

let qsv_log_env = env::var("QSV_LOG_LEVEL").unwrap_or_else(|_| "off".to_string());
let qsv_log_dir = env::var("QSV_LOG_DIR").unwrap_or_else(|_| ".".to_string());

Logger::try_with_env_or_str(qsv_log_env)
.unwrap()
.log_to_file(
FileSpec::default()
.directory(qsv_log_dir)
.suppress_timestamp(),
)
.format_for_files(flexi_logger::detailed_format)
.o_append(true)
.rotate(
Criterion::Size(1_000_000),
Naming::Numbers,
Cleanup::KeepLogAndCompressedFiles(10, 100),
)
.start()
.unwrap();
}

pub fn qsv_update(verbose: bool) -> Result<(), Box<dyn ::std::error::Error>> {
use self_update::cargo_crate_version;

if env::var("QSV_NO_UPDATE").is_ok() {
return Ok(());
}

let curr_version = cargo_crate_version!();
let releases = self_update::backends::github::ReleaseList::configure()
.repo_owner("jqnatividad")
.repo_name("qsv")
.build()?
.fetch()?;
let latest_release = &releases[0].version;

if log_enabled!(Level::Info) {
info!(
"Current version: {} Latest Release: {}",
curr_version, latest_release
);
}

if latest_release > &curr_version.to_string() {
println!(
"Update {} available. Current version is {}.",
latest_release, curr_version
);
let status = self_update::backends::github::Update::configure()
.repo_owner("jqnatividad")
.repo_name("qsv")
.bin_name("qsvlite")
.show_download_progress(true)
.show_output(verbose)
.no_confirm(false)
.current_version(curr_version)
.build()?
.update()?;
let exe_full_path = format!("{:?}", std::env::current_exe().unwrap());
let update_status = format!(
"Update successful for {}: `{}`!",
exe_full_path,
status.version()
);
if verbose {
println!("{}", update_status);
}
if log_enabled!(Level::Info) {
info!("{}", update_status);
}
}
Ok(())
}

0 comments on commit 58348e8

Please sign in to comment.