Skip to content

Commit

Permalink
Add file ignore flag
Browse files Browse the repository at this point in the history
Co-authored-by: Lukas Rysavy <[email protected]>
Co-authored-by: Nathan Kessler <[email protected]>
  • Loading branch information
3 people authored Oct 6, 2020
1 parent 77fbce8 commit abb359f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ TEST_RULES=test/rules
TEST_DOTFILES=test/dotfiles
TEST_DEST_DIR=test/dest
TEST_EXPECTED_DIR=test/expected
TEST_IGNORE_ARG=--ignore ignored_file

DIFF_FLAGS=-qNr

Expand All @@ -26,7 +27,7 @@ $(EXECFILE): release
test: $(EXECFILE)
rm -rf $(TEST_DEST_DIR)
mkdir $(TEST_DEST_DIR)
./$(EXECFILE) $(TEST_RULES) $(TEST_DOTFILES) $(TEST_DEST_DIR); \
./$(EXECFILE) $(TEST_RULES) $(TEST_DOTFILES) $(TEST_DEST_DIR) $(TEST_IGNORE_ARG); \
diff $(DIFF_FLAGS) $(TEST_DEST_DIR) $(TEST_EXPECTED_DIR)
if [ ! -x "$(TEST_DEST_DIR)/binary_file" ]; then \
@echo "Expected binary_file to be executable."; \
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ dot-templater --diff CONFIG SRC_DIR DEST_DIR

Compares files from `SRC_DIR` modified according to rules in `CONFIG` against the contents of `DEST_DIR`.

### Parameters
* `-i <file> [...files]` or `--ignore <file> [...files]`
Excludes a file or directory (and all children) from templating, ie. they will not be copied to the destination directory.
For example, use `-i .git` for git controlled dotfile repositories.

### Config Format
Any line beginning with `#` is ignored. Config file can contain key/value substitutions and feature flags.

Expand Down
13 changes: 12 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub struct Arguments<'a> {
pub source: &'a str,
pub dest: &'a str,
pub diff: Mode,
pub ignore: Vec<&'a str>,
}

impl<'a> Arguments<'a> {
Expand All @@ -159,12 +160,17 @@ impl<'a> Arguments<'a> {

source = Self::trim_trailing_slash(&source);
dest = Self::trim_trailing_slash(&dest);
let ignore = match args.values_of("ignore") {
Some(value) => value.collect(),
None => vec![],
};

Self {
rules,
source,
dest,
diff,
ignore,
}
}

Expand Down Expand Up @@ -236,8 +242,13 @@ pub fn template(
source_dir: &str,
dest_dir: &str,
mode: Mode,
ignore: Vec<&str>,
) -> Result<(), Box<dyn Error>> {
for entry in WalkDir::new(source_dir) {
for entry in WalkDir::new(source_dir).into_iter().filter_entry(|entry| {
let ignore_list: Vec<&Path> = ignore.iter().map(|fname| Path::new(fname)).collect();

!ignore_list.contains(&entry.path().strip_prefix(&source_dir).unwrap())
}) {
let source_file = entry?;
let source_file = source_file.path();
let dest_file = source_file.to_str().unwrap().replace(source_dir, dest_dir);
Expand Down
18 changes: 14 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ fn main() {
.long("diff")
.help("Diff mode"),
)
.arg(
Arg::with_name("ignore")
.short("i")
.long("ignore")
.help("Ignore file")
.multiple(true)
.min_values(0)
.takes_value(true),
)
.after_help(concat!(
"Copy files from SRC_DIR to DEST_DIR using rules defined in CONFIG.\n\n",
"Rules configuration:\n",
Expand All @@ -65,8 +74,9 @@ fn main() {
process::exit(1);
});

dot_templater::template(&config, &args.source, &args.dest, args.diff).unwrap_or_else(|err| {
eprintln!("Error while performing templating: {}", err);
process::exit(1);
});
dot_templater::template(&config, &args.source, &args.dest, args.diff, args.ignore)
.unwrap_or_else(|err| {
eprintln!("Error while performing templating: {}", err);
process::exit(1);
});
}
1 change: 1 addition & 0 deletions test/dotfiles/ignored_file
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I am ignored

0 comments on commit abb359f

Please sign in to comment.