Skip to content

Commit

Permalink
refactor: add UpdateInformer and FakeUpdateInformer structs for c…
Browse files Browse the repository at this point in the history
…onvenient use (#14)
  • Loading branch information
mgrachev authored Jan 5, 2022
1 parent 4f07b5c commit df22959
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 158 deletions.
19 changes: 12 additions & 7 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### 🛠 Fixed

## [v0.2.0] - 2022-01-05
### 🚀 Added
- Add `UpdateInformer` and `FakeUpdateInformer` structs for convenient use [#14](https://github.com/mgrachev/update-informer/pull/14)

## [v0.1.0] - 2021-12-30
### 🚀 Added
- Add `stub_check_version` function and update docs [#13](https://github.com/mgrachev/update-informer/pull/13) ([@mgrachev](https://github.com/mgrachev))
- Add documentation and update examples [#12](https://github.com/mgrachev/update-informer/pull/12) ([@mgrachev](https://github.com/mgrachev))
- Save latest version to file and add interval check [#11](https://github.com/mgrachev/update-informer/pull/11) ([@mgrachev](https://github.com/mgrachev))
- Set up CI/CD [#10](https://github.com/mgrachev/update-informer/pull/10) ([@mgrachev](https://github.com/mgrachev))
- Add tests for registries: Crates.io and GitHub [#9](https://github.com/mgrachev/update-informer/pull/9) ([@mgrachev](https://github.com/mgrachev))
- Check updates on GitHub [#8](https://github.com/mgrachev/update-informer/pull/8) ([@mgrachev](https://github.com/mgrachev))
- Check updates on Crates.io [#1](https://github.com/mgrachev/update-informer/pull/1) ([@mgrachev](https://github.com/mgrachev))
- Add `stub_check_version` function and update docs [#13](https://github.com/mgrachev/update-informer/pull/13)
- Add documentation and update examples [#12](https://github.com/mgrachev/update-informer/pull/12)
- Save latest version to file and add interval check [#11](https://github.com/mgrachev/update-informer/pull/11)
- Set up CI/CD [#10](https://github.com/mgrachev/update-informer/pull/10)
- Add tests for registries: Crates.io and GitHub [#9](https://github.com/mgrachev/update-informer/pull/9)
- Check updates on GitHub [#8](https://github.com/mgrachev/update-informer/pull/8)
- Check updates on Crates.io [#1](https://github.com/mgrachev/update-informer/pull/1)

### ⚙️ Changed

### 🛠 Fixed

[v0.2.0]: https://github.com/mgrachev/update-informer/releases/tag/v0.2.0
[v0.1.0]: https://github.com/mgrachev/update-informer/releases/tag/v0.1.0
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "update-informer"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
authors = ["Mikhail Grachev <[email protected]>"]
description = "Update informer for CLI applications"
Expand Down
55 changes: 33 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Update-informer [![CI](https://github.com/mgrachev/update-informer/workflows/CI/badge.svg)](https://github.com/mgrachev/update-informer/actions)
# Update-informer [![CI](https://github.com/mgrachev/update-informer/workflows/CI/badge.svg)](https://github.com/mgrachev/update-informer/actions) [![Crates.io](https://img.shields.io/crates/v/update-informer)](https://crates.io/crates/update-informer) [![docs.rs](https://img.shields.io/docsrs/update-informer)](https://docs.rs/update-informer)

Update informer for CLI applications written in Rust 🦀

Expand All @@ -18,44 +18,49 @@ Add `update-informer` to `Cargo.toml`:

```toml
[dependencies]
update-informer = "0.1.0"
update-informer = "0.2.0"
```

To check for a new version on **Crates.io**, use the `check_version` function.<br>
To check for a new version on **Crates.io**, use the `UpdateInformer::check_version` function.<br>
This function takes the project name and current version as well as check interval:

```rust
use update_informer::{check_version, registry::Crates};
use update_informer::{registry::Crates, Check, UpdateInformer};

if let Ok(Some(version)) = check_version(Crates, "repo", "0.1.0", Duration::from_secs(60 * 60 * 24)) {
let informer = UpdateInformer::new(Crates, "repo", "0.1.0", Duration::from_secs(60 * 60 * 24));
if let Ok(Some(version)) = informer.check_version() {
println!("New version is available: {}", version);
}
```

Also, you can take the name and version of the project from **Cargo** using environment variables:

```rust
use update_informer::{check_version, registry::Crates};
use update_informer::{registry::Crates, Check, UpdateInformer};

check_version(Crates, env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), Duration::from_secs(60 * 60 * 24));
let name = env!("CARGO_PKG_NAME");
let version = env!("CARGO_PKG_VERSION");
UpdateInformer::new(Crates, name, version, Duration::from_secs(60 * 60 * 24)).check_version();
```

Note that the first check will start only after the interval has expired:

```rust
use update_informer::{check_version, registry::Crates};
use update_informer::{registry::Crates, Check, UpdateInformer};

const EVERY_HOUR: Duration = Duration::from_secs(60 * 60);

check_version(Crates, "repo", "0.1.0", EVERY_HOUR); // The check will start only after an hour
let informer = UpdateInformer::new(Crates, "repo", "0.1.0", EVERY_HOUR);
informer.check_version(); // The check will start only after an hour
```

To check for a new version on **GitHub** (note that the project name must contain the owner):

```rust
use update_informer::{check_version, registry::GitHub};
use update_informer::{registry::GitHub, Check, UpdateInformer};

check_version(GitHub, "owner/repo", "0.1.0", Duration::from_secs(60 * 60 * 24));
let informer = UpdateInformer::new(GitHub, "owner/repo", "0.1.0", Duration::from_secs(60 * 60 * 24));
informer.check_version();
```

## Example
Expand All @@ -65,14 +70,15 @@ A real example of using `update_informer` with [colored](https://github.com/mack
```rust
use colored::*;
use std::time::Duration;
use update_informer::{check_version, registry::Crates};
use update_informer::{registry::Crates, Check, UpdateInformer};

fn main() {
let pkg_name = env!("CARGO_PKG_NAME");
let current_version = env!("CARGO_PKG_VERSION");
let interval = Duration::from_secs(60 * 60 * 24);

if let Ok(Some(version)) = check_version(Crates, pkg_name, current_version, interval) {
let informer = UpdateInformer::new(Crates, pkg_name, current_version, interval);
if let Ok(Some(version)) = informer.check_version() {
let msg = format!(
"A new release of {pkg_name} is available: v{current_version} -> {new_version}",
pkg_name = pkg_name.italic().cyan(),
Expand All @@ -97,26 +103,29 @@ The result will look like:

## Tests

In order not to check for updates in tests, you can use the `stub_check_version` function, which returns the desired version.
In order not to check for updates in tests, you can use the `FakeUpdateInformer::check_version` function, which returns the desired version.

Example of usage in unit tests:

```rust
use std::time::Duration;
use update_informer::registry::Crates;
use update_informer::{registry::Crates, Check, FakeUpdateInformer, UpdateInformer};

let name = "repo";
let version = "0.1.0";
let interval = Duration::from_secs(60 * 60 * 24);

#[cfg(not(test))]
let result = update_informer::check_version(Crates, "repo", "0.1.0", Duration::from_secs(60 * 60 * 24));
let informer = UpdateInformer::new(Crates, name, version, interval);

#[cfg(test)]
let result = update_informer::stub_check_version(Crates, "repo", "0.1.0", Duration::from_secs(60 * 60 * 24), "1.0.0");
let informer = FakeUpdateInformer::new(Crates, name, version, interval, "1.0.0");

if let Ok(Some(version)) = result {
if let Ok(Some(version)) = informer.check_version() {
println!("New version is available: {}", version);
}
```

To use the `stub_check_version` function in integration tests, you must first add the feature flag to `Cargo.toml`:
To use the `FakeUpdateInformer::check_version` function in integration tests, you must first add the feature flag to `Cargo.toml`:

```toml
[features]
Expand All @@ -127,10 +136,12 @@ Then use this feature flag in your code and integration tests:

```rust
#[cfg(not(feature = "stub_check_version"))]
let result = update_informer::check_version(Crates, "repo", "0.1.0", Duration::from_secs(60 * 60 * 24));
let informer = UpdateInformer::new(Crates, name, version, interval);

#[cfg(feature = "stub_check_version")]
let result = update_informer::stub_check_version(Crates, "repo", "0.1.0", Duration::from_secs(60 * 60 * 24), "1.0.0");
let informer = FakeUpdateInformer::new(Crates, name, version, interval, "1.0.0");

informer.check_version();
```

## Sponsors
Expand Down
Loading

0 comments on commit df22959

Please sign in to comment.