mirror of
https://github.com/msfjarvis/gitice
synced 2025-08-14 11:47:01 +05:30
switch to clap's derive macros
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
5eff1508ac
commit
27e73863ec
2 changed files with 39 additions and 43 deletions
31
src/cli.rs
Normal file
31
src/cli.rs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
use clap::{crate_version, Clap};
|
||||||
|
|
||||||
|
#[derive(Clap)]
|
||||||
|
#[clap(version = crate_version!(), author = "Harsh Shandilya <me@msfjarvis.dev>")]
|
||||||
|
pub(crate) struct Opts {
|
||||||
|
#[clap(subcommand)]
|
||||||
|
pub(crate) subcommand: SubCommand,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clap)]
|
||||||
|
pub(crate) enum SubCommand {
|
||||||
|
Freeze(Freeze),
|
||||||
|
Thaw(Thaw),
|
||||||
|
}
|
||||||
|
|
||||||
|
/// recursively find git repos and record their states into a lockfile
|
||||||
|
#[derive(Clap)]
|
||||||
|
pub(crate) struct Freeze {
|
||||||
|
/// directory to search and freeze repos from.
|
||||||
|
pub(crate) directory: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// takes the given
|
||||||
|
#[derive(Clap)]
|
||||||
|
pub(crate) struct Thaw {
|
||||||
|
/// directory to put cloned repos into.
|
||||||
|
pub(crate) directory: String,
|
||||||
|
/// the lockfile to restore repositories from.
|
||||||
|
#[clap(default_value = "gitice.lock")]
|
||||||
|
pub(crate) lockfile: String,
|
||||||
|
}
|
51
src/main.rs
51
src/main.rs
|
@ -1,54 +1,19 @@
|
||||||
|
pub(crate) mod cli;
|
||||||
pub(crate) mod git;
|
pub(crate) mod git;
|
||||||
pub(crate) mod model;
|
pub(crate) mod model;
|
||||||
|
|
||||||
|
use clap::Clap;
|
||||||
|
use cli::Opts;
|
||||||
|
use cli::SubCommand;
|
||||||
use git::freeze_repos;
|
use git::freeze_repos;
|
||||||
use git::thaw_repos;
|
use git::thaw_repos;
|
||||||
|
|
||||||
use anyhow::anyhow;
|
|
||||||
use clap::{crate_version, App, AppSettings, Arg};
|
|
||||||
|
|
||||||
fn main() -> anyhow::Result<()> {
|
fn main() -> anyhow::Result<()> {
|
||||||
let matches = App::new("gitice")
|
let opts = Opts::parse();
|
||||||
.about("Command-line tool for backing up and restoring multiple Git repositories from a directory")
|
|
||||||
.version(crate_version!())
|
|
||||||
.setting(AppSettings::ColoredHelp)
|
|
||||||
.setting(AppSettings::DeriveDisplayOrder)
|
|
||||||
.setting(AppSettings::SubcommandRequiredElseHelp)
|
|
||||||
.subcommand(
|
|
||||||
App::new("freeze")
|
|
||||||
.about("Generate a gitice.lock file with all the repositories in the given directory")
|
|
||||||
.setting(AppSettings::ColoredHelp)
|
|
||||||
.args(&[Arg::new("directory")
|
|
||||||
.about("Directory to look for Git repos in")
|
|
||||||
.required(true)
|
|
||||||
.index(1)]),
|
|
||||||
)
|
|
||||||
.subcommand(
|
|
||||||
App::new("thaw")
|
|
||||||
.about("Given a gitice.lock and a directory, clones back all the repositories from the lockfile in the directory")
|
|
||||||
.setting(AppSettings::ColoredHelp)
|
|
||||||
.args(&[
|
|
||||||
Arg::new("directory")
|
|
||||||
.about("Directory to restore repositories in")
|
|
||||||
.required(true)
|
|
||||||
.index(1),
|
|
||||||
Arg::new("lockfile")
|
|
||||||
.about("The lockfile to restore repositories from")
|
|
||||||
.short('l')
|
|
||||||
.long("lockfile")
|
|
||||||
.required(false)
|
|
||||||
.default_value("gitice.lock")
|
|
||||||
]),
|
|
||||||
)
|
|
||||||
.get_matches();
|
|
||||||
|
|
||||||
match matches.subcommand().unwrap() {
|
match opts.subcommand {
|
||||||
("freeze", m) => freeze_repos(m.value_of("directory").unwrap())?,
|
SubCommand::Freeze(p) => freeze_repos(&p.directory)?,
|
||||||
("thaw", m) => thaw_repos(
|
SubCommand::Thaw(p) => thaw_repos(&p.directory, &p.lockfile)?,
|
||||||
m.value_of("directory").unwrap(),
|
|
||||||
m.value_of("lockfile").unwrap(),
|
|
||||||
)?,
|
|
||||||
(cmd, _) => return Err(anyhow!("unknown subcommand: {}", cmd)),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue