Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add paths configuration option, deprecate path option #280

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ gfold -d classic -c never ~/ --dry-run > $HOME/.config/gfold.toml
Here are the contents of the resulting config file:

```toml
path = '/home/neloth'
paths = ['/home/neloth']
display_mode = 'Classic'
color_mode = 'Never'
```
Expand Down
29 changes: 23 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use std::{env, fs};
/// deserialize empty, non-existent, partial, and complete config files.
#[derive(Serialize)]
pub struct Config {
/// The path that `gfold` will begin traversal in and collect results from.
pub path: PathBuf,
/// The paths that `gfold` will traverse and collect results from.
pub paths: Vec<PathBuf>,
/// The display format for results printed to `stdout`.
pub display_mode: DisplayMode,
/// The color mode for results printed to `stdout`.
Expand Down Expand Up @@ -63,10 +63,24 @@ impl Config {
}

fn from_entry_config(entry_config: &EntryConfig) -> Result<Self> {
if entry_config.path.is_some() && entry_config.paths.is_some() {
return Err(anyhow::anyhow!(
"Cannot have both `path` and `paths` in config"
));
}
Ok(Config {
path: match &entry_config.path {
Some(path) => normalize_path(path)?,
None => env::current_dir()?.canonicalize()?,
paths: if let Some(paths) = &entry_config.paths {
paths
.iter()
.map(|p| normalize_path(p))
.collect::<Result<Vec<PathBuf>, _>>()?
} else if let Some(path) = &entry_config.path {
println!(
"WARNING: the `path` configuration option is deprecated. Use `paths` instead."
);
uncenter marked this conversation as resolved.
Show resolved Hide resolved
vec![normalize_path(path)?]
} else {
vec![env::current_dir()?.canonicalize()?]
},
display_mode: match &entry_config.display_mode {
Some(display_mode) => *display_mode,
Expand Down Expand Up @@ -98,8 +112,11 @@ fn normalize_path(path: &Path) -> Result<PathBuf> {
/// this struct privately.
#[derive(Deserialize, Default)]
struct EntryConfig {
/// Reflection of the `path` field on [`Config`].
/// Formerly a reflection of the `path` field on [`Config`]. Use `paths` instead.
/// This field is deprecated and will be removed in a future release.
pub path: Option<PathBuf>,
/// Reflection of the `paths` field on [`Config`].
pub paths: Option<Vec<PathBuf>>,
uncenter marked this conversation as resolved.
Show resolved Hide resolved
/// Reflection of the `display_mode` field on [`Config`].
pub display_mode: Option<DisplayMode>,
/// Reflection of the `color_mode` field on [`Config`].
Expand Down
24 changes: 16 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use gfold::RepositoryCollector;
use clap::Parser;
use env_logger::Builder;
use log::{debug, LevelFilter};
use std::env;
use std::{env, path::PathBuf};

mod config;
mod display;
Expand All @@ -33,7 +33,7 @@ Troubleshooting: investigate unexpected behavior by prepending execution with \"
#[command(version, about = HELP, long_about = None)]
struct Cli {
/// specify path to target directory (defaults to current working directory)
pub path: Option<String>,
pub paths: Option<Vec<PathBuf>>,

#[arg(short, long)]
pub color_mode: Option<ColorMode>,
Expand Down Expand Up @@ -73,8 +73,12 @@ fn main() -> anyhow::Result<()> {
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()?;
if let Some(found_paths) = &cli.paths {
let current_dir = env::current_dir()?;
config.paths = found_paths
.iter()
.map(|p| current_dir.join(p).canonicalize())
.collect::<Result<Vec<PathBuf>, _>>()?;
}
debug!("finalized config options");

Expand All @@ -86,10 +90,14 @@ fn main() -> anyhow::Result<()> {
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)?;
for path in &config.paths {
debug!("processing path: {:?}", path);

let repository_collection =
RepositoryCollector::run(path, include_email, include_submodules)?;
let display_harness = DisplayHarness::new(config.display_mode, config.color_mode);
display_harness.run(&repository_collection)?;
}
}
Ok(())
}
Loading