use clap::{Parser, Subcommand};
use colored::Colorize;
use gait::{
init_logger,
remote::{
check,
common::{Parsed, Target, sequence},
sync,
},
};
use std::process::exit;
pub use gait::{CachedRepository, RepositoryConfiguration, WireOperation};
#[derive(Parser)]
#[command(version, author, about, long_about = None)]
struct Cli {
#[command(subcommand)]
command: Command,
#[arg(global = true, short, long)]
name: Option<String>,
#[arg(global = true, short, long)]
target: Option<String>,
#[arg(global = true, short, long)]
singlethread: bool,
}
#[derive(Subcommand)]
enum Command {
Sync,
Check,
DirectSync {
#[arg(long)]
url: String,
#[arg(long)]
rev: String,
#[arg(long)]
src: String,
#[arg(long)]
dst: String,
},
DirectCheck {
#[arg(long)]
url: String,
#[arg(long)]
rev: String,
#[arg(long)]
src: String,
#[arg(long)]
dst: String,
},
}
#[tokio::main]
async fn main() {
init_logger();
let cli = Cli::parse();
let target = cli.target.or(cli.name);
let mode = if cli.singlethread {
sequence::Mode::Single
} else {
sequence::Mode::Parallel
};
let result = match cli.command {
Command::Sync => sync::sync_with_caching(&Target::Declared(target), mode).await,
Command::Check => check::check(Target::Declared(target), &mode),
Command::DirectSync { url, rev, src, dst } => {
sync::sync_with_caching(
&Target::Direct(Parsed {
name: None,
dsc: None,
mtd: None,
url,
rev,
src,
dst,
}),
mode,
)
.await
}
Command::DirectCheck { url, rev, src, dst } => check::check(
Target::Direct(Parsed {
name: None,
dsc: None,
mtd: None,
url,
rev,
src,
dst,
}),
&mode,
),
};
match result.as_ref() {
Ok(true) => println!("{}", "Success".green().bold()),
Ok(false) => println!("{}", "Failure".red().bold()),
Err(e) => eprintln!("{}", e.to_string().red().bold()),
}
match result {
Ok(true) => exit(0),
_ => exit(1),
}
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Cli::command().debug_assert();
}