Skip to content

Commit

Permalink
feat(zk_toolbox): Add check for zksync repo path (#2447)
Browse files Browse the repository at this point in the history
## What ❔
Add check for zksync repo path
  • Loading branch information
matias-gonz authored Jul 24, 2024
1 parent 0f475c9 commit f1cbb74
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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,
},
};

Expand All @@ -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());
Expand All @@ -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
}
}
});

Expand Down Expand Up @@ -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
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions zk_toolbox/crates/zk_inception/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit f1cbb74

Please sign in to comment.