Skip to content

Commit

Permalink
Merge pull request #1 from VaheDanielyan/development
Browse files Browse the repository at this point in the history
Add exit codes and quiet flag
  • Loading branch information
VaheDanielyan authored Jan 6, 2024
2 parents 6c0492c + 209e4d9 commit f56bf97
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "diffdir"
version = "0.4.3"
version = "0.4.4"
edition = "2021"
authors = ["Vahe Danielyan <[email protected]>"]
license = "MIT"
Expand Down
35 changes: 16 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,21 @@ cargo install --path .
## Usage

```sh
NAME
diffdir - A cli tool to compare two directories
SYNOPSIS
diffdir [--ignore] [--ignore-file] [--no-colors] [-h|--help] [-V|--version] <DIR_A> <DIR_B>
DESCRIPTION
A cli tool to compare two directories
OPTIONS
--ignore=IGNORE_PATTERNS
--ignore-file=IGNORE_FILE
--no-colors
-h, --help
Print help
-V, --version
Print version
<DIR_A>
<DIR_B>
AUTHORS
Vahe Danielyan <[email protected]>

Usage: diffdir [OPTIONS] <dir a> <dir b>

Arguments:
<dir a>
<dir b>

Options:
--ignore <IGNORE_PATTERNS>...
--ignore-file <IGNORE_FILE>
--quiet Surpress output
--no-colors will not format into ansi string and / or include colors
-h, --help Print help
-V, --version Print version
```

## Output

In Addition to the standard text output the program will return **42** if there are any differences between the directories and **0** in case of them being identical. This can be handy when calling this tool from other programs.
9 changes: 7 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use clap::*;
#[derive(Parser)]
#[clap(author, version, about = "A cli tool to compare two directories", long_about)]
pub struct Args {
#[clap(value_parser)]
#[clap(value_parser, value_name = "dir a")]
pub dir_a: PathBuf,

#[clap(value_parser)]
#[clap(value_parser, value_name = "dir b")]
pub dir_b: PathBuf,

#[clap(long = "ignore", value_parser, use_value_delimiter(true), value_delimiter = ' ', num_args=1..)]
Expand All @@ -20,6 +20,11 @@ pub struct Args {
#[clap(long = "ignore-file", value_parser)]
pub ignore_file: Option<PathBuf>,

/// Surpress output
#[clap(long = "quiet", value_parser)]
pub quiet: bool,

/// will not format into ansi string and / or include colors
#[clap(long = "no-colors", value_parser)]
pub no_colors: bool,
}
Expand Down
8 changes: 7 additions & 1 deletion src/diffcmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,20 @@ impl CmpResult {
only_in_b: Vec::new(),
differs: Vec::new() }
}
pub fn are_different(&self) -> bool {
if self.only_in_a.is_empty() && self.only_in_b.is_empty() && self.differs.is_empty() {
return false;
}
true
}
pub fn format_text(&self, ansi: bool) -> Vec<String> {
let bold = Style::new().bold();
let bold_underline = bold.underline();
let mut result : Vec<String> = Vec::new();
let mut result_plain: Vec<String> = Vec::new();

println!();
if self.only_in_a.is_empty() && self.only_in_b.is_empty() && self.differs.is_empty() {
if !self.are_different() {
let message = format!("The directories appear to be the same\n");
let styled_message = bold.
paint(&message);
Expand Down
24 changes: 13 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
extern crate diffdir;

use core::panic;
use atty::Stream;

use diffdir::diffcmp::{DirCmp, CmpResult};
use diffdir::args::Args;

use core::panic;
use atty::Stream;
use clap::Parser;

fn main() {
Expand All @@ -32,12 +30,16 @@ fn main() {

let dir_comparator = DirCmp::new(&args.dir_a, &args.dir_b, &merged_patterns);
let result: CmpResult = dir_comparator.compare_directories();
let text = if atty::is(Stream::Stdout) {
result.format_text(!args.no_colors)
} else {
result.format_text(false)
};
for item in text {
print!("{}", item);
if !args.quiet {
let text = if atty::is(Stream::Stdout) {
result.format_text(!args.no_colors)
} else {
result.format_text(false)
};

for item in text {
print!("{}", item);
}
}
if result.are_different() { std::process::exit(42) }
}

0 comments on commit f56bf97

Please sign in to comment.