Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add self_update option to git_cm app #41

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ edit = "0.1.2"
git2 = "0.13.6"
itertools = "0.9.0"
once_cell = "1.4.0"
self_update = "0.19"
serde = { version = "1.0.114", features = ["derive"] }

[package.metadata.commits]
Expand Down
59 changes: 58 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use crate::{
},
questions::{ask, SurveyResults},
};
use anyhow::{Error, Result};
use clap::Clap;
use dialoguer::{theme::ColorfulTheme, Confirm};
use itertools::Itertools;
use self_update::{cargo_crate_version, version::bump_is_greater};
use std::{collections::HashMap, path::Path};

mod args;
mod cargo;
mod git;
Expand Down Expand Up @@ -48,7 +50,62 @@ fn create_commit(commit_msg: &str, repo: &Path) {
println!("Wrote commit: {}", hash);
}

fn update_app() -> Result<(), Error> {
// Check if the latest release published is newer than the actual one which
// is currently being used.
match look_for_new_release() {
Ok(None) => Ok(()),
Ok(Some(_)) => {
let want_to_bump = Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(
"A newer version for this crate was found.\nWould you like to update your application?"
)
.default(false)
.interact()?;
if want_to_bump == true {
let status = self_update::backends::github::Update::configure()
.repo_owner("SirWindfield")
.repo_name("git-cm")
.bin_name("git-cm")
.show_download_progress(true)
.show_output(true)
.current_version(cargo_crate_version!())
.build()?
.update()?;
println!(
"You've sucessfully updated your version to `{}`!",
status.version()
);
Ok(())
} else {
Ok(())
}
}
Err(e) => Err(e),
}
}

fn look_for_new_release() -> Result<Option<String>> {
let releases = self_update::backends::github::ReleaseList::configure()
.repo_owner("SirWindfield")
.repo_name("git-cm")
.build()?
.fetch()?;
if bump_is_greater(cargo_crate_version!(), &releases[0].version)? {
Ok(Some(releases[0].version.to_string()))
} else {
Ok(None)
}
}

fn run(app: App) {
// Check if there's a new release of the crate. In that case, ask the user
// if he/she wants to update it.
match update_app() {
Ok(()) => (),
Err(e) => eprintln!("An error ocurred during the app update process: {}", e),
};

// No point to continue if repo doesn't exist or there are no staged files
if check_staged_files_exist(app.repo_path.as_path()) {
// We can short-hand the editor mode for now as there aren't type-agnostic
Expand Down