Skip to content

Commit

Permalink
Add compare-flag
Browse files Browse the repository at this point in the history
  • Loading branch information
mstruebing committed Sep 18, 2020
1 parent 7981df7 commit 89f7ff2
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/lib.rs
@@ -1,5 +1,6 @@
use crate::common::*;

use std::collections::HashSet;
use std::error::Error;
use std::path::PathBuf;

Expand All @@ -16,6 +17,11 @@ pub fn run(args: &clap::ArgMatches, current_dir: &PathBuf) -> Result<Vec<Warning
let mut skip_checks: Vec<&str> = Vec::new();
let mut excluded_paths: Vec<PathBuf> = Vec::new();

if args.is_present("compare") {
let files = args.values_of("compare");
println!("files: {:?}", files);
}

let is_recursive = args.is_present("recursive");

if let Some(skip) = args.values_of("skip") {
Expand Down Expand Up @@ -72,6 +78,71 @@ pub fn run(args: &clap::ArgMatches, current_dir: &PathBuf) -> Result<Vec<Warning
Ok(warnings)
}

#[derive(Debug)]
struct CompareType {
pub path: PathBuf,
pub keys: Vec<String>,
}

pub fn compare(
args: &clap::ArgMatches,
current_dir: &PathBuf,
) -> Result<Vec<String>, Box<dyn Error>> {
let mut warnings: Vec<String> = Vec::new();
let mut file_paths: Vec<PathBuf> = Vec::new();
let mut compared_files: Vec<CompareType> = Vec::new();
let mut all_keys: HashSet<String> = vec![].into_iter().collect();

if let Some(inputs) = args.values_of("compare") {
file_paths = inputs
.filter_map(|f| fs_utils::canonicalize(f).ok())
.collect();
}

// Create compared structure
for path in file_paths {
let mut keys: Vec<String> = Vec::new();
let relative_path = match fs_utils::get_relative_path(&path, &current_dir) {
Some(p) => p,
None => continue,
};

let (fe, strs) = match FileEntry::from(relative_path.clone()) {
Some(f) => f,
None => continue,
};

for line in get_line_entries(&fe, strs) {
if let Some(key) = line.get_key() {
all_keys.insert(key.clone());
keys.push(key)
}
}

let bla: CompareType = CompareType {
path: relative_path,
keys,
};

compared_files.push(bla);
}

for file in compared_files {
// copy all keys
let mut missing_keys = all_keys.clone();
missing_keys.retain(|key| !file.keys.contains(key));

if missing_keys.len() > 0 {
warnings.push(format!(
"file: {:?} is missing keys: {:?}",
file.path, missing_keys
))
}
}

Ok(warnings)
}

fn get_file_paths(
dir_entries: Vec<PathBuf>,
excludes: &[PathBuf],
Expand Down
21 changes: 21 additions & 0 deletions src/main.rs
@@ -1,12 +1,24 @@
use clap::Arg;
use std::error::Error;
use std::ffi::OsStr;
// use std::path::PathBuf;
use std::{env, process};

fn main() -> Result<(), Box<dyn Error>> {
let current_dir = env::current_dir()?;
let args = get_args(current_dir.as_os_str());

if args.is_present("compare") {
if let Ok(warnings) = dotenv_linter::compare(&args, &current_dir) {
for warning in warnings {
println!("{}", warning);
process::exit(1);
}
}

process::exit(0);
}

if args.is_present("show-checks") {
dotenv_linter::available_check_names()
.iter()
Expand Down Expand Up @@ -75,6 +87,15 @@ fn get_args(current_dir: &OsStr) -> clap::ArgMatches {
.required(true)
.multiple(true),
)
.arg(
Arg::with_name("compare")
.short("c")
.long("compare")
.value_name("FILE_NAME")
.help("Compare keys of files")
.multiple(true)
.takes_value(true),
)
.arg(
Arg::with_name("exclude")
.short("e")
Expand Down

0 comments on commit 89f7ff2

Please sign in to comment.