From 9ca5b0c5da7f3d274ee26d342e6219052b30da06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eirik=20Brandtz=C3=A6g?= Date: Thu, 20 Jul 2023 14:32:12 +0200 Subject: [PATCH] New command: checkupdate (#56) * New command: checkupdate Will prompt user to update if update is available. Based on previously run version req + tags. * New command: Cache clean Simply delete .cache/gg --- src/stage4/Cargo.lock | 70 +++++++++++++++++++++++++++++++++++++-- src/stage4/Cargo.toml | 1 + src/stage4/src/barus.rs | 11 ++++++ src/stage4/src/checker.rs | 25 ++++++++++++-- src/stage4/src/main.rs | 43 ++++++++++++++---------- 5 files changed, 129 insertions(+), 21 deletions(-) create mode 100644 src/stage4/src/barus.rs diff --git a/src/stage4/Cargo.lock b/src/stage4/Cargo.lock index bea23d5..adfe5d4 100644 --- a/src/stage4/Cargo.lock +++ b/src/stage4/Cargo.lock @@ -375,6 +375,18 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "dialoguer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59c6f2989294b9a498d3ad5491a79c6deb604617378e1cdc4bfc1c1361fe2f87" +dependencies = [ + "console", + "shell-words", + "tempfile", + "zeroize", +] + [[package]] name = "digest" version = "0.10.6" @@ -526,6 +538,15 @@ dependencies = [ "libc", ] +[[package]] +name = "fastrand" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e51093e27b0797c359783294ca4f0a911c270184cb10f85783b118614a1501be" +dependencies = [ + "instant", +] + [[package]] name = "filetime" version = "0.2.21" @@ -534,7 +555,7 @@ checksum = "5cbc844cecaee9d4443931972e1289c8ff485cb4cc2767cb03ca139ed6885153" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.2.16", "windows-sys 0.48.0", ] @@ -896,6 +917,15 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "instant" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" +dependencies = [ + "cfg-if", +] + [[package]] name = "io-lifetimes" version = "1.0.10" @@ -1137,7 +1167,7 @@ checksum = "9069cbb9f99e3a5083476ccb29ceb1de18b9118cafa53e90c9551235de2b9521" dependencies = [ "cfg-if", "libc", - "redox_syscall", + "redox_syscall 0.2.16", "smallvec", "windows-sys 0.45.0", ] @@ -1419,6 +1449,15 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_syscall" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567664f262709473930a4bf9e51bf2ebf3348f2e748ccc50dea20646858f8f29" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.7.0" @@ -1701,6 +1740,12 @@ dependencies = [ "digest", ] +[[package]] +name = "shell-words" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24188a676b6ae68c3b2cb3a01be17fbf7240ce009799bb56d5b1409051e78fde" + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1758,6 +1803,7 @@ name = "stage4" version = "0.1.0" dependencies = [ "async-compression", + "dialoguer", "env_logger", "futures", "futures-io", @@ -1873,6 +1919,20 @@ dependencies = [ "xattr", ] +[[package]] +name = "tempfile" +version = "3.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31c0432476357e58790aaa47a8efb0c5138f137343f3b5f23bd36a27e3b0a6d6" +dependencies = [ + "autocfg", + "cfg-if", + "fastrand", + "redox_syscall 0.3.5", + "rustix", + "windows-sys 0.48.0", +] + [[package]] name = "tendril" version = "0.4.3" @@ -2475,6 +2535,12 @@ dependencies = [ "lzma-sys", ] +[[package]] +name = "zeroize" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" + [[package]] name = "zip" version = "0.6.4" diff --git a/src/stage4/Cargo.toml b/src/stage4/Cargo.toml index b441d16..b7640c2 100644 --- a/src/stage4/Cargo.toml +++ b/src/stage4/Cargo.toml @@ -29,3 +29,4 @@ indicatif = "0.17.2" which = "4.3.0" serde-xml-rs = "0.6.0" walkdir = "2.3.3" +dialoguer = "0.10.4" diff --git a/src/stage4/src/barus.rs b/src/stage4/src/barus.rs new file mode 100644 index 0000000..554c237 --- /dev/null +++ b/src/stage4/src/barus.rs @@ -0,0 +1,11 @@ +use std::fmt::Write; +use indicatif::{ProgressBar, ProgressState, ProgressStyle}; + +pub fn create_barus() -> ProgressBar { + let pb = ProgressBar::new(1); + pb.set_style(ProgressStyle::with_template("{prefix:.bold} {spinner:.green} {msg} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})") + .unwrap() + .with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) + .progress_chars("#>-")); + pb +} diff --git a/src/stage4/src/checker.rs b/src/stage4/src/checker.rs index b09d2ad..5620d55 100644 --- a/src/stage4/src/checker.rs +++ b/src/stage4/src/checker.rs @@ -1,9 +1,11 @@ use std::fs; +use dialoguer::Confirm; use log::{debug, info}; -use crate::executor::{AppInput, GgMeta}; +use crate::barus::create_barus; +use crate::executor::{AppInput, GgMeta, prep}; use crate::Executor; -pub async fn check(input: &AppInput) { +pub async fn check(input: &AppInput, update: bool) { let entries = walkdir::WalkDir::new("./.cache/gg").into_iter() .filter_map(|x| x.ok()) .filter(|x| x.file_name().to_string_lossy() == "gg-meta.json"); @@ -27,6 +29,25 @@ pub async fn check(input: &AppInput) { if latest_version.clone().map(|v| v.to_version()) > current_version.clone().map(|v| v.to_version()) { println!(" ** {}: New version available!", executor.get_name()); + if update { + if Confirm::new() + .with_prompt("Do you want to update?") + .interact() + .unwrap_or(false) { + println!("Updating..."); + if let Some(parent) = entry.path().parent() { + if fs::remove_dir_all(parent).is_ok() { + let pb = create_barus(); + let e = executor; + let _ = prep(&*e, input, &pb).await; + } else { + println!("Unable to update"); + } + } else { + println!("Unable to update"); + } + } + } } } } diff --git a/src/stage4/src/main.rs b/src/stage4/src/main.rs index f598ceb..d0fbaf1 100644 --- a/src/stage4/src/main.rs +++ b/src/stage4/src/main.rs @@ -1,12 +1,12 @@ use std::collections::HashMap; -use std::fmt::Write; use std::fs; use std::process::ExitCode; use futures_util::future::join_all; -use indicatif::{MultiProgress, ProgressBar, ProgressState, ProgressStyle}; +use indicatif::MultiProgress; use log::{debug, info, LevelFilter}; +use crate::barus::create_barus; use crate::bloody_indiana_jones::download; use crate::executor::{AppInput, Executor, ExecutorCmd, GgVersionReq, prep, try_run}; use crate::no_clap::NoClap; @@ -19,6 +19,7 @@ mod no_clap; mod bloody_maven; mod executors; mod checker; +mod barus; fn print_help(ver: &str) { println!(r"gg.cmd @@ -29,16 +30,18 @@ Version: {ver} Usage: ./gg.cmd [options] @:@ [program arguments] Options: - -v Info output - -vv Debug output - -vvv Trace output - -w Even more output - -V Print version + -v Info output + -vv Debug output + -vvv Trace output + -w Even more output + -V Print version Built in commands: - update Update gg.cmd - help Print help - check Check for updates + update Update gg.cmd + help Print help + check Check for updates + checkupdate Check for updates and update if available + cacheclean Clean cache Examples: ./gg.cmd node @@ -99,7 +102,7 @@ async fn main() -> ExitCode { "update" => { println!("Updating gg.cmd..."); let url = "https://github.com/eirikb/gg/releases/latest/download/gg.cmd"; - let pb = ProgressBar::new(0); + let pb = create_barus(); download(url, "gg.cmd", &pb).await; return ExitCode::from(0); } @@ -108,7 +111,16 @@ async fn main() -> ExitCode { return ExitCode::from(0); } "check" => { - checker::check(input).await; + checker::check(input, false).await; + return ExitCode::from(0); + } + "checkupdate" => { + checker::check(input, true).await; + return ExitCode::from(0); + } + "cacheclean" => { + println!("Cleaning cache"); + let _ = fs::remove_dir_all(".cache/gg"); return ExitCode::from(0); } _ => {} @@ -156,11 +168,8 @@ async fn main() -> ExitCode { let m = MultiProgress::new(); let alles = executors.iter().enumerate().map(|(i, x)| { - let pb = m.insert(i, ProgressBar::new(1)); - pb.set_style(ProgressStyle::with_template("{prefix:.bold} {spinner:.green} {msg} [{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})") - .unwrap() - .with_key("eta", |state: &ProgressState, w: &mut dyn Write| write!(w, "{:.1}s", state.eta().as_secs_f64()).unwrap()) - .progress_chars("#>-")); + let pb = create_barus(); + let pb = m.insert(i, pb); (x, pb) }).map(|(x, pb)| async move { let app_path = prep(&**x, &input, &pb).await.expect("Prep failed");