Skip to content

Commit

Permalink
feat: add cask check-updates command. close #13
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Mar 22, 2022
1 parent 6268969 commit 0a329e4
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 15 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ Show more information with `cask --help` command.

## Command

| Command | Description |
| ---------------------------------- | --------------------------------- |
| cask install \<PACKAGE\> [VERSION] | Install package |
| cask uninstall \<PACKAGE\> | Uninstall package |
| cask info \<PACKAGE\> | Show information of package |
| cask update \<PACKAGE\> | Upgrade package to latest |
| cask list | List installed package |
| cask clean | Clear residual data |
| cask self-update | Update Cask to the newest version |
| Command | Description |
| ---------------------------------- | ----------------------------------- |
| cask install \<PACKAGE\> [VERSION] | Install package |
| cask uninstall \<PACKAGE\> | Uninstall package |
| cask info \<PACKAGE\> | Show information of package |
| cask update \<PACKAGE\> | Update package to latest |
| cask check-updates | Check and update packages to latest |
| cask list | List installed package |
| cask clean | Clear residual data |
| cask self-update | Update Cask to the newest version |

## Requirement

Expand Down
58 changes: 58 additions & 0 deletions src/command_check_updates.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![deny(warnings)]

use crate::{cask, command_install};

use eyre::Report;
use semver::Version;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct PackageInfo {
name: String,
bin: String,
current_version: String,
latest_version: String,
}

pub async fn check_updates(cask: &cask::Cask, is_check_only: bool) -> Result<(), Report> {
let mut packages: Vec<PackageInfo> = vec![];

for package in cask.list_formula()? {
eprintln!("Checking {} for update...", package.package.name);

let latest_version_op = package.get_latest_version()?;

if latest_version_op.is_none() {
continue;
}

let latest_version_str = latest_version_op.unwrap();

let cask_info = package.cask.unwrap();

let current = Version::parse(&cask_info.version).unwrap();
let latest = Version::parse(&latest_version_str).unwrap();

if latest > current {
packages.push(PackageInfo {
name: package.package.name,
bin: package.package.bin,
current_version: cask_info.version,
latest_version: latest_version_str,
})
}
}

for package in packages {
eprintln!(
"{}@{} get a update to {}",
package.name, package.current_version, package.latest_version
);

if !is_check_only {
command_install::install(cask, &package.name, Some(&package.latest_version)).await?;
}
}

Ok(())
}
2 changes: 0 additions & 2 deletions src/command_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ struct PackageInfo {
}

pub async fn list(cask: &cask::Cask, is_print_as_json: bool) -> Result<(), Report> {
cask.init()?;

let mut packages: Vec<PackageInfo> = vec![];

for package in cask.list_formula()? {
Expand Down
2 changes: 1 addition & 1 deletion src/command_upgrade.rs → src/command_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{cask, command_install, formula};
use eyre::Report;
use semver::Version;

pub async fn upgrade(
pub async fn update(
cask: &cask::Cask,
package_name: &str,
is_check_only: bool,
Expand Down
7 changes: 7 additions & 0 deletions src/formula.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,13 @@ impl Formula {
.map_err(|e| eyre::format_err!("{}", e))
}
}

// get the latest version of package
pub fn get_latest_version(&self) -> Result<Option<String>, Report> {
let version = self.get_versions()?;

Ok(version.first().map(|f| f.to_string()))
}
}

#[cfg(test)]
Expand Down
25 changes: 22 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// #![deny(warnings)]

mod cask;
mod command_check_updates;
mod command_clean;
mod command_info;
mod command_install;
mod command_list;
mod command_self_update;
mod command_uninstall;
mod command_upgrade;
mod command_update;
mod formula;
mod hooker;
mod symlink;
Expand Down Expand Up @@ -83,6 +84,17 @@ async fn main() {
)
.arg_required_else_help(true),
)
.subcommand(
Command::new("check-updates")
.about("Check and update packages to latest")
.arg(
Arg::new("check-only")
.short('c')
.long("check-only")
.help("Check update only")
.takes_value(false),
),
)
.subcommand(Command::new("self-update").about("Update Cask to the newest version"))
.subcommand(Command::new("clean").about("Clear residual data"));

Expand Down Expand Up @@ -132,11 +144,18 @@ async fn main() {

executor::block_on(f).expect("info installed package fail!");
}
Some(("upgrade", sub_matches)) => {
Some(("update", sub_matches)) => {
let package_name = sub_matches.value_of("PACKAGE").expect("required");
let is_check_only = sub_matches.is_present("check-only");

let f = command_upgrade::upgrade(&cask, package_name, is_check_only);
let f = command_update::update(&cask, package_name, is_check_only);

executor::block_on(f).expect("info installed package fail!");
}
Some(("check-updates", sub_matches)) => {
let is_check_only = sub_matches.is_present("check-only");

let f = command_check_updates::check_updates(&cask, is_check_only);

executor::block_on(f).expect("info installed package fail!");
}
Expand Down

0 comments on commit 0a329e4

Please sign in to comment.