Skip to content

Commit

Permalink
feat: implement a check to the latest version of the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
ppoliani committed Feb 4, 2024
1 parent 09fa6f9 commit e58cf38
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
.vscode
local_keys
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ num-bigint = "0.4.4"
num-traits = "0.2.17"
rand = "0.8.5"
rand_chacha = "0.3.1"
reqwest = { version = "0.11", features = ["stream"] }
reqwest = { version = "0.11", features = ["stream", "json"] }
secp256k1 = "0.28.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = { version = "1.0" }
Expand All @@ -41,6 +41,7 @@ tokio = { version = "1.34", features = [
"macros",
] }
tokio-stream = "0.1.14"
versions = "6.1.0"

[patch.crates-io]
# see docs/serialization.md
Expand Down
4 changes: 4 additions & 0 deletions src/bin/zkbtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use zkbitcoin::{
},
snarkjs::{self, CompilationResult},
taproot_addr_from,
utils::version,
};

#[derive(Parser)]
Expand Down Expand Up @@ -140,6 +141,9 @@ async fn main() -> Result<()> {
taproot_addr_from(ZKBITCOIN_FEE_PUBKEY).unwrap().to_string()
);

// ignore if there is any error
let _ = version::check_version().await;

// parse CLI
let cli = Cli::parse();
match &cli.command {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod json_rpc_stuff;
pub mod plonk;
pub mod snarkjs;
pub mod srs;
pub mod utils;

/// 1. Alice signs a transaction to deploy a smart contract.
pub mod alice_sign_tx;
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod version;
43 changes: 43 additions & 0 deletions src/utils/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use anyhow::Result;
use log::warn;
use reqwest::Client;
use serde::Deserialize;
use versions::Versioning;

const RELEASES_URL: &'static str =
"https://api.github.com/repos//sigma0-xyz/zkbitcoin/releases/latest";

#[derive(Deserialize, Debug)]
struct Release {
url: String,
tag_name: String,
}

async fn fetch_latest_version() -> Result<Release> {
let client = Client::new();

let release = client
.get(RELEASES_URL)
.header("User-Agent", "Rust CLI")
.send()
.await?
.json::<Release>()
.await?;

Ok(release)
}

pub async fn check_version() -> Result<()> {
let current_version = Versioning::new(env!("CARGO_PKG_VERSION"));
let latest_release = fetch_latest_version().await?;
let latest_version = Versioning::new(&latest_release.tag_name.replace("v", ""));

if current_version < latest_version {
warn!(
"You are using an old version. Please download the latest version: {}",
latest_release.url
)
}

Ok(())
}

0 comments on commit e58cf38

Please sign in to comment.