-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cli): merge argument parsing and command execution (#1568)
## Summary Refactors the CLI to give it a cleaner structure. ## Background The CLI was split into a `cli` module that was concerned with parsing arguments, and a `commands` which consumed the argument data structures. The patch clears this up by implementing inherent `<Args>::run` methods on all leaf argument types, so that the implementation follows the same structure for all its effects. Note that this match does restructure the public facing command line (except for a few lines of documentation). It merely restructures the implementation. ## Changes - Refactors each type deriving `clap::Args` to have an inherent method `run`. - Flattens the crate structure to not be unnecessarily nested. ## Testing These will be tested in follow PRs. Smoke tests using a subset of the astria CLI commands should still work. ## Related Issues Closes #1546
- Loading branch information
1 parent
939a689
commit 29a5d19
Showing
23 changed files
with
1,233 additions
and
634 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
mod collect; | ||
mod submit; | ||
|
||
use std::{ | ||
collections::BTreeMap, | ||
path::{ | ||
Path, | ||
PathBuf, | ||
}, | ||
}; | ||
|
||
use astria_core::protocol::transaction::v1alpha1::Action; | ||
use clap::Subcommand; | ||
use color_eyre::eyre::{ | ||
self, | ||
ensure, | ||
WrapErr as _, | ||
}; | ||
use tracing::instrument; | ||
|
||
/// Interact with a Sequencer node | ||
#[derive(Debug, clap::Args)] | ||
pub(super) struct Command { | ||
#[command(subcommand)] | ||
command: SubCommand, | ||
} | ||
|
||
impl Command { | ||
pub(super) async fn run(self) -> eyre::Result<()> { | ||
match self.command { | ||
SubCommand::CollectWithdrawals(args) => args.run().await, | ||
SubCommand::SubmitWithdrawals(args) => args.run().await, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Subcommand)] | ||
enum SubCommand { | ||
/// Commands for interacting with Sequencer accounts | ||
CollectWithdrawals(collect::Command), | ||
SubmitWithdrawals(submit::Command), | ||
} | ||
|
||
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] | ||
#[serde(transparent)] | ||
struct ActionsByRollupHeight(BTreeMap<u64, Vec<Action>>); | ||
|
||
impl ActionsByRollupHeight { | ||
fn new() -> Self { | ||
Self(BTreeMap::new()) | ||
} | ||
|
||
fn into_inner(self) -> BTreeMap<u64, Vec<Action>> { | ||
self.0 | ||
} | ||
|
||
#[instrument(skip_all, err)] | ||
fn insert(&mut self, rollup_height: u64, actions: Vec<Action>) -> eyre::Result<()> { | ||
ensure!( | ||
self.0.insert(rollup_height, actions).is_none(), | ||
"already collected actions for block at rollup height `{rollup_height}`; no 2 blocks \ | ||
with the same height should have been seen", | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[instrument(skip_all, fields(target = %output.path.display()), err)] | ||
fn write_to_output(self, output: Output) -> eyre::Result<()> { | ||
let writer = std::io::BufWriter::new(output.handle); | ||
serde_json::to_writer(writer, &self.0).wrap_err("failed writing actions to file") | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Output { | ||
handle: std::fs::File, | ||
path: PathBuf, | ||
} | ||
|
||
#[instrument(skip(target), fields(target = %target.as_ref().display()), err)] | ||
fn open_output<P: AsRef<Path>>(target: P, overwrite: bool) -> eyre::Result<Output> { | ||
let handle = if overwrite { | ||
let mut options = std::fs::File::options(); | ||
options.write(true).create(true).truncate(true); | ||
options | ||
} else { | ||
let mut options = std::fs::File::options(); | ||
options.write(true).create_new(true); | ||
options | ||
} | ||
.open(&target) | ||
.wrap_err("failed to open specified file for writing")?; | ||
Ok(Output { | ||
handle, | ||
path: target.as_ref().to_path_buf(), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.