From b8b5e322ff1cace9fd4b30c6d6a5990b8f411eab Mon Sep 17 00:00:00 2001 From: Ryan Fleck Date: Wed, 6 May 2020 15:04:58 -0400 Subject: [PATCH 1/6] Update cli.rb so it works on single .rule files. --- cli.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/cli.rb b/cli.rb index fd3b170..24035e8 100644 --- a/cli.rb +++ b/cli.rb @@ -22,7 +22,16 @@ # License along with this program. If not, see # . require 'multi_json' -require_relative 'lib/xa/rules/parse' +require_relative 'lib/xa/rules/parse/content' -include XA::Rules::Parse -IO.write(ARGV[1], MultiJson.dump(parse(IO.read(ARGV[0])), pretty: true)) +include XA::Rules::Parse::Content + +IO.write( + ARGV[1], + MultiJson.dump( + parse_rule( + IO.read( + ARGV[0] + ) + ), + pretty: true)) From ad3c4e639a7b2b7da0255d29805994e95d4f9cd5 Mon Sep 17 00:00:00 2001 From: Ryan Fleck Date: Wed, 6 May 2020 15:14:48 -0400 Subject: [PATCH 2/6] Infer second argument if only one is passed --- cli.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/cli.rb b/cli.rb index 24035e8..b569fce 100644 --- a/cli.rb +++ b/cli.rb @@ -21,17 +21,13 @@ # You should have received a copy of the GNU Affero General Public # License along with this program. If not, see # . + require 'multi_json' require_relative 'lib/xa/rules/parse/content' include XA::Rules::Parse::Content -IO.write( - ARGV[1], - MultiJson.dump( - parse_rule( - IO.read( - ARGV[0] - ) - ), - pretty: true)) +input_file = ARGV[0] +output_file = (ARGV[1] != nil) ? ARGV[1] : "#{input_file}.json" + +IO.write(output_file, MultiJson.dump(parse_rule(IO.read(input_file)), pretty: true)) From 74516b9e7596552cb6d2760a3a7954e554eedc08 Mon Sep 17 00:00:00 2001 From: Ryan Fleck Date: Wed, 6 May 2020 15:28:02 -0400 Subject: [PATCH 3/6] Differentiate between rule and table file parsing. --- cli.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cli.rb b/cli.rb index b569fce..f3300a5 100644 --- a/cli.rb +++ b/cli.rb @@ -30,4 +30,10 @@ input_file = ARGV[0] output_file = (ARGV[1] != nil) ? ARGV[1] : "#{input_file}.json" -IO.write(output_file, MultiJson.dump(parse_rule(IO.read(input_file)), pretty: true)) +if input_file.end_with?(".rule") + IO.write(output_file, MultiJson.dump(parse_rule(IO.read(input_file)), pretty: true)) +elsif input_file.end_with?(".table") + IO.write(output_file, MultiJson.dump(parse_table(IO.read(input_file)), pretty: true)) +else + raise 'File type not *.rule or *.table' +end From b5cfd4dec38e3531cf928199adfcac00c59aa21d Mon Sep 17 00:00:00 2001 From: Ryan Fleck Date: Wed, 6 May 2020 15:35:48 -0400 Subject: [PATCH 4/6] Add exception for users who provide no input. --- cli.rb | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cli.rb b/cli.rb index f3300a5..7ed4944 100644 --- a/cli.rb +++ b/cli.rb @@ -27,13 +27,21 @@ include XA::Rules::Parse::Content -input_file = ARGV[0] -output_file = (ARGV[1] != nil) ? ARGV[1] : "#{input_file}.json" +def run + input_file = ARGV[0] + output_file = (ARGV[1] != nil) ? ARGV[1] : "#{input_file}.json" + + if input_file.end_with?(".rule") + IO.write(output_file, MultiJson.dump(parse_rule(IO.read(input_file)), pretty: true)) + elsif input_file.end_with?(".table") + IO.write(output_file, MultiJson.dump(parse_table(IO.read(input_file)), pretty: true)) + else + raise 'File type not *.rule or *.table' + end +end -if input_file.end_with?(".rule") - IO.write(output_file, MultiJson.dump(parse_rule(IO.read(input_file)), pretty: true)) -elsif input_file.end_with?(".table") - IO.write(output_file, MultiJson.dump(parse_table(IO.read(input_file)), pretty: true)) +if ARGV[0] == nil + raise 'Please provide a .rule or .table file to parse' else - raise 'File type not *.rule or *.table' -end + run +end \ No newline at end of file From e81aebbb64691a92a38a656d8cbcfa19a6cb8b10 Mon Sep 17 00:00:00 2001 From: Ryan Fleck Date: Wed, 6 May 2020 15:39:20 -0400 Subject: [PATCH 5/6] Bump RVM version in .travis.yml --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index c094d17..66a8c3b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: required language: ruby rvm: -- 2.4.2 +- 2.7.0 script: - "bundle exec rspec" notifications: From fcd4114a2f0e839830a3b04d2cd7fcfe2b1e50ed Mon Sep 17 00:00:00 2001 From: Ryan Fleck Date: Wed, 6 May 2020 20:38:33 -0400 Subject: [PATCH 6/6] Modify cli.rb to process a folder of .rule/.table --- .gitignore | 3 +++ cli.rb | 33 +++++++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index d5a0040..f7076d6 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,9 @@ capybara-*.html rerun.txt pickle-email-*.html +/test-runs/ +/.idea/ + # TODO Comment out these rules if you are OK with secrets being uploaded to the repo config/initializers/secret_token.rb config/secrets.yml diff --git a/cli.rb b/cli.rb index 7ed4944..2e1bb2e 100644 --- a/cli.rb +++ b/cli.rb @@ -27,10 +27,14 @@ include XA::Rules::Parse::Content -def run - input_file = ARGV[0] - output_file = (ARGV[1] != nil) ? ARGV[1] : "#{input_file}.json" +# Determines if file is a rule or table, and runs relevant library method. +def parse_file(input_file, output_file) + puts "Parsing #{input_file}" + + if output_file == nil + output_file = "#{input_file}.json" + end if input_file.end_with?(".rule") IO.write(output_file, MultiJson.dump(parse_rule(IO.read(input_file)), pretty: true)) elsif input_file.end_with?(".table") @@ -40,8 +44,21 @@ def run end end -if ARGV[0] == nil - raise 'Please provide a .rule or .table file to parse' -else - run -end \ No newline at end of file +# Executes on program start. For each file in a folder, or a single file, runs parse_file +def cli_run + if ARGV[0] == nil + raise 'Please provide a .rule or .table file to parse' + elsif File.directory?(ARGV[0]) + Dir.foreach(ARGV[0]) do |name| + file = File.join(ARGV[0], name) + if file.end_with?(".table") || file.end_with?(".rule") + parse_file(file, nil) + end + end + else + parse_file(ARGV[0], ARGV[1]) + end +end + +# Execute immediately +cli_run