Skip to content

Commit

Permalink
feat: use CLI args to get the config path
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Zaunseder committed Mar 6, 2024
1 parent d8dfbf1 commit 6775f2f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 17 deletions.
17 changes: 6 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,25 @@ use parser::parse_commit;
#[command(version, about, long_about = None)]
struct Cli {
/// Path to the config file
#[arg(short, long, default_value = "commitlint.config")]
config_path: PathBuf,
#[arg(short, long, default_value = "commitguard.config")]
config_name: String,

/// Current working directory
#[arg(long, default_value = current_dir().unwrap_or_else(|_e| PathBuf::from("/")).into_os_string())]
cwd: PathBuf,
}

fn main() -> ExitCode {
// let commit_message =
// "feat(nice): add cool feature\n\nsome body\n\nsecond body line\n\nsome footer";

// let commit = parse_commit(&commit_message);
// println!("{:#?}", commit);
let args = Cli::parse();

// read commit from stdin
let mut buffer = String::new();
stdin().read_to_string(&mut buffer).unwrap_or(0);
let commit = parse_commit(&buffer);

let lint_result = rules::run(&commit);
let config_path = args.cwd.join(args.config_name);
let lint_result = rules::run(&commit, config_path);

let report_handler = GraphicalReportHandler::new();

if lint_result.has_warnings() {
Expand Down Expand Up @@ -66,9 +64,6 @@ fn main() -> ExitCode {
lint_result.errors_len()
);

let args = Cli::parse();
println!("{:?}", args);

if lint_result.has_errors() {
return ExitCode::FAILURE;
}
Expand Down
9 changes: 3 additions & 6 deletions src/rules/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path;
use std::path::PathBuf;

use config::Config;
use serde::Deserialize;
Expand Down Expand Up @@ -139,10 +139,7 @@ impl LintResult {
}
}

pub fn run(commit: &Commit) -> LintResult {
let pwd = std::env::current_dir().unwrap_or_else(|_e| path::PathBuf::from("/"));
println!("Current directory: {}", pwd.display());

pub fn run(commit: &Commit, config_path: PathBuf) -> LintResult {
let settings = Config::builder()
.add_source(config::File::from_str(
r#"
Expand All @@ -156,7 +153,7 @@ pub fn run(commit: &Commit) -> LintResult {
config::FileFormat::Toml,
))
// Source can be `commitlint.config.toml` or `commitlint.config.json``
.add_source(config::File::from(pwd.join("commitguard.config")).required(false))
.add_source(config::File::from(config_path).required(false))
.build()
.unwrap();

Expand Down

0 comments on commit 6775f2f

Please sign in to comment.