Skip to content

Commit

Permalink
Use parameter in all subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezzfelipe committed Oct 8, 2024
1 parent c731ab9 commit a0045fe
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/bin/dolos/bootstrap/mithril.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,12 @@ fn import_hardano_into_wal(
pub fn run(config: &crate::Config, args: &Args, feedback: &Feedback) -> miette::Result<()> {
//crate::common::setup_tracing(&config.logging)?;

if args.skip_if_not_empty & crate::common::open_wal(config).is_ok() {
info!("Skipping bootstrap, data already present.");
return Ok(());
if args.skip_if_not_empty {
// Open WAL in closure to ensure it is dropped before continuing.
let should_skip = crate::common::open_wal(config).is_ok();
if should_skip {
return Ok(());
}
}

let mithril = config
Expand Down
39 changes: 27 additions & 12 deletions src/bin/dolos/bootstrap/relay.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
use dolos::wal::redb::WalStore;
use miette::{bail, Context, IntoDiagnostic};
use tracing::info;

use crate::feedback::Feedback;

#[derive(Debug, clap::Args, Default)]
pub struct Args {}
pub struct Args {
/// Skip the bootstrap if there's already data in the stores
#[arg(long, action)]
skip_if_not_empty: bool,
}

fn open_empty_wal(config: &crate::Config) -> miette::Result<WalStore> {
fn open_empty_wal(config: &crate::Config, args: &Args) -> miette::Result<Option<WalStore>> {
let wal = crate::common::open_wal(config)?;

let is_empty = wal.is_empty().into_diagnostic()?;

if !is_empty {
bail!("can't continue with data already available");
if args.skip_if_not_empty {
return Ok(None);
} else {
bail!("can't continue with data already available");
}
}

Ok(wal)
Ok(Some(wal))
}

pub fn run(config: &crate::Config, _args: &Args, _feedback: &Feedback) -> miette::Result<()> {
let mut wal = open_empty_wal(config).context("opening WAL")?;

wal.initialize_from_origin()
.into_diagnostic()
.context("initializing WAL")?;

Ok(())
pub fn run(config: &crate::Config, args: &Args, _feedback: &Feedback) -> miette::Result<()> {
match open_empty_wal(config, args).context("opening WAL")? {
Some(mut wal) => {
wal.initialize_from_origin()
.into_diagnostic()
.context("initializing WAL")?;

Ok(())
}
None => {
info!("Skipping bootstrap, data already present.");
Ok(())
}
}
}
21 changes: 17 additions & 4 deletions src/bin/dolos/bootstrap/snapshot.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
use flate2::read::GzDecoder;
use miette::{Context, IntoDiagnostic};
use tar::Archive;
use tracing::info;

use crate::feedback::{Feedback, ProgressReader};

#[derive(Debug, clap::Args, Default)]
pub struct Args {}
pub struct Args {
/// Skip the bootstrap if there's already data in the stores
#[arg(long, action)]
skip_if_not_empty: bool,
}

fn fetch_snapshot(config: &crate::Config, feedback: &Feedback) -> miette::Result<()> {
fn fetch_snapshot(config: &crate::Config, feedback: &Feedback, args: &Args) -> miette::Result<()> {
let snapshot_url = config
.snapshot
.as_ref()
.map(|x| &x.download_url)
.ok_or_else(|| miette::miette!("Snapshot URL not specified in config"))?;

// Check if exists and is not empty.
if let Ok(mut entries) = std::fs::read_dir(&config.storage.path) {
if entries.next().is_some() && args.skip_if_not_empty {
info!("Skipping bootstrap, data already present.");
return Ok(());
}
}

std::fs::create_dir_all(&config.storage.path)
.into_diagnostic()
.context("Failed to create target directory")?;
Expand Down Expand Up @@ -48,8 +61,8 @@ fn fetch_snapshot(config: &crate::Config, feedback: &Feedback) -> miette::Result
Ok(())
}

pub fn run(config: &crate::Config, _args: &Args, feedback: &Feedback) -> miette::Result<()> {
fetch_snapshot(config, feedback)?;
pub fn run(config: &crate::Config, args: &Args, feedback: &Feedback) -> miette::Result<()> {
fetch_snapshot(config, feedback, args)?;

Ok(())
}

0 comments on commit a0045fe

Please sign in to comment.