Skip to content

Commit

Permalink
feat(zk_toolbox): Clean command (#2387)
Browse files Browse the repository at this point in the history
## What ❔

<!-- What are the changes this PR brings about? -->
<!-- Example: This PR adds a PR template to the repo. -->
<!-- (For bigger PRs adding more context is appreciated) -->

## Why ❔

<!-- Why are these changes done? What goal do they contribute to? What
are the principles behind them? -->
<!-- Example: PR templates ensure PR reviewers, observers, and future
iterators are in context about the evolution of repos. -->

## Checklist

<!-- Check your PR fulfills the following items. -->
<!-- For draft PRs check the boxes as you complete them. -->

- [ ] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [ ] Tests for the changes have been added / updated.
- [ ] Documentation comments have been added / updated.
- [ ] Code has been formatted via `zk fmt` and `zk lint`.

---------

Signed-off-by: Danil <[email protected]>
  • Loading branch information
Deniallugo authored Jul 8, 2024
1 parent 275a333 commit 52a4680
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
73 changes: 73 additions & 0 deletions zk_toolbox/crates/zk_supervisor/src/commands/clean/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use anyhow::Context;
use clap::Subcommand;
use common::{docker, logger};
use config::{EcosystemConfig, DOCKER_COMPOSE_FILE};
use xshell::Shell;

use crate::messages::{
MSG_CONTRACTS_CLEANING, MSG_CONTRACTS_CLEANING_FINISHED, MSG_DOCKER_COMPOSE_CLEANED,
MSG_DOCKER_COMPOSE_DOWN, MSG_DOCKER_COMPOSE_REMOVE_VOLUMES,
};

#[derive(Subcommand, Debug)]
pub enum CleanCommands {
All,
Containers,
ContractsCache,
}

pub fn run(shell: &Shell, args: CleanCommands) -> anyhow::Result<()> {
let ecosystem = EcosystemConfig::from_file(shell)?;
match args {
CleanCommands::All => {
containers(shell)?;
contracts(shell, &ecosystem)?;
}
CleanCommands::Containers => containers(shell)?,
CleanCommands::ContractsCache => contracts(shell, &ecosystem)?,
}
Ok(())
}

pub fn containers(shell: &Shell) -> anyhow::Result<()> {
logger::info(MSG_DOCKER_COMPOSE_DOWN);
docker::down(shell, DOCKER_COMPOSE_FILE)?;
logger::info(MSG_DOCKER_COMPOSE_REMOVE_VOLUMES);
shell.remove_path("volumes")?;
logger::info(MSG_DOCKER_COMPOSE_CLEANED);
Ok(())
}

pub fn contracts(shell: &Shell, ecosystem_config: &EcosystemConfig) -> anyhow::Result<()> {
let path_to_foundry = ecosystem_config.path_to_foundry();
logger::info(MSG_CONTRACTS_CLEANING);
shell
.remove_path(path_to_foundry.join("artifacts"))
.context("artifacts")?;
shell
.remove_path(path_to_foundry.join("cache"))
.context("cache")?;
shell
.remove_path(path_to_foundry.join("cache-forge"))
.context("cache-forge")?;
shell
.remove_path(path_to_foundry.join("out"))
.context("out")?;
shell
.remove_path(path_to_foundry.join("typechain"))
.context("typechain")?;
shell
.remove_path(path_to_foundry.join("script-config"))
.context("remove script-config")?;
shell
.create_dir(path_to_foundry.join("script-config"))
.context("create script-config")?;
shell
.remove_path(path_to_foundry.join("script-out"))
.context("remove script-out")?;
shell
.create_dir(path_to_foundry.join("script-out"))
.context("create script-out")?;
logger::info(MSG_CONTRACTS_CLEANING_FINISHED);
Ok(())
}
1 change: 1 addition & 0 deletions zk_toolbox/crates/zk_supervisor/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod clean;
pub mod database;
pub mod test;
8 changes: 7 additions & 1 deletion zk_toolbox/crates/zk_supervisor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ use common::{
};
use config::EcosystemConfig;
use messages::{
msg_global_chain_does_not_exist, MSG_SUBCOMMAND_DATABASE_ABOUT, MSG_SUBCOMMAND_TESTS_ABOUT,
msg_global_chain_does_not_exist, MSG_SUBCOMMAND_CLEAN, MSG_SUBCOMMAND_DATABASE_ABOUT,
MSG_SUBCOMMAND_TESTS_ABOUT,
};
use xshell::Shell;

use crate::commands::clean::CleanCommands;

mod commands;
mod dals;
mod messages;
Expand All @@ -31,6 +34,8 @@ enum SupervisorSubcommands {
Database(DatabaseCommands),
#[command(subcommand, about = MSG_SUBCOMMAND_TESTS_ABOUT)]
Test(TestCommands),
#[command(subcommand, about = MSG_SUBCOMMAND_CLEAN)]
Clean(CleanCommands),
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -80,6 +85,7 @@ async fn run_subcommand(args: Supervisor, shell: &Shell) -> anyhow::Result<()> {
match args.command {
SupervisorSubcommands::Database(command) => commands::database::run(shell, command).await?,
SupervisorSubcommands::Test(command) => commands::test::run(shell, command)?,
SupervisorSubcommands::Clean(command) => commands::clean::run(shell, command)?,
}
Ok(())
}
Expand Down
10 changes: 10 additions & 0 deletions zk_toolbox/crates/zk_supervisor/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(super) fn msg_global_chain_does_not_exist(chain: &str, available_chains: &st
// Subcommands help
pub(super) const MSG_SUBCOMMAND_DATABASE_ABOUT: &str = "Database related commands";
pub(super) const MSG_SUBCOMMAND_TESTS_ABOUT: &str = "Run tests";
pub(super) const MSG_SUBCOMMAND_CLEAN: &str = "Clean artifacts";

// Database related messages
pub(super) const MSG_NO_DATABASES_SELECTED: &str = "No databases selected";
Expand Down Expand Up @@ -94,3 +95,12 @@ pub(super) const MSG_INTEGRATION_TESTS_BUILDING_CONTRACTS: &str = "Building test
pub(super) const MSG_REVERT_TEST_ENABLE_CONSENSUS_HELP: &str = "Enable consensus";
pub(super) const MSG_REVERT_TEST_RUN_INFO: &str = "Running revert and restart test";
pub(super) const MSG_REVERT_TEST_RUN_SUCCESS: &str = "Revert and restart test ran successfully";

// Cleaning related messages
pub(super) const MSG_DOCKER_COMPOSE_DOWN: &str = "docker compose down";
pub(super) const MSG_DOCKER_COMPOSE_REMOVE_VOLUMES: &str = "docker compose remove volumes";
pub(super) const MSG_DOCKER_COMPOSE_CLEANED: &str = "docker compose network cleaned";
pub(super) const MSG_CONTRACTS_CLEANING: &str =
"Removing contracts building and deployment artifacts";
pub(super) const MSG_CONTRACTS_CLEANING_FINISHED: &str =
"Contracts building and deployment artifacts are cleaned up";

0 comments on commit 52a4680

Please sign in to comment.