use anyhow::Result;
use clap::{Parser, Subcommand};
use std::env;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(arg_required_else_help = true)]
struct Cli {
#[command(subcommand)]
command: Option<Commands>,
}
#[derive(Subcommand)]
enum Commands {
Add {
name: String,
},
List,
Remove {
name: String,
},
Status,
Pull {
#[arg(long, short)]
all: bool,
name: Option<String>,
},
Prune {
#[arg(long)]
dry_run: bool,
#[arg(long, short)]
force: bool,
},
}
fn main() {
if let Err(e) = run() {
eprintln!("Error: {}", e);
std::process::exit(1);
}
}
fn run() -> Result<()> {
let cli = Cli::parse();
match &cli.command {
Some(Commands::Add { name }) => {
let current_dir = env::current_dir()?;
let repo = gwtr::ensure_git_repository(¤t_dir)?;
gwtr::create_worktree(&repo, name)?;
}
Some(Commands::List) => {
let current_dir = env::current_dir()?;
let repo = gwtr::ensure_git_repository(¤t_dir)?;
gwtr::list_worktrees(&repo)?;
}
Some(Commands::Remove { name }) => {
let current_dir = env::current_dir()?;
let repo = gwtr::ensure_git_repository(¤t_dir)?;
gwtr::remove_worktree(&repo, name)?;
}
Some(Commands::Status) => {
let current_dir = env::current_dir()?;
let repo = gwtr::ensure_git_repository(¤t_dir)?;
gwtr::show_worktrees_status(&repo)?;
}
Some(Commands::Pull { all, name }) => {
let current_dir = env::current_dir()?;
let repo = gwtr::ensure_git_repository(¤t_dir)?;
if *all {
gwtr::pull_all_worktrees(&repo)?;
} else if let Some(worktree_name) = name {
gwtr::pull_worktree(&repo, worktree_name)?;
} else {
gwtr::pull_current_worktree(&repo)?;
}
}
Some(Commands::Prune { dry_run, force }) => {
let current_dir = env::current_dir()?;
let repo = gwtr::ensure_git_repository(¤t_dir)?;
gwtr::prune_merged_worktrees(&repo, *dry_run, *force)?;
}
None => {
}
}
Ok(())
}