diff --git a/src/cli.rs b/src/cli.rs deleted file mode 100644 index b685c3e..0000000 --- a/src/cli.rs +++ /dev/null @@ -1,93 +0,0 @@ -//! This module contains the CLI entrypoint, CLI options and config generation based on the user's -//! settings and environment. - -use clap::Parser; -use gfold::RepositoryCollector; -use log::debug; -use std::env; - -use crate::config::{ColorMode, Config, DisplayMode}; -use crate::display::DisplayHarness; - -const HELP: &str = "\ -More information: https://github.com/nickgerace/gfold - -Description: this application helps you keep track of multiple Git repositories via CLI. By default, it displays relevant information for all repos in the current working directory. - -Config File Usage: while CLI options are prioritized, default options will fallback to the config file if it exists. Here are the config file lookup locations: - - $XDG_CONFIG_HOME/gfold.toml - $XDG_CONFIG_HOME/gfold/config.toml - $HOME/.config/gfold.toml (or {{FOLDERID_Profile}}\\.config\\gfold.toml on Windows) - -Troubleshooting: investigate unexpected behavior by prepending execution with \"RUST_BACKTRACE=1\"and \"RUST_LOG=debug\". You can adjust those variable's values to aid investigation."; - -#[derive(Parser)] -#[command(version, about = HELP, long_about = None)] -struct Cli { - /// specify path to target directory (defaults to current working directory) - path: Option, - - #[arg(short, long)] - color_mode: Option, - #[arg(short, long)] - display_mode: Option, - - /// display finalized config options and exit (merged options from an optional config file and command line arguments) - #[arg(long)] - dry_run: bool, - /// ignore config file settings - #[arg(short, long)] - ignore_config_file: bool, -} - -pub struct CliHarness { - cli: Cli, -} - -impl CliHarness { - /// Parse CLI arguments and store the result on the [`self`](Self). - pub fn new() -> Self { - let cli = Cli::parse(); - debug!("collected args"); - Self { cli } - } - - /// Merge configurations as needed, collect results and display them. - pub fn run(&self) -> anyhow::Result<()> { - let mut config = if self.cli.ignore_config_file { - Config::try_config_default()? - } else { - Config::try_config()? - }; - debug!("loaded initial config"); - - if let Some(found_display_mode_raw) = &self.cli.display_mode { - config.display_mode = *found_display_mode_raw; - } - - if let Some(found_color_mode) = &self.cli.color_mode { - config.color_mode = *found_color_mode; - } - - if let Some(found_path) = &self.cli.path { - config.path = env::current_dir()?.join(found_path).canonicalize()?; - } - - debug!("finalized config options"); - if self.cli.dry_run { - config.print()?; - } else { - let (include_email, include_submodules) = match config.display_mode { - DisplayMode::Classic => (false, false), - DisplayMode::Json => (true, true), - DisplayMode::Standard | DisplayMode::StandardAlphabetical => (true, false), - }; - let repository_collection = - RepositoryCollector::run(&config.path, include_email, include_submodules)?; - let display_harness = DisplayHarness::new(config.display_mode, config.color_mode); - display_harness.run(&repository_collection)?; - } - Ok(()) - } -} diff --git a/src/main.rs b/src/main.rs index 6a82bbd..3e3c832 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,20 +4,53 @@ #![warn(missing_docs, clippy::missing_errors_doc, clippy::missing_panics_doc)] +use crate::config::{ColorMode, Config, DisplayMode}; +use crate::display::DisplayHarness; +use gfold::RepositoryCollector; + +use clap::Parser; use env_logger::Builder; -use log::debug; -use log::LevelFilter; +use log::{debug, LevelFilter}; use std::env; -use crate::cli::CliHarness; - -mod cli; mod config; mod display; -/// Initializes the logger based on the debug flag and `RUST_LOG` environment variable and uses -/// the [`CliHarness`] to generate a [`Config`](config::Config). Then, this calls -/// [`CliHarness::run()`]. +const HELP: &str = "\ +More information: https://github.com/nickgerace/gfold + +Description: this application helps you keep track of multiple Git repositories via CLI. By default, it displays relevant information for all repos in the current working directory. + +Config File Usage: while CLI options are prioritized, default options will fallback to the config file if it exists. Here are the config file lookup locations: + + $XDG_CONFIG_HOME/gfold.toml + $XDG_CONFIG_HOME/gfold/config.toml + $HOME/.config/gfold.toml (or {{FOLDERID_Profile}}\\.config\\gfold.toml on Windows) + +Troubleshooting: investigate unexpected behavior by prepending execution with \"RUST_BACKTRACE=1\"and \"RUST_LOG=debug\". You can adjust those variable's values to aid investigation."; + +#[derive(Parser)] +#[command(version, about = HELP, long_about = None)] +struct Cli { + /// specify path to target directory (defaults to current working directory) + pub path: Option, + + #[arg(short, long)] + pub color_mode: Option, + #[arg(short, long)] + pub display_mode: Option, + + /// display finalized config options and exit (merged options from an optional config file and command line arguments) + #[arg(long)] + pub dry_run: bool, + /// ignore config file settings + #[arg(short, long)] + pub ignore_config_file: bool, +} + +/// Initializes the logger based on the debug flag and `RUST_LOG` environment variable, then +/// parses CLI arguments and generates a [`Config`](config::Config) by merging configurations as needed, +/// and finally collects results and displays them. fn main() -> anyhow::Result<()> { if env::var("RUST_LOG").is_err() { Builder::new().filter_level(LevelFilter::Off).init(); @@ -26,7 +59,37 @@ fn main() -> anyhow::Result<()> { } debug!("initialized logger"); - let cli_harness = CliHarness::new(); - cli_harness.run()?; + let cli = Cli::parse(); + let mut config = if cli.ignore_config_file { + Config::try_config_default()? + } else { + Config::try_config()? + }; + debug!("loaded initial config"); + + if let Some(found_display_mode_raw) = &cli.display_mode { + config.display_mode = *found_display_mode_raw; + } + if let Some(found_color_mode) = &cli.color_mode { + config.color_mode = *found_color_mode; + } + if let Some(found_path) = &cli.path { + config.path = env::current_dir()?.join(found_path).canonicalize()?; + } + debug!("finalized config options"); + + if cli.dry_run { + config.print()?; + } else { + let (include_email, include_submodules) = match config.display_mode { + DisplayMode::Classic => (false, false), + DisplayMode::Json => (true, true), + DisplayMode::Standard | DisplayMode::StandardAlphabetical => (true, false), + }; + let repository_collection = + RepositoryCollector::run(&config.path, include_email, include_submodules)?; + let display_harness = DisplayHarness::new(config.display_mode, config.color_mode); + display_harness.run(&repository_collection)?; + } Ok(()) }