Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds rules command #99

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ Usage:
funzzy [options]
funzzy init
funzzy watch [<command>] [options]
funzzy rules [--match=<path>] [--config=<cfgfile>]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe tasks is better?

funzzy <command> [options]

Commands:
init Create a new '.watch.yaml' file.
watch Watch for file changes and execute a command.
rules List all rules available in the config file.

Options:
<command> Run an arbitrary command for current folder.
-m --match=<path> Check which rules match the given path.
-c --config=<cfgfile> Use given config file.
-t --target=<task> Execute only the given task target.
-n --non-block Execute tasks and cancel them if a new event is received.
Expand All @@ -57,12 +60,14 @@ pub struct Args {
// comand
pub cmd_init: bool,
pub cmd_watch: bool,
pub cmd_rules: bool,

pub arg_command: String,

// options
pub flag_config: String,
pub flag_target: String,
pub flag_match: String,

pub flag_n: bool,
pub flag_h: bool,
Expand All @@ -75,6 +80,12 @@ fn main() {
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());

let config_file = if args.flag_config.is_empty() {
cli::watch::DEFAULT_FILENAME
} else {
&args.flag_config
};

match args {
// Metainfo
Args { flag_v: true, .. } => show(get_version().as_str()),
Expand All @@ -83,6 +94,46 @@ fn main() {
// Commands
Args { cmd_init: true, .. } => execute(InitCommand::new(cli::watch::DEFAULT_FILENAME)),

Args { cmd_rules: true, .. } if !args.flag_match.is_empty() => {
let rules = if args.flag_config.is_empty() {
let default_filename = cli::watch::DEFAULT_FILENAME;
match rules::from_file(default_filename) {
Ok(rules) => rules,
Err(err) => match rules::from_file(&default_filename.replace(".yaml", ".yml")) {
Ok(rules) => rules,
Err(_) => error("Failed to read default config file", err),
},
}
} else {
match rules::from_file(&args.flag_config) {
Ok(rules) => rules,
Err(err) => error("Failed to read config file", err),
}
};

rules::print_matches(rules, &args.flag_match);
}

Args { cmd_rules: true, .. } => {
let rules = if args.flag_config.is_empty() {
let default_filename = cli::watch::DEFAULT_FILENAME;
match rules::from_file(default_filename) {
Ok(rules) => rules,
Err(err) => match rules::from_file(&default_filename.replace(".yaml", ".yml")) {
Ok(rules) => rules,
Err(_) => error("Failed to read default config file", err),
},
}
} else {
match rules::from_file(&args.flag_config) {
Ok(rules) => rules,
Err(err) => error("Failed to read config file", err),
}
};

rules::print_rules(rules);
}

Args { arg_command, .. } if !arg_command.is_empty() => {
match from_stdin() {
Ok(content) => {
Expand All @@ -106,6 +157,7 @@ fn main() {
}

_ => {

let rules = if args.flag_config.is_empty() {
let default_filename = cli::watch::DEFAULT_FILENAME;
match rules::from_file(default_filename) {
Expand Down
61 changes: 61 additions & 0 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,67 @@ pub fn from_file(filename: &str) -> Result<Vec<Rules>, String> {
}
}

pub fn print_rules(rules: Vec<Rules>) {
let template = &r"

Available Rules
{{#available}}
";

let available = rules
.iter()
.cloned()
.map(|r| format!(" -{}", r.name))
.collect::<Vec<String>>()
.join("\n");

let output = template.replace("{{#available}}", &available);

stdout::info(output.as_str());

}

pub fn print_matches(rules: Vec<Rules>, path: &str) {
let template = &r"

Matches - count: {{#count}}

{{#matches}}

Available Rules

{{#available}}
";


let available = rules
.iter()
.cloned()
.map(|r| format!(" -{}", r.name))
.collect::<Vec<String>>()
.join("\n");

let with_available = template.replace("{{#available}}", &available);

let matches = rules
.iter()
.cloned()
.filter(|r| !r.ignore(path) && r.watch(path))
.collect::<Vec<Rules>>();

let with_count = with_available.replace("{{#count}}", &matches.len().to_string());

let output = with_count.replace("{{#matches}}",
&matches
.iter()
.filter(|r| !r.ignore(path) && r.watch(path))
.map(|r| format!(" -{}", r.name))
.collect::<Vec<String>>()
.join("\n"));

stdout::info(output.as_str());
}

fn pattern(pattern: &str) -> Pattern {
Pattern::new(&format!("**/{}", pattern)).expect("Pattern error.")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function assert_file_occurrencies() {
for i in {1..5}
do
occurrencies=$(grep -o "$2" "$1" | wc -l)
echo "occurrencies: $occurrencies"
echo "occurrencies of '$2': $occurrencies"
if [ $occurrencies -eq $3 ]; then
success=1
break
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/specs/list-all-available-rules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
source "$HELPERS"
test "list all avaiable rules"

echo "
- name: 1st task
run: 'echo before'
change: [
\"$WORKDIR/**\",
\"$WORKDIR/src/**/*.txt\"
]

- name: 2nd task
run: 'echo before'
change: \"$WORKDIR/src/**/*.log\"
ignore: \"$WORKDIR/src/test.log\"

- name: 3rd task
run: 'echo before'
change: [
\"$WORKDIR/tmp/**/*.log\"
]
" > $WORKDIR/.check.yaml

$TEST_DIR/funzzy rules -c $WORKDIR/.check.yaml >> $WORKDIR/output.log

wait_for_file "$WORKDIR/output.log"

cat "$WORKDIR"/output.log

assert_file_occurrencies "$WORKDIR/output.log" "1st task" 1
assert_file_occurrencies "$WORKDIR/output.log" "2nd task" 1
assert_file_occurrencies "$WORKDIR/output.log" "3rd task" 1

cleanup
55 changes: 55 additions & 0 deletions tests/integration/specs/list-rules-match-path.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
source "$HELPERS"
test "given check path match at one rules"

echo "
- name: 1st task
run: 'echo before'
change: [
\"$WORKDIR/**\",
\"$WORKDIR/src/**/*.txt\"
]

- name: 2nd task
run: 'echo before'
change: \"$WORKDIR/src/**/*.log\"
ignore: \"$WORKDIR/src/test.log\"

- name: 3rd task
run: 'echo before'
change: [
\"$WORKDIR/tmp/**/*.log\"
]
" > $WORKDIR/.check.yaml

$TEST_DIR/funzzy rules -m "$WORKDIR/src/foo.txt" -c $WORKDIR/.check.yaml >> $WORKDIR/output.log

wait_for_file "$WORKDIR/output.log"

cat "$WORKDIR"/output.log

assert_file_occurrencies "$WORKDIR/output.log" "1st task" 2
assert_file_occurrencies "$WORKDIR/output.log" "2nd task" 1
assert_file_occurrencies "$WORKDIR/output.log" "3rd task" 1

echo "" > "$WORKDIR"/output.log

$TEST_DIR/funzzy rules -m "$WORKDIR/src/foo.log" -c $WORKDIR/.check.yaml >> $WORKDIR/output.log

cat "$WORKDIR"/output.log

assert_file_occurrencies "$WORKDIR/output.log" "1st task" 2
assert_file_occurrencies "$WORKDIR/output.log" "2nd task" 2
assert_file_occurrencies "$WORKDIR/output.log" "3rd task" 1

echo "" > "$WORKDIR"/output.log

$TEST_DIR/funzzy rules -m "$WORKDIR/src/test.log" -c $WORKDIR/.check.yaml >> $WORKDIR/output.log

cat "$WORKDIR"/output.log

assert_file_occurrencies "$WORKDIR/output.log" "1st task" 2
assert_file_occurrencies "$WORKDIR/output.log" "2nd task" 1
assert_file_occurrencies "$WORKDIR/output.log" "3rd task" 1

cleanup