From 0c6a4a72efd020e11dd2db64acd4f107116d71bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20K=C3=B6rber?= Date: Wed, 24 Nov 2021 17:22:10 +0100 Subject: [PATCH] Move repo-tree functionality into own subcommand --- src/cmd.rs | 16 +++++++-- src/lib.rs | 102 +++++++++++++++++++++++++++-------------------------- 2 files changed, 66 insertions(+), 52 deletions(-) diff --git a/src/cmd.rs b/src/cmd.rs index 608e6cc..e75bbd2 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -19,6 +19,20 @@ pub struct Opts { #[derive(Parser)] pub enum SubCommand { + #[clap(about = "Manage repositories")] + Repos(Repos), + #[clap(visible_alias = "wt", about = "Manage worktrees")] + Worktree(Worktree), +} + +#[derive(Parser)] +pub struct Repos { + #[clap(subcommand, name = "action")] + pub action: ReposAction, +} + +#[derive(Parser)] +pub enum ReposAction { #[clap( visible_alias = "run", about = "Synchronize the repositories to the configured values" @@ -28,8 +42,6 @@ pub enum SubCommand { Find(Find), #[clap(about = "Show status of configured repositories")] Status(OptionalConfig), - #[clap(visible_alias = "wt", about = "Manage worktrees")] - Worktree(Worktree), } #[derive(Parser)] diff --git a/src/lib.rs b/src/lib.rs index c18cb24..e887c39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -797,66 +797,68 @@ pub fn run() { let opts = cmd::parse(); match opts.subcmd { - cmd::SubCommand::Sync(sync) => { - let config = match config::read_config(&sync.config) { - Ok(c) => c, - Err(e) => { - print_error(&e); - process::exit(1); - } - }; - sync_trees(config); - } - cmd::SubCommand::Status(args) => match &args.config { - Some(config_path) => { - let config = match config::read_config(config_path) { + cmd::SubCommand::Repos(repos) => match repos.action { + cmd::ReposAction::Sync(sync) => { + let config = match config::read_config(&sync.config) { Ok(c) => c, Err(e) => { print_error(&e); process::exit(1); } }; - show_status(config); + sync_trees(config); } - None => { - let dir = match std::env::current_dir() { - Ok(d) => d, - Err(e) => { - print_error(&format!("Could not open current directory: {}", e)); - process::exit(1); - } - }; + cmd::ReposAction::Status(args) => match &args.config { + Some(config_path) => { + let config = match config::read_config(config_path) { + Ok(c) => c, + Err(e) => { + print_error(&e); + process::exit(1); + } + }; + show_status(config); + } + None => { + let dir = match std::env::current_dir() { + Ok(d) => d, + Err(e) => { + print_error(&format!("Could not open current directory: {}", e)); + process::exit(1); + } + }; - let has_worktree = dir.join(GIT_MAIN_WORKTREE_DIRECTORY).exists(); - show_single_repo_status(&dir, has_worktree); + let has_worktree = dir.join(GIT_MAIN_WORKTREE_DIRECTORY).exists(); + show_single_repo_status(&dir, has_worktree); + } + }, + cmd::ReposAction::Find(find) => { + let path = Path::new(&find.path); + if !path.exists() { + print_error(&format!("Path \"{}\" does not exist", path.display())); + process::exit(1); + } + let path = &path.canonicalize().unwrap(); + if !path.is_dir() { + print_error(&format!("Path \"{}\" is not a directory", path.display())); + process::exit(1); + } + + let trees = vec![find_in_tree(path).unwrap()]; + if trees.iter().all(|t| match &t.repos { + None => false, + Some(r) => r.is_empty(), + }) { + print_warning("No repositories found"); + } else { + let config = Config { trees }; + + let toml = toml::to_string(&config).unwrap(); + + print!("{}", toml); + } } }, - cmd::SubCommand::Find(find) => { - let path = Path::new(&find.path); - if !path.exists() { - print_error(&format!("Path \"{}\" does not exist", path.display())); - process::exit(1); - } - let path = &path.canonicalize().unwrap(); - if !path.is_dir() { - print_error(&format!("Path \"{}\" is not a directory", path.display())); - process::exit(1); - } - - let trees = vec![find_in_tree(path).unwrap()]; - if trees.iter().all(|t| match &t.repos { - None => false, - Some(r) => r.is_empty(), - }) { - print_warning("No repositories found"); - } else { - let config = Config { trees }; - - let toml = toml::to_string(&config).unwrap(); - - print!("{}", toml); - } - } cmd::SubCommand::Worktree(args) => { let dir = match std::env::current_dir() { Ok(d) => d,