Skip to content

Commit

Permalink
Merge branch 'use-clap-for-argument-parsing'
Browse files Browse the repository at this point in the history
  • Loading branch information
faern committed Aug 30, 2024
2 parents 35053be + 56b95f9 commit 53ca585
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 19 deletions.
88 changes: 86 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ anyhow = "1.0.86"
glob = "0.3.1"
phf = { version = "0.11.2", features = ["macros"] }
tree-sitter-rust = "0.21.2"
clap = { version = "4.5.16", features = ["derive"] }
log = "0.4.22"
env_logger = "0.11.5"

[dev-dependencies]
trycmd = "0.15.5"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
unicop [FILES]...
```

Where `[FILES]...` is a list of files or directory to check, default: `.`.
Where `[FILES]...` is a list of files or directory to check.

## Example

Expand Down
70 changes: 54 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io;
use std::path::Path;
use std::path::PathBuf;

use anyhow::Context;
use clap::Parser;
use config::CodeType;
use config::Config;
use config::Language;
Expand Down Expand Up @@ -105,24 +106,33 @@ impl RuleDispatcher {
}
}

#[derive(Debug, clap::Parser)]
#[command(arg_required_else_help = true)]
struct Args {
/// One or more files or directories to scan. Directories are scanned recursively.
paths: Vec<PathBuf>,
}

fn main() -> anyhow::Result<()> {
let mut args: Vec<String> = env::args().skip(1).collect();
if args.is_empty() {
args = vec![String::from(".")]
}
env_logger::init();

let args = Args::parse();

let default_config = get_default_config();
let user_config = get_user_config()?;
let dispatcher = RuleDispatcher {
user_config,
let mut dispatcher = RuleDispatcher {
user_config: None,
default_config,
};

for arg in args {
for entry in walkdir::WalkDir::new(arg) {
for path in args.paths {
for entry in walkdir::WalkDir::new(path) {
match entry {
Err(err) => eprintln!("{:}", err),
Ok(entry) if entry.file_type().is_file() => check_file(&dispatcher, entry.path()),
Ok(entry) if entry.file_type().is_file() => {
let entry_path = entry.path();
dispatcher.user_config = get_user_config(entry_path)?;
check_file(&dispatcher, entry_path);
}
Ok(_) => {}
}
}
Expand Down Expand Up @@ -178,11 +188,39 @@ fn check_file(dispatcher: &RuleDispatcher, path: &Path) {
}
}

fn get_user_config() -> anyhow::Result<Option<Config>> {
match std::fs::read_to_string("./unicop.toml") {
Ok(config_str) => toml::from_str(&config_str).context("Failed to parse config"),
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
Err(e) => Err(e).context("Failed to read config file"),
fn get_user_config(path: &Path) -> anyhow::Result<Option<Config>> {
let absolute_path = path
.canonicalize()
.with_context(|| format!("Failed to resolve absolute path for {}", path.display()))?;
let mut config_dir = if absolute_path.is_file() {
// If scanning a file, then check for the config file in the same directory.
absolute_path.parent().unwrap()
} else {
// And if scanning a dir, look for the config file in the dir.
&absolute_path
};

loop {
let config_path = config_dir.join("unicop.toml");

match std::fs::read_to_string(&config_path) {
Ok(config_str) => {
log::debug!(
"Using config {} for scan path {}",
config_path.display(),
absolute_path.display()
);
break toml::from_str(&config_str).context("Failed to parse config");
}
Err(e) if e.kind() == io::ErrorKind::NotFound => match config_dir.parent() {
Some(parent_dir) => config_dir = parent_dir,
None => {
log::debug!("No user config for scan path {}", absolute_path.display());
break Ok(None);
}
},
Err(e) => break Err(e).context("Failed to read config file"),
}
}
}

Expand Down

0 comments on commit 53ca585

Please sign in to comment.