use crate::aave::{self, AaveCommand};
use crate::cancel::CancelHandle;
use crate::context::CliContext;
use crate::database;
use crate::error::CliError;
use crate::exchange::{self, ExchangeCommand};
use crate::fleet::{self, FleetCommand};
use crate::path::{self, PathCommand};
use crate::pool::{self, PoolCommand};
use crate::prompt::{PromptPlan, Prompter};
use crate::report::CommandReport;
#[derive(Debug, Clone, PartialEq)]
pub enum Command {
Database(DatabaseCommand),
Exchange(ExchangeCommand),
Pool(PoolCommand),
Aave(AaveCommand),
Fleet(FleetCommand),
Path(PathCommand),
}
impl Command {
#[must_use]
pub fn prompt_plan(&self, ctx: &CliContext<'_>) -> PromptPlan {
match self {
Self::Database(command) => command.prompt_plan(ctx),
Self::Exchange(command) => command.prompt_plan(ctx),
Self::Pool(command) => command.prompt_plan(ctx),
Self::Aave(command) => command.prompt_plan(ctx),
Self::Fleet(command) => command.prompt_plan(ctx),
Self::Path(command) => command.prompt_plan(ctx),
}
}
pub fn execute(
&self,
ctx: &CliContext<'_>,
prompter: &dyn Prompter,
) -> Result<CommandReport, CliError> {
self.execute_with_cancel(ctx, prompter, &CancelHandle::new())
}
pub fn execute_with_cancel(
&self,
ctx: &CliContext<'_>,
prompter: &dyn Prompter,
cancel: &CancelHandle,
) -> Result<CommandReport, CliError> {
match self {
Self::Database(command) => {
database::execute(command, ctx, prompter).map(CommandReport::Database)
}
Self::Exchange(command) => {
exchange::execute(command, ctx, prompter).map(CommandReport::Exchange)
}
Self::Pool(command) => {
pool::execute(command, ctx, prompter, cancel).map(CommandReport::Pool)
}
Self::Aave(command) => {
aave::execute(command, ctx, prompter, cancel).map(CommandReport::Aave)
}
Self::Fleet(command) => fleet::execute(command, ctx).map(CommandReport::Fleet),
Self::Path(command) => path::execute(command, ctx).map(CommandReport::Path),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DatabaseCommand {
Backup,
Reset {
force: bool,
},
Upgrade {
force: bool,
},
Compact,
Cutover {
dry_run: bool,
force: bool,
},
Heal {
dry_run: bool,
force: bool,
},
Inspect,
}
impl DatabaseCommand {
#[must_use]
pub fn prompt_plan(&self, ctx: &CliContext<'_>) -> PromptPlan {
match self {
Self::Backup => PromptPlan::OnCondition(
database::database_backup_path(&ctx.database_path().value).exists(),
),
Self::Reset { .. }
| Self::Upgrade { .. }
| Self::Cutover { .. }
| Self::Heal { .. } => PromptPlan::UnlessForce,
Self::Compact | Self::Inspect => PromptPlan::None,
}
}
}