Move repo-tree functionality into own subcommand

This commit is contained in:
2021-11-24 17:22:10 +01:00
parent e516a652f5
commit 0c6a4a72ef
2 changed files with 66 additions and 52 deletions

View File

@@ -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)]

View File

@@ -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,