use clap::{CommandFactory, Parser};
use git_workspace::commands::{
add_provider_to_config, archive, completion, execute_cmd, fetch, list, lock,
pull_all_repositories, update,
};
use git_workspace::config::ProviderSource;
use git_workspace::utils::{ensure_workspace_dir_exists, expand_workspace_path};
use std::path::PathBuf;
#[derive(clap::Parser)]
#[command(name = "git-workspace", author, about, version)]
struct Args {
#[arg(short = 'w', long = "workspace", env = "GIT_WORKSPACE")]
workspace: PathBuf,
#[command(subcommand)]
command: Command,
}
#[derive(clap::Parser)]
enum Command {
Update {
#[arg(short = 't', long = "threads", default_value = "8")]
threads: usize,
},
Fetch {
#[arg(short = 't', long = "threads", default_value = "8")]
threads: usize,
},
Lock {},
SwitchAndPull {
#[arg(short = 't', long = "threads", default_value = "8")]
threads: usize,
},
List {
#[arg(long = "full")]
full: bool,
},
Archive {
#[arg(long = "force")]
force: bool,
},
Run {
#[arg(short = 't', long = "threads", default_value = "8")]
threads: usize,
#[arg(required = true)]
command: String,
args: Vec<String>,
},
Add {
#[arg(long = "file", default_value = "workspace.toml")]
file: PathBuf,
#[command(subcommand)]
command: ProviderSource,
},
Completion {
shell: clap_complete::Shell,
},
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
handle_main(args)
}
fn handle_main(args: Args) -> anyhow::Result<()> {
let workspace_path = expand_workspace_path(&args.workspace)?;
let workspace_path = ensure_workspace_dir_exists(&workspace_path)?;
match args.command {
Command::List { full } => list(&workspace_path, full)?,
Command::Update { threads } => {
lock(&workspace_path)?;
update(&workspace_path, threads)?
}
Command::Lock {} => {
lock(&workspace_path)?;
}
Command::Archive { force } => archive(&workspace_path, force)?,
Command::Fetch { threads } => fetch(&workspace_path, threads)?,
Command::Add { file, command } => add_provider_to_config(&workspace_path, command, &file)?,
Command::Run {
threads,
command,
args,
} => execute_cmd(&workspace_path, threads, command, args)?,
Command::SwitchAndPull { threads } => pull_all_repositories(&workspace_path, threads)?,
Command::Completion { shell } => completion(shell, &mut Args::command())?,
};
Ok(())
}