Skip to content

Commit

Permalink
refactor: break out inner Subcommand from Cli
Browse files Browse the repository at this point in the history
This lets us add some flags in subsequent commits!
  • Loading branch information
ErichDonGubler committed Jan 3, 2022
1 parent cd1c38b commit f9e819a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
8 changes: 7 additions & 1 deletion src/bin/cpsc/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ use thiserror::Error as ThisError;
/// <https://github.com/capisco-dotfiles/capisco/LICENSE.md>.
#[derive(Debug, Parser)]
#[clap(author, version)]
pub(crate) enum Cli {
pub(crate) struct Cli {
#[clap(subcommand)]
pub subcommand: Subcommand,
}

#[derive(Debug, Parser)]
pub(crate) enum Subcommand {
/// Use a starter file to quickly import or export a configuration.
///
/// TODO: There's lots of ambitions for starter files, but they're yet to be fully designed or
Expand Down
30 changes: 17 additions & 13 deletions src/bin/cpsc/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use self::{
repo_db::{NewOverlayOptions, NewStandaloneOptions, RepoDb, RepoEntry},
};
use crate::cli::{
Cli, CliNewRepoName, CliRepoKind, ListFormat, OverlaySubcommand, RepoSpec, StandaloneSubcommand,
Cli, CliNewRepoName, CliRepoKind, ListFormat, OverlaySubcommand, RepoSpec,
StandaloneSubcommand, Subcommand,
};
use anyhow::{anyhow, bail, Context};
use format::lazy_format;
Expand Down Expand Up @@ -94,14 +95,15 @@ impl Runner {
}

pub(crate) fn run(&mut self, cli_args: Cli) -> anyhow::Result<()> {
let Cli { subcommand } = cli_args;
let log_registered = |name, repo: RepoEntry<'_>| {
log::info!("registered {:?} as {}", name, repo.short_desc());
};
match cli_args {
Cli::Starter(_subcmd) => {
match subcommand {
Subcommand::Starter(_subcmd) => {
bail!("`starter` commands are not implemented yet, stay tuned!")
}
Cli::Standalone(subcmd) => match subcmd {
Subcommand::Standalone(subcmd) => match subcmd {
StandaloneSubcommand::Init { path, name } => {
let Self { dirs, git, repos } = self;
let path = path.map(Ok).unwrap_or_else(current_dir)?;
Expand Down Expand Up @@ -183,7 +185,7 @@ impl Runner {
Ok(())
}
},
Cli::Overlay(subcmd) => match subcmd {
Subcommand::Overlay(subcmd) => match subcmd {
OverlaySubcommand::Init { name } => {
let Self { dirs, git, repos } = self;
let (name, repo) =
Expand Down Expand Up @@ -223,7 +225,7 @@ impl Runner {
Ok(())
}
},
Cli::Run {
Subcommand::Run {
repo_name,
cd_root,
cmd_and_args,
Expand Down Expand Up @@ -281,7 +283,7 @@ impl Runner {
// TODO: This `allow` is necessary, but `clippy` throws a false positive. We need
// to `collect` first in order to avoid borrowing `self` while iterating.
#[allow(clippy::needless_collect)]
Cli::ForEach {
Subcommand::ForEach {
no_cd_root,
cmd_and_args,
} => {
Expand All @@ -298,10 +300,12 @@ impl Runner {
repo_short_desc
);
match self
.run(Cli::Run {
repo_name: repo_name.clone(),
cd_root: !no_cd_root,
cmd_and_args: cmd_and_args.clone(),
.run(Cli {
subcommand: Subcommand::Run {
repo_name: repo_name.clone(),
cd_root: !no_cd_root,
cmd_and_args: cmd_and_args.clone(),
},
})
.with_context(|| anyhow!("failed to run command for repo {:?}", repo_name))
{
Expand All @@ -320,12 +324,12 @@ impl Runner {
Ok(())
}
}
Cli::Remove { name } => {
Subcommand::Remove { name } => {
let Self { dirs, git, repos } = self;
repos.try_remove_entire_repo(dirs, git, name)?;
Ok(())
}
Cli::List { repo_spec, format } => {
Subcommand::List { repo_spec, format } => {
let Self {
dirs,
git: _, // TODO: diagnostics for broken stuff? :D
Expand Down

0 comments on commit f9e819a

Please sign in to comment.