soil_cli/commands/
purge_chain_cmd.rs1use crate::{
8 error,
9 params::{DatabaseParams, SharedParams},
10 CliConfiguration,
11};
12use clap::Parser;
13use soil_service::DatabaseSource;
14use std::{
15 fmt::Debug,
16 fs,
17 io::{self, Write},
18};
19
20#[derive(Debug, Clone, Parser)]
22pub struct PurgeChainCmd {
23 #[arg(short = 'y')]
25 pub yes: bool,
26
27 #[allow(missing_docs)]
28 #[clap(flatten)]
29 pub shared_params: SharedParams,
30
31 #[allow(missing_docs)]
32 #[clap(flatten)]
33 pub database_params: DatabaseParams,
34}
35
36impl PurgeChainCmd {
37 pub fn run(&self, database_config: DatabaseSource) -> error::Result<()> {
39 let db_path = database_config.path().and_then(|p| p.parent()).ok_or_else(|| {
40 error::Error::Input("Cannot purge custom database implementation".into())
41 })?;
42
43 if !self.yes {
44 print!("Are you sure to remove {:?}? [y/N]: ", &db_path);
45 io::stdout().flush().expect("failed to flush stdout");
46
47 let mut input = String::new();
48 io::stdin().read_line(&mut input)?;
49 let input = input.trim();
50
51 match input.chars().next() {
52 Some('y') | Some('Y') => {},
53 _ => {
54 println!("Aborted");
55 return Ok(());
56 },
57 }
58 }
59
60 match fs::remove_dir_all(&db_path) {
61 Ok(_) => {
62 println!("{:?} removed.", &db_path);
63 Ok(())
64 },
65 Err(ref err) if err.kind() == io::ErrorKind::NotFound => {
66 eprintln!("{:?} did not exist.", &db_path);
67 Ok(())
68 },
69 Err(err) => Result::Err(err.into()),
70 }
71 }
72}
73
74impl CliConfiguration for PurgeChainCmd {
75 fn shared_params(&self) -> &SharedParams {
76 &self.shared_params
77 }
78
79 fn database_params(&self) -> Option<&DatabaseParams> {
80 Some(&self.database_params)
81 }
82}