use std::path::PathBuf;
use anyhow::Result;
use clap::{Args, Parser, Subcommand};
use crate::commands;
#[derive(Debug, Parser)]
#[command(name = "neoengram", version, about)]
struct Cli {
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
Init(InitArgs),
Add(AddArgs),
Rm(RmArgs),
Status,
Commit(CommitArgs),
Log(LogArgs),
Show(ShowArgs),
Checkout(CheckoutArgs),
Recover(RecoverArgs),
Fsck,
}
#[derive(Debug, Args)]
struct InitArgs {
#[arg(value_name = "PATH", default_value = ".")]
path: PathBuf,
}
#[derive(Debug, Args)]
struct AddArgs {
#[arg(value_name = "PATH", default_value = ".")]
path: PathBuf,
#[arg(short = 'A', long)]
all: bool,
}
#[derive(Debug, Args)]
struct RmArgs {
#[arg(value_name = "PATH")]
path: PathBuf,
#[arg(long)]
cached: bool,
#[arg(short, long)]
force: bool,
}
#[derive(Debug, Args)]
struct CommitArgs {
#[arg(short, long, value_name = "MESSAGE")]
message: String,
}
#[derive(Debug, Args)]
struct LogArgs {
#[arg(short = 'n', long, value_name = "COUNT")]
max_count: Option<usize>,
}
#[derive(Debug, Args)]
struct ShowArgs {
#[arg(value_name = "TARGET", default_value = "HEAD")]
target: String,
}
#[derive(Debug, Args)]
struct CheckoutArgs {
#[arg(value_name = "TARGET", default_value = "HEAD")]
target: String,
#[arg(long)]
force: bool,
}
#[derive(Debug, Args)]
struct RecoverArgs {
#[arg(long)]
abort: bool,
}
pub(crate) async fn run() -> Result<()> {
match Cli::parse().command {
Command::Init(arguments) => commands::init::execute(arguments.path).await,
Command::Add(arguments) => {
if arguments.all {
commands::add::execute_with_options(arguments.path, true).await
} else {
commands::add::execute(arguments.path).await
}
}
Command::Rm(arguments) => {
commands::rm::execute(arguments.path, arguments.cached, arguments.force).await
}
Command::Status => commands::status::execute().await,
Command::Commit(arguments) => commands::commit::execute(arguments.message).await,
Command::Log(arguments) => commands::log::execute(arguments.max_count).await,
Command::Show(arguments) => commands::show::execute(arguments.target).await,
Command::Checkout(arguments) => {
commands::checkout::execute(arguments.target, arguments.force).await
}
Command::Recover(arguments) => commands::recover::execute(arguments.abort).await,
Command::Fsck => commands::fsck::execute().await,
}
}