Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #156 from subspace/wipe-flags
Browse files Browse the repository at this point in the history
farmer and node flags for wipe command
  • Loading branch information
ozgunozerk authored Mar 27, 2023
2 parents 274a0c6 + ec39b04 commit 9ca3ea4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 40 deletions.
72 changes: 40 additions & 32 deletions src/commands/wipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,50 @@ use crate::utils::{cache_directory_getter, node_directory_getter, plot_directory
/// implementation of the `wipe` command
///
/// wipes both farmer and node files (basically a fresh start)
pub(crate) async fn wipe() -> Result<()> {
let config = match parse_config() {
Ok(args) => Some(args),
Err(_) => {
println!(
"could not read your config. Wipe will still continue... \n{}",
"However, if you have set a custom location for your plots, you will need to \
manually delete your plots!"
.underline()
);
None
}
};
let node_directory = node_directory_getter();
let _ = Node::wipe(node_directory).await;
pub(crate) async fn wipe(wipe_farmer: bool, wipe_node: bool) -> Result<()> {
if wipe_node {
println!("wiping node...");
let node_directory = node_directory_getter();
let _ = Node::wipe(node_directory).await;
}

if wipe_farmer {
println!("wiping farmer...");
let config = match parse_config() {
Ok(args) => Some(args),
Err(_) => {
println!(
"could not read your config. Wipe will still continue... \n{}",
"However, if you have set a custom location for your plots, you will need to \
manually delete your plots!"
.underline()
);
None
}
};

// TODO: modify here when supporting multi-plot
// if config can be read, delete the farmer using the path in the config, else,
// delete the default location
if let Some(config) = config {
match PlotDescription::new(config.farmer.plot_directory, config.farmer.plot_size) {
Ok(plot) => {
let _ = plot.wipe().await;
// TODO: modify here when supporting multi-plot
// if config can be read, delete the farmer using the path in the config, else,
// delete the default location
if let Some(config) = config {
match PlotDescription::new(config.farmer.plot_directory, config.farmer.plot_size) {
Ok(plot) => {
let _ = plot.wipe().await;
}
Err(err) => println!(
"Skipping wiping plot. Got error while constructing the plot reference: {err}"
),
}
Err(err) => println!(
"Skipping wiping plot. Got error while constructing the plot reference: {err}"
),
let _ =
CacheDescription::new(cache_directory_getter(), config.farmer.advanced.cache_size)?
.wipe()
.await;
} else {
let _ = tokio::fs::remove_dir_all(plot_directory_getter()).await;
}
let _ = CacheDescription::new(cache_directory_getter(), config.farmer.advanced.cache_size)?
.wipe()
.await;
} else {
let _ = tokio::fs::remove_dir_all(plot_directory_getter()).await;
}

delete_summary().await;
delete_summary().await;
}

println!("Wipe successful!");

Expand Down
34 changes: 26 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ mod utils;
use clap::{Parser, Subcommand};
use color_eyre::eyre::Report;
use color_eyre::Help;
use commands::farm::farm;
use commands::info::info;
use commands::init::init;
use commands::wipe::wipe;
use tracing::instrument;

use crate::utils::support_message;
use crate::commands::farm::farm;
use crate::commands::info::info;
use crate::commands::init::init;
use crate::commands::wipe::wipe;
use crate::utils::{get_user_input, support_message, yes_or_no_parser};

#[cfg(all(
target_arch = "x86_64",
Expand Down Expand Up @@ -56,7 +56,12 @@ enum Commands {
executor: bool,
},
#[command(about = "wipes the node and farm instance (along with your plots)")]
Wipe,
Wipe {
#[arg(long, action)]
farmer: bool,
#[arg(long, action)]
node: bool,
},
}

#[tokio::main]
Expand All @@ -73,8 +78,21 @@ async fn main() -> Result<(), Report> {
Commands::Farm { verbose, executor } => {
farm(verbose, executor).await.suggestion(support_message())?;
}
Commands::Wipe => {
wipe().await?;
Commands::Wipe { mut farmer, mut node } => {
// if user did not supply any argument, this means user wants to delete them
// both, but `farmer` and `node` are both false at the moment
if !farmer && !node {
let prompt = "This will delete both farmer and node (complete wipe). Do you want \
to proceed? [y/n]";
if let Ok(false) = get_user_input(prompt, None, yes_or_no_parser) {
println!("Wipe operation aborted, nothing has been deleted...");
return Ok(());
}

farmer = true;
node = true;
}
wipe(farmer, node).await.suggestion(support_message())?;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ pub(crate) fn size_parser(size: &str) -> Result<ByteSize> {
}
}

pub(crate) fn yes_or_no_parser(answer: &str) -> Result<bool> {
match answer.to_lowercase().as_str() {
"y" | "yes" => Ok(true),
"n" | "no" => Ok(false),
_ => Err(eyre!("could not interpret your answer. Please provide `y` or `n`.")),
}
}

/// generates a plot path from the given path
pub(crate) fn plot_directory_getter() -> PathBuf {
data_dir_getter().join("plots")
Expand Down

0 comments on commit 9ca3ea4

Please sign in to comment.