Skip to content

Commit b749238

Browse files
authored
Consolidate cli.rs into main.rs (#279)
1 parent bb4aa6a commit b749238

File tree

2 files changed

+73
-103
lines changed

2 files changed

+73
-103
lines changed

Diff for: src/cli.rs

-93
This file was deleted.

Diff for: src/main.rs

+73-10
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,53 @@
44
55
#![warn(missing_docs, clippy::missing_errors_doc, clippy::missing_panics_doc)]
66

7+
use crate::config::{ColorMode, Config, DisplayMode};
8+
use crate::display::DisplayHarness;
9+
use gfold::RepositoryCollector;
10+
11+
use clap::Parser;
712
use env_logger::Builder;
8-
use log::debug;
9-
use log::LevelFilter;
13+
use log::{debug, LevelFilter};
1014
use std::env;
1115

12-
use crate::cli::CliHarness;
13-
14-
mod cli;
1516
mod config;
1617
mod display;
1718

18-
/// Initializes the logger based on the debug flag and `RUST_LOG` environment variable and uses
19-
/// the [`CliHarness`] to generate a [`Config`](config::Config). Then, this calls
20-
/// [`CliHarness::run()`].
19+
const HELP: &str = "\
20+
More information: https://github.com/nickgerace/gfold
21+
22+
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.
23+
24+
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:
25+
26+
$XDG_CONFIG_HOME/gfold.toml
27+
$XDG_CONFIG_HOME/gfold/config.toml
28+
$HOME/.config/gfold.toml (or {{FOLDERID_Profile}}\\.config\\gfold.toml on Windows)
29+
30+
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.";
31+
32+
#[derive(Parser)]
33+
#[command(version, about = HELP, long_about = None)]
34+
struct Cli {
35+
/// specify path to target directory (defaults to current working directory)
36+
pub path: Option<String>,
37+
38+
#[arg(short, long)]
39+
pub color_mode: Option<ColorMode>,
40+
#[arg(short, long)]
41+
pub display_mode: Option<DisplayMode>,
42+
43+
/// display finalized config options and exit (merged options from an optional config file and command line arguments)
44+
#[arg(long)]
45+
pub dry_run: bool,
46+
/// ignore config file settings
47+
#[arg(short, long)]
48+
pub ignore_config_file: bool,
49+
}
50+
51+
/// Initializes the logger based on the debug flag and `RUST_LOG` environment variable, then
52+
/// parses CLI arguments and generates a [`Config`](config::Config) by merging configurations as needed,
53+
/// and finally collects results and displays them.
2154
fn main() -> anyhow::Result<()> {
2255
if env::var("RUST_LOG").is_err() {
2356
Builder::new().filter_level(LevelFilter::Off).init();
@@ -26,7 +59,37 @@ fn main() -> anyhow::Result<()> {
2659
}
2760
debug!("initialized logger");
2861

29-
let cli_harness = CliHarness::new();
30-
cli_harness.run()?;
62+
let cli = Cli::parse();
63+
let mut config = if cli.ignore_config_file {
64+
Config::try_config_default()?
65+
} else {
66+
Config::try_config()?
67+
};
68+
debug!("loaded initial config");
69+
70+
if let Some(found_display_mode_raw) = &cli.display_mode {
71+
config.display_mode = *found_display_mode_raw;
72+
}
73+
if let Some(found_color_mode) = &cli.color_mode {
74+
config.color_mode = *found_color_mode;
75+
}
76+
if let Some(found_path) = &cli.path {
77+
config.path = env::current_dir()?.join(found_path).canonicalize()?;
78+
}
79+
debug!("finalized config options");
80+
81+
if cli.dry_run {
82+
config.print()?;
83+
} else {
84+
let (include_email, include_submodules) = match config.display_mode {
85+
DisplayMode::Classic => (false, false),
86+
DisplayMode::Json => (true, true),
87+
DisplayMode::Standard | DisplayMode::StandardAlphabetical => (true, false),
88+
};
89+
let repository_collection =
90+
RepositoryCollector::run(&config.path, include_email, include_submodules)?;
91+
let display_harness = DisplayHarness::new(config.display_mode, config.color_mode);
92+
display_harness.run(&repository_collection)?;
93+
}
3194
Ok(())
3295
}

0 commit comments

Comments
 (0)