Skip to content

Commit

Permalink
New command: checkupdate (#56)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
eirikb authored Jul 20, 2023
1 parent a644960 commit 9ca5b0c
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 21 deletions.
70 changes: 68 additions & 2 deletions src/stage4/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 src/stage4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
11 changes: 11 additions & 0 deletions src/stage4/src/barus.rs
Original file line number Diff line number Diff line change
@@ -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
}
25 changes: 23 additions & 2 deletions src/stage4/src/checker.rs
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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");
}
}
}
}
}
}
Expand Down
43 changes: 26 additions & 17 deletions src/stage4/src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -29,16 +30,18 @@ Version: {ver}
Usage: ./gg.cmd [options] <executable name>@<version>:<dependent executable name>@<version> [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
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
_ => {}
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 9ca5b0c

Please sign in to comment.