Skip to content

Commit

Permalink
Merge pull request #41 from anton-rs/10-07-feat_cli_subcommands
Browse files Browse the repository at this point in the history
10 07 feat cli subcommands
  • Loading branch information
refcell authored Oct 7, 2023
2 parents 5839a0b + dea7124 commit f160592
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 86 deletions.
25 changes: 0 additions & 25 deletions LICENSE

This file was deleted.

24 changes: 13 additions & 11 deletions bin/opup/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::{ArgAction, Parser, Subcommand};
use eyre::Result;
use std::path::PathBuf;

use crate::up::UpCommand;

/// Command line arguments
#[derive(Parser, Debug)]
Expand All @@ -13,17 +14,13 @@ pub struct Args {
/// The subcommand to run
#[clap(subcommand)]
pub command: Option<Command>,

/// An optional path to a stack config file.
#[arg(long, short)]
config: Option<PathBuf>,
}

/// Possible CLI subcommands
#[derive(Debug, Subcommand)]
pub enum Command {
/// Build and run the devnet stack
Up,
Up(UpCommand),
/// Bring the devnet stack down
Down,
/// Nuke the devnet stack
Expand All @@ -33,17 +30,22 @@ pub enum Command {
}

pub fn run() -> Result<()> {
let Args { v, config, command } = Args::parse();
let Args { v, command } = Args::parse();

crate::telemetry::init_tracing_subscriber(v)?;

crate::banners::banner()?;

match command {
Some(Command::Up) | None => crate::stack::Stack::new(config).run()?,
Some(Command::Down) => unimplemented!("down command not yet implemented"),
Some(Command::Nuke) => unimplemented!("nuke command not yet implemented"),
Some(Command::Clean) => unimplemented!("clean command not yet implemented"),
// If no subcommand is provided, run the Up command with default config.
None => UpCommand::new(None, false).run()?,

Some(command) => match command {
Command::Up(up_command) => up_command.run()?,
Command::Down => unimplemented!("down command not yet implemented"),
Command::Nuke => unimplemented!("nuke command not yet implemented"),
Command::Clean => unimplemented!("clean command not yet implemented"),
},
}

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions bin/opup/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/// The CLI entrypoint for the binary.
pub mod cli;

/// Module containing stack packages.
pub mod stack;

/// Command banners.
pub mod banners;

Expand All @@ -12,3 +9,6 @@ pub mod telemetry;

/// Runner contains asynchronous helpers for running commands.
pub mod runner;

/// The Up subcommand module that contains the logic for bringing up the stack.
pub mod up;
43 changes: 0 additions & 43 deletions bin/opup/src/stack.rs

This file was deleted.

56 changes: 56 additions & 0 deletions bin/opup/src/up.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use bollard::Docker;
use clap::Args;
use eyre::Result;
use std::path::{Path, PathBuf};

use op_config::Config;
use op_stages::Stages;

/// The Up CLI Subcommand.
#[derive(Debug, Args)]
pub struct UpCommand {
/// An optional path to a stack config file.
#[arg(long, short)]
pub config: Option<PathBuf>,

/// Whether to build a hard-coded default devnet stack, ignoring the config file.
#[arg(long, short)]
pub devnet: bool,
}

impl UpCommand {
/// Create a new Up CLI Subcommand.
pub fn new(config: Option<PathBuf>, devnet: bool) -> Self {
Self { config, devnet }
}

/// Run the Up CLI Subcommand.
pub fn run(&self) -> Result<()> {
crate::runner::run_until_ctrl_c(async {
tracing::info!(target: "cli", "bootstrapping op stack");

// todo: remove this once we have a proper stage docker component
// for now, this placeholds use of [bollard].
let docker = Docker::connect_with_local_defaults()?;
let version = docker.version().await?;
tracing::info!(target: "cli", "docker version: {:?}", version);

if self.devnet {
tracing::info!(target: "cli", "Building default devnet stack");
Stages::from(Config::default()).execute().await
} else {
// Get the directory of the config file if it exists.
let config_dir = self.config.as_ref().and_then(|p| p.parent());
let config_dir = config_dir.unwrap_or_else(|| Path::new("."));

// Build a config from the parsed config directory.
tracing::info!(target: "cli", "Loading op-stack config from {:?}", config_dir);
let stack = Config::load_with_root(config_dir);

tracing::info!(target: "cli", "Stack: {:#?}", stack);

Stages::from(stack).execute().await
}
})
}
}
8 changes: 4 additions & 4 deletions crates/config/tests/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ fn test_default_config() {
assert_eq!(config.rollup_client, RollupClient::default());
assert_eq!(config.challenger, ChallengerAgent::default());

assert_eq!(config.enable_sequencing, false);
assert_eq!(config.enable_fault_proofs, false);
assert!(!config.enable_sequencing);
assert!(!config.enable_fault_proofs);
}

#[test]
Expand Down Expand Up @@ -85,8 +85,8 @@ fn test_read_config_from_toml() {
assert_eq!(config.l2_client, L2Client::OpReth);
assert_eq!(config.rollup_client, RollupClient::Magi);
assert_eq!(config.challenger, ChallengerAgent::OpChallengerGo);
assert_eq!(config.enable_sequencing, true);
assert_eq!(config.enable_fault_proofs, true);
assert!(!config.enable_sequencing);
assert!(!config.enable_fault_proofs);
}

#[test]
Expand Down

0 comments on commit f160592

Please sign in to comment.