Skip to content

Commit

Permalink
feat: add log
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Jan 29, 2024
1 parent c3887bb commit 663db2a
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 24 deletions.
37 changes: 35 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions goup-downloader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ sha2 = "0.10"
flate2 = "1.0"
tar = "0.4"
zip = "0.6"
log = "0.4"
# git2 = "0.18"
8 changes: 4 additions & 4 deletions goup-downloader/src/downloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Downloader {
],
)?;
}
println!("Updating the go development tree...");
log::info!("Updating the go development tree...");
//* git fetch origin master
Self::execute_command("git", &gotip_go, ["fetch", "origin", "master"])?;
//* git -c advice.detachedHead=false checkout FETCH_HEAD
Expand Down Expand Up @@ -105,7 +105,7 @@ impl Downloader {
let version_dest_dir = Dir::new(&home).version(version);
// 是否已解压成功并且存在
if Dir::is_dot_unpacked_success_file_exists(&home, version) {
println!(
log::info!(
"{}: already installed in {:?}",
version,
version_dest_dir.display()
Expand Down Expand Up @@ -145,14 +145,14 @@ impl Downloader {
// 校验压缩包sha256
Self::verify_archive_file_sha256(&archive_file, &archive_url)?;
// 解压
println!("Unpacking {} ...", archive_file.display());
log::info!("Unpacking {} ...", archive_file.display());
archive_file
.to_string_lossy()
.parse::<Unpack>()?
.unpack(&version_dest_dir, &archive_file)?;
// 设置解压成功
Dir::create_dot_unpacked_success_file(&home, version)?;
println!(
log::info!(
"Success: {} installed in {}",
version,
version_dest_dir.display()
Expand Down
1 change: 1 addition & 0 deletions goup-version/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ anyhow = "1.0"
dirs = "5.0"
regex = "1.10"
reqwest = { version = "0.11", default-features = false, features = ["blocking", "rustls-tls-native-roots"] }
log = "0.4"

[target.'cfg(windows)'.dependencies]
junction = "1.0"
Expand Down
18 changes: 10 additions & 8 deletions goup-version/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,20 @@ impl Version {
{
junction::create(original, &link)?;
}
println!("Default Go is set to '{version}'");
log::info!("Default Go is set to '{version}'");
Ok(())
}

pub fn remove_go_version(version: &str) -> Result<(), anyhow::Error> {
let version = Self::normalize(version);
let version_dir = Dir::from_home_dir()?.version(version);
if version_dir.exists() {
fs::remove_dir_all(&version_dir)?;
let cur = Self::current_go_version()?;
if Some(&version) == cur.as_ref() {
log::warn!("{} is current active version, do not remove it", version);
} else {
let version_dir = Dir::from_home_dir()?.version(version);
if version_dir.exists() {
fs::remove_dir_all(&version_dir)?;
}
}
Ok(())
}
Expand All @@ -156,10 +161,7 @@ impl Version {
for ver in vers {
let version = Self::normalize(ver);
if Some(&version) == cur.as_ref() {
println!(
"warning: {} is current active version, do not remove it",
ver
);
log::warn!("{} is current active version, do not remove it", ver);
continue;
}
let version_dir = Dir::new(&home).version(&version);
Expand Down
2 changes: 2 additions & 0 deletions goup/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ prettytable-rs = "0.10"
dialoguer = "0.11"
self_update = {version = "0.39", default-features = false, features = ["rustls", "compression-flate2", "compression-zip-deflate"]}
shadow-rs = "0.26"
log = "0.4"
env_logger = "0.11"
10 changes: 5 additions & 5 deletions goup/src/command/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Run for Install {
Toolchain::Stable => {
let version = Version::get_upstream_latest_go_version(&self.host)?;
let version = Version::normalize(&version);
println!("Installing {} ...", version);
log::info!("Installing {} ...", version);
Downloader::install_go_version(&version)?;
version
}
Expand All @@ -42,7 +42,7 @@ impl Run for Install {
.last()
.ok_or_else(|| anyhow!("failed get latest unstable version"))?;
let version = Version::normalize(version);
println!("Installing {} ...", version);
log::info!("Installing {} ...", version);
Downloader::install_go_version(&version)?;
version
}
Expand All @@ -52,17 +52,17 @@ impl Run for Install {
.last()
.ok_or_else(|| anyhow!("failed get latest beta version"))?;
let version = Version::normalize(version);
println!("Installing {} ...", version);
log::info!("Installing {} ...", version);
Downloader::install_go_version(&version)?;
version
}
Toolchain::Version(version) => {
println!("Installing {} ...", version);
log::info!("Installing {} ...", version);
Downloader::install_go_version(&version)?;
version
}
Toolchain::Nightly => {
println!("Installing gotip ...");
log::info!("Installing gotip ...");
Downloader::install_go_tip(self.cl.as_deref())?;
"gotip".to_owned()
}
Expand Down
2 changes: 1 addition & 1 deletion goup/src/command/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Run for List {
fn run(&self) -> Result<(), anyhow::Error> {
let vers = Version::list_go_version()?;
if vers.is_empty() {
println!(
log::info!(
"No Go is installed by goup.{}",
if let Ok(go_bin) = which("go") {
format!(" Using system Go {}.", go_bin.to_string_lossy())
Expand Down
20 changes: 19 additions & 1 deletion goup/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod upgrade;

use clap::{ArgAction, Args, CommandFactory};
use clap::{Parser, Subcommand};
use log::LevelFilter;
use shadow_rs::shadow;
use std::env::consts::{ARCH, OS};

Expand Down Expand Up @@ -53,9 +54,22 @@ pub trait Run {

#[derive(Args, Debug, PartialEq)]
struct Global {
/// Verbose
/// Verbose log
#[arg(short, long, action = ArgAction::Count)]
verbose: u8,
/// Whether or not to write the target in the log format.
#[arg(short, long)]
enable_target: bool,
}

impl Global {
fn log_filter_level(&self) -> LevelFilter {
match self.verbose {
0 => LevelFilter::Info,
1 => LevelFilter::Debug,
_ => LevelFilter::Trace,
}
}
}

#[derive(Parser, Debug, PartialEq)]
Expand Down Expand Up @@ -102,6 +116,10 @@ enum Command {

impl Run for Cli {
fn run(&self) -> Result<(), anyhow::Error> {
env_logger::builder()
.format_target(self.global.enable_target)
.filter_level(self.global.log_filter_level())
.init();
match &self.command {
Command::Install(cmd) => cmd.run(),
Command::List(cmd) => cmd.run(),
Expand Down
4 changes: 2 additions & 2 deletions goup/src/command/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Run for Remove {
if self.version.is_empty() {
let vers = Version::list_go_version()?;
if vers.is_empty() {
println!("No go is installed");
log::info!("No go is installed");
return Ok(());
}
let items: Vec<&str> = vers.iter().map(|v| v.version.as_ref()).collect();
Expand All @@ -26,7 +26,7 @@ impl Run for Remove {
.items(&items)
.interact()?;
if selection.is_empty() {
println!("No item selected");
log::info!("No item selected");
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion goup/src/command/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Run for Upgrade {
.current_version(cargo_crate_version!())
.build()?
.update()?;
println!("Update status: `v{}`!", status.version());
log::info!("Update status: `v{}`!", status.version());
Ok(())
}
}

0 comments on commit 663db2a

Please sign in to comment.