Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(zk_toolbox): Add check for zksync repo path #2447

Merged
merged 10 commits into from
Jul 24, 2024
Original file line number Diff line number Diff line change
@@ -1,19 +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_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_directory_does_not_contain_cargo_toml_err, 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_NOT_MAIN_REPO_OR_FORK_ERR, MSG_REPOSITORY_ORIGIN_PROMPT, MSG_START_CONTAINERS_HELP,
MSG_START_CONTAINERS_PROMPT,
},
};
Expand All @@ -34,7 +38,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 +49,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 +118,46 @@ impl std::fmt::Display for LinkToCodeSelection {
}
}
}

fn check_link_to_code(shell: &Shell, path: &str) -> anyhow::Result<()> {
let path = Path::new(path);
if !path.exists() {
bail!(msg_path_to_zksync_does_not_exist_err(
path.to_str().unwrap()
));
}

Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
if !path.join("Cargo.toml").exists() {
bail!(msg_directory_does_not_contain_cargo_toml_err(
path.to_str().unwrap()
));
}

Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
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
11 changes: 11 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,17 @@ 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 =
"Oh no, seems it's not a main repo or a fork";
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
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:?}")
Deniallugo marked this conversation as resolved.
Show resolved Hide resolved
}

pub(super) fn msg_directory_does_not_contain_cargo_toml_err(path: &str) -> String {
format!("Directory does not contain Cargo.toml: {path:?}")
}

/// Ecosystem and chain init related messages
pub(super) const MSG_L1_RPC_URL_HELP: &str = "L1 RPC URL";
Expand Down
Loading