|
| 1 | +use std::io::{Error, ErrorKind}; |
| 2 | + |
| 3 | +use clap::{ArgMatches, Command}; |
| 4 | +use self_update::{backends::github::Update, cargo_crate_version}; |
| 5 | + |
| 6 | +use crate::{ |
| 7 | + args::{no_confirm::ArgNoConfirm, Args}, |
| 8 | + cmd::Cmd, |
| 9 | + output::OutMessage, |
| 10 | +}; |
| 11 | + |
| 12 | +/// init cmd should be used to generate an empty gum-config |
| 13 | +pub(crate) fn add_upgrade_cmd() -> Command<'static> { |
| 14 | + return Command::new("upgrade") |
| 15 | + .about("Update current gum with a newer version") |
| 16 | + .arg(ArgNoConfirm::add()); |
| 17 | +} |
| 18 | + |
| 19 | +pub(crate) struct UpgradeCmd { |
| 20 | + no_confirm: bool, |
| 21 | +} |
| 22 | + |
| 23 | +pub(crate) fn prepare<'a>(sub_matches: &'a ArgMatches) -> Result<impl Cmd<'a>, Error> { |
| 24 | + let no_confirm: bool = ArgNoConfirm::parse(sub_matches).unwrap().value(); |
| 25 | + Ok(UpgradeCmd { no_confirm }) |
| 26 | +} |
| 27 | + |
| 28 | +impl<'a> Cmd<'a> for UpgradeCmd { |
| 29 | + fn exec(&self) -> Result<(), Error> { |
| 30 | + let status = match Update::configure() |
| 31 | + .repo_owner("allanger") |
| 32 | + .repo_name("gitlab-user-manager") |
| 33 | + .bin_name("gum") |
| 34 | + .show_download_progress(true) |
| 35 | + .current_version(cargo_crate_version!()) |
| 36 | + .no_confirm(self.no_confirm) |
| 37 | + .build() |
| 38 | + .unwrap() |
| 39 | + .update() |
| 40 | + { |
| 41 | + Ok(s) => s, |
| 42 | + Err(err) => return Err(Error::new(ErrorKind::Other, err.to_string())), |
| 43 | + }; |
| 44 | + OutMessage::message_empty(format!("Update status: `{}`!", status.version()).as_str()); |
| 45 | + Ok(()) |
| 46 | + } |
| 47 | +} |
0 commit comments