diff --git a/Cargo.lock b/Cargo.lock index bf8c6319..43e08752 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1878,6 +1878,7 @@ dependencies = [ "rand 0.8.5", "regex", "reqwest", + "semver 1.0.22", "seq-macro", "serde", "serde-wasm-bindgen", diff --git a/Cargo.toml b/Cargo.toml index 85323fb5..b84014ac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ num = "0.4.1" portable-atomic = "1.6.0" tosubcommand = { git = "https://github.com/zkonduit/enum_to_subcommand", package = "tosubcommand" } metal = { git = "https://github.com/gfx-rs/metal-rs", optional = true } +semver = "1.0.22" # evm related deps [target.'cfg(not(target_arch = "wasm32"))'.dependencies] diff --git a/install_ezkl_cli.sh b/install_ezkl_cli.sh index 2e731945..af1fe5c9 100644 --- a/install_ezkl_cli.sh +++ b/install_ezkl_cli.sh @@ -99,6 +99,10 @@ fi echo "Removing old ezkl binary if it exists" [ -e file ] && rm file +# echo platform and architecture +echo "Platform: $PLATFORM" +echo "Architecture: $ARCHITECTURE" + # download the release and unpack the right tarball if [ "$PLATFORM" == "windows-msvc" ]; then JSON_RESPONSE=$(curl -s "$RELEASE_URL") diff --git a/src/bin/ezkl.rs b/src/bin/ezkl.rs index abaab128..ad82ceda 100644 --- a/src/bin/ezkl.rs +++ b/src/bin/ezkl.rs @@ -17,17 +17,14 @@ use rand::prelude::SliceRandom; #[cfg(not(target_arch = "wasm32"))] #[cfg(feature = "icicle")] use std::env; -#[cfg(not(target_arch = "wasm32"))] -use std::error::Error; #[tokio::main(flavor = "current_thread")] #[cfg(not(target_arch = "wasm32"))] -pub async fn main() -> Result<(), Box> { +pub async fn main() { let args = Cli::parse(); if let Some(generator) = args.generator { ezkl::commands::print_completions(generator, &mut Cli::command()); - Ok(()) } else if let Some(command) = args.command { init_logger(); #[cfg(not(any(target_arch = "wasm32", feature = "no-banner")))] @@ -38,15 +35,17 @@ pub async fn main() -> Result<(), Box> { } else { info!("Running with CPU"); } - info!("command: \n {}", &command.as_json().to_colored_json_auto()?); + info!( + "command: \n {}", + &command.as_json().to_colored_json_auto().unwrap() + ); let res = run(command).await; match &res { Ok(_) => info!("succeeded"), Err(e) => error!("failed: {}", e), }; - res.map(|_| ()) } else { - Err("No command provided".into()) + error!("no command provided"); } } diff --git a/src/commands.rs b/src/commands.rs index 8f0b97e1..6b27a2f6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -868,6 +868,12 @@ pub enum Commands { #[arg(long, value_hint = clap::ValueHint::Other)] addr_vk: Option, }, + /// Updates ezkl binary to version specified (or latest if not specified) + Update { + /// The version to update to + #[arg(value_hint = clap::ValueHint::Other, short='v', long)] + version: Option, + }, } diff --git a/src/execute.rs b/src/execute.rs index 8e48d4cc..04fe7507 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -502,6 +502,52 @@ pub async fn run(command: Commands) -> Result> { ) .await } + Commands::Update { version } => update_ezkl_binary(&version).map(|e| e.to_string()), + } +} + +/// Assert that the version is valid +fn assert_version_is_valid(version: &str) -> Result<(), Box> { + let err_string = "Invalid version string. Must be in the format v0.0.0"; + if version.is_empty() { + return Err(err_string.into()); + } + // safe to unwrap since we know the length is not 0 + if version.chars().nth(0).unwrap() != 'v' { + return Err(err_string.into()); + } + + semver::Version::parse(&version[1..]) + .map_err(|_| "Invalid version string. Must be in the format v0.0.0")?; + + Ok(()) +} + +const INSTALL_BYTES: &[u8] = include_bytes!("../install_ezkl_cli.sh"); + +fn update_ezkl_binary(version: &Option) -> Result> { + // run the install script with the version + let install_script = std::str::from_utf8(INSTALL_BYTES)?; + // now run as sh script with the version as an argument + let mut command = std::process::Command::new("sh"); + let mut command = command.arg("-c").arg(install_script); + + if let Some(version) = version { + assert_version_is_valid(version)?; + command = command.arg(version) + }; + let output = command.output()?; + + if output.status.success() { + info!("updated binary"); + Ok("".to_string()) + } else { + Err(format!( + "failed to update binary: {}, {}", + std::str::from_utf8(&output.stdout)?, + std::str::from_utf8(&output.stderr)? + ) + .into()) } }