From f1cbb74b863b6e0bcfa74ad780beef29844bac6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Ignacio=20Gonz=C3=A1lez?= Date: Wed, 24 Jul 2024 17:37:08 +0200 Subject: [PATCH] feat(zk_toolbox): Add check for zksync repo path (#2447) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## What ❔ Add check for zksync repo path --- .../src/commands/ecosystem/args/create.rs | 61 +++++++++++++++++-- .../src/commands/ecosystem/create.rs | 2 +- .../crates/zk_inception/src/messages.rs | 7 +++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/create.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/create.rs index f005a98f6b64..d3d5fe129678 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/create.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/args/create.rs @@ -1,20 +1,23 @@ -use std::path::PathBuf; +use std::path::{Path, PathBuf}; +use anyhow::bail; use clap::Parser; -use common::{Prompt, PromptConfirm, PromptSelect}; +use common::{cmd::Cmd, logger, Prompt, PromptConfirm, PromptSelect}; use serde::{Deserialize, Serialize}; use slugify_rs::slugify; use strum::IntoEnumIterator; use strum_macros::EnumIter; use types::{L1Network, WalletCreation}; +use xshell::{cmd, Shell}; use crate::{ commands::chain::{args::create::ChainCreateArgs, ChainCreateArgsFinal}, messages::{ + msg_path_to_zksync_does_not_exist_err, MSG_CONFIRM_STILL_USE_FOLDER, MSG_ECOSYSTEM_NAME_PROMPT, MSG_L1_NETWORK_HELP, MSG_L1_NETWORK_PROMPT, MSG_LINK_TO_CODE_HELP, MSG_LINK_TO_CODE_PROMPT, MSG_LINK_TO_CODE_SELECTION_CLONE, - MSG_LINK_TO_CODE_SELECTION_PATH, MSG_REPOSITORY_ORIGIN_PROMPT, MSG_START_CONTAINERS_HELP, - MSG_START_CONTAINERS_PROMPT, + MSG_LINK_TO_CODE_SELECTION_PATH, MSG_NOT_MAIN_REPO_OR_FORK_ERR, + MSG_REPOSITORY_ORIGIN_PROMPT, MSG_START_CONTAINERS_HELP, MSG_START_CONTAINERS_PROMPT, }, }; @@ -34,7 +37,7 @@ pub struct EcosystemCreateArgs { } impl EcosystemCreateArgs { - pub fn fill_values_with_prompt(mut self) -> EcosystemCreateArgsFinal { + pub fn fill_values_with_prompt(mut self, shell: &Shell) -> EcosystemCreateArgsFinal { let mut ecosystem_name = self .ecosystem_name .unwrap_or_else(|| Prompt::new(MSG_ECOSYSTEM_NAME_PROMPT).ask()); @@ -45,7 +48,16 @@ impl EcosystemCreateArgs { PromptSelect::new(MSG_REPOSITORY_ORIGIN_PROMPT, LinkToCodeSelection::iter()).ask(); match link_to_code_selection { LinkToCodeSelection::Clone => "".to_string(), - LinkToCodeSelection::Path => Prompt::new(MSG_LINK_TO_CODE_PROMPT).ask(), + LinkToCodeSelection::Path => { + let mut path: String = Prompt::new(MSG_LINK_TO_CODE_PROMPT).ask(); + if let Err(err) = check_link_to_code(shell, &path) { + logger::warn(err); + if !PromptConfirm::new(MSG_CONFIRM_STILL_USE_FOLDER).ask() { + path = pick_new_link_to_code(shell); + } + } + path + } } }); @@ -105,3 +117,40 @@ impl std::fmt::Display for LinkToCodeSelection { } } } + +fn check_link_to_code(shell: &Shell, path: &str) -> anyhow::Result<()> { + let path = Path::new(path); + if !shell.path_exists(path) { + bail!(msg_path_to_zksync_does_not_exist_err( + path.to_str().unwrap() + )); + } + + let _guard = shell.push_dir(path); + let out = String::from_utf8( + Cmd::new(cmd!(shell, "git remote -v")) + .run_with_output()? + .stdout, + )?; + + if !out.contains("matter-labs/zksync-era") { + bail!(MSG_NOT_MAIN_REPO_OR_FORK_ERR); + } + + Ok(()) +} + +fn pick_new_link_to_code(shell: &Shell) -> String { + let link_to_code: String = Prompt::new(MSG_LINK_TO_CODE_PROMPT).ask(); + match check_link_to_code(shell, &link_to_code) { + Ok(_) => link_to_code, + Err(err) => { + logger::warn(err); + if !PromptConfirm::new(MSG_CONFIRM_STILL_USE_FOLDER).ask() { + pick_new_link_to_code(shell) + } else { + link_to_code + } + } + } +} diff --git a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/create.rs b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/create.rs index b7fdfee855f6..30dffad035ab 100644 --- a/zk_toolbox/crates/zk_inception/src/commands/ecosystem/create.rs +++ b/zk_toolbox/crates/zk_inception/src/commands/ecosystem/create.rs @@ -39,7 +39,7 @@ pub fn run(args: EcosystemCreateArgs, shell: &Shell) -> anyhow::Result<()> { } fn create(args: EcosystemCreateArgs, shell: &Shell) -> anyhow::Result<()> { - let args = args.fill_values_with_prompt(); + let args = args.fill_values_with_prompt(shell); logger::note(MSG_SELECTED_CONFIG, logger::object_to_string(&args)); logger::info(MSG_CREATING_ECOSYSTEM); diff --git a/zk_toolbox/crates/zk_inception/src/messages.rs b/zk_toolbox/crates/zk_inception/src/messages.rs index af40b48e5795..a33143b4bd66 100644 --- a/zk_toolbox/crates/zk_inception/src/messages.rs +++ b/zk_toolbox/crates/zk_inception/src/messages.rs @@ -36,6 +36,13 @@ pub(super) const MSG_ECOSYSTEM_ALREADY_EXISTS_ERR: &str = "Ecosystem already exi pub(super) const MSG_ECOSYSTEM_CONFIG_INVALID_ERR: &str = "Invalid ecosystem configuration"; pub(super) const MSG_LINK_TO_CODE_SELECTION_CLONE: &str = "Clone for me (recommended)"; pub(super) const MSG_LINK_TO_CODE_SELECTION_PATH: &str = "I have the code already"; +pub(super) const MSG_NOT_MAIN_REPO_OR_FORK_ERR: &str = + "It's not a zkSync Era main repository or fork"; +pub(super) const MSG_CONFIRM_STILL_USE_FOLDER: &str = "Do you still want to use this folder?"; + +pub(super) fn msg_path_to_zksync_does_not_exist_err(path: &str) -> String { + format!("Path to zkSync Era repo does not exist: {path:?}") +} /// Ecosystem and chain init related messages pub(super) const MSG_L1_RPC_URL_HELP: &str = "L1 RPC URL";