Class: Railroader::Commandline
- Inherits:
-
Object
- Object
- Railroader::Commandline
- Defined in:
- lib/railroader/commandline.rb
Overview
Implements handling of running Railroader from the command line.
Class Method Summary collapse
-
.check_latest ⇒ Object
Check for the latest version.
-
.compare_results(options) ⇒ Object
Runs a comparison report based on the options provided.
-
.early_exit_options(options) ⇒ Object
Handle options that exit without generating a report.
-
.parse_options(argv) ⇒ Object
Parse ARGV-style array of options.
-
.quit(exit_code = 0, message = nil) ⇒ Object
Exits with the given exit code and prints out the message, if given.
-
.regular_report(options) ⇒ Object
Runs a regular report based on the options provided.
-
.run(options, default_app_path = ".") ⇒ Object
Runs everything:.
-
.run_railroader(options) ⇒ Object
Actually run Railroader.
-
.run_report(options) ⇒ Object
Run either a comparison or regular report based on options provided.
-
.set_interrupt_handler(options) ⇒ Object
Sets interrupt handler to gracefully handle Ctrl+C.
-
.set_options(options, default_app_path = ".") ⇒ Object
Modifies options, including setting the app_path if none is given in the options hash.
-
.start(options = nil, app_path = ".") ⇒ Object
Main method to run Railroader from the command line.
Class Method Details
.check_latest ⇒ Object
Check for the latest version.
If the latest version is newer, quit with a message.
41 42 43 44 45 |
# File 'lib/railroader/commandline.rb', line 41 def check_latest if error = Railroader.ensure_latest quit Railroader::Not_Latest_Version_Exit_Code, error end end |
.compare_results(options) ⇒ Object
Runs a comparison report based on the options provided.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/railroader/commandline.rb', line 48 def compare_results require 'json' vulns = Railroader.compare .merge(:quiet => [:quiet]) if [:comparison_output_file] File.open [:comparison_output_file], "w" do |f| f.puts JSON.pretty_generate(vulns) end Railroader.notify "Comparison saved in '#{options[:comparison_output_file]}'" else puts JSON.pretty_generate(vulns) end if [:exit_on_warn] && vulns[:new].count > 0 quit Railroader::Warnings_Found_Exit_Code end end |
.early_exit_options(options) ⇒ Object
Handle options that exit without generating a report.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/railroader/commandline.rb', line 68 def if [:list_checks] or [:list_optional_checks] Railroader.list_checks quit elsif [:create_config] Railroader.dump_config quit elsif [:show_help] puts Railroader::Options.create_option_parser({}) quit elsif [:show_version] require 'railroader/version' puts "railroader #{Railroader::Version}" quit end end |
.parse_options(argv) ⇒ Object
Parse ARGV-style array of options.
Exits if options are invalid.
Returns an option hash and the app_path.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/railroader/commandline.rb', line 90 def argv begin , _ = Railroader::Options.parse! argv rescue OptionParser::ParseError => e $stderr.puts e. $stderr.puts "Please see `railroader --help` for valid options" quit(-1) end if argv[-1] app_path = argv[-1] else app_path = "." end return , app_path end |
.quit(exit_code = 0, message = nil) ⇒ Object
Exits with the given exit code and prints out the message, if given.
Override this method for different behavior.
111 112 113 114 |
# File 'lib/railroader/commandline.rb', line 111 def quit exit_code = 0, = nil warn if exit exit_code end |
.regular_report(options) ⇒ Object
Runs a regular report based on the options provided.
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/railroader/commandline.rb', line 117 def regular_report tracker = run_railroader if tracker.[:exit_on_warn] and not tracker.filtered_warnings.empty? quit Railroader::Warnings_Found_Exit_Code end if tracker.[:exit_on_error] and tracker.errors.any? quit Railroader::Errors_Found_Exit_Code end end |
.run(options, default_app_path = ".") ⇒ Object
Runs everything:
-
set_interrupt_handler -
early_exit_options -
set_options -
check_latest -
run_report
30 31 32 33 34 35 36 |
# File 'lib/railroader/commandline.rb', line 30 def run , default_app_path = "." set_interrupt_handler , default_app_path check_latest if [:ensure_latest] run_report end |
.run_railroader(options) ⇒ Object
Actually run Railroader.
Returns a Tracker object.
132 133 134 |
# File 'lib/railroader/commandline.rb', line 132 def run_railroader Railroader.run .merge(:print_report => true, :quiet => [:quiet]) end |
.run_report(options) ⇒ Object
Run either a comparison or regular report based on options provided.
137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/railroader/commandline.rb', line 137 def run_report begin if [:previous_results_json] compare_results else regular_report end rescue Railroader::NoApplication => e quit Railroader::No_App_Found_Exit_Code, e. rescue Railroader::MissingChecksError => e quit Railroader::Missing_Checks_Exit_Code, e. end end |
.set_interrupt_handler(options) ⇒ Object
Sets interrupt handler to gracefully handle Ctrl+C
152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/railroader/commandline.rb', line 152 def set_interrupt_handler trap("INT") do warn "\nInterrupted - exiting." if [:debug] warn caller end exit! end end |
.set_options(options, default_app_path = ".") ⇒ Object
Modifies options, including setting the app_path if none is given in the options hash.
166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/railroader/commandline.rb', line 166 def , default_app_path = "." unless [:app_path] [:app_path] = default_app_path end if [:quiet].nil? [:quiet] = :command_line end end |
.start(options = nil, app_path = ".") ⇒ Object
Main method to run Railroader from the command line.
If no options are provided, ARGV will be parsed and used instead. Otherwise, the options are expected to be a Hash like the one returned after ARGV is parsed.
14 15 16 17 18 19 20 21 |
# File 'lib/railroader/commandline.rb', line 14 def start = nil, app_path = "." unless , app_path = ARGV end run , app_path end |