Class: SqlFinder::Application
- Inherits:
-
Object
- Object
- SqlFinder::Application
- Defined in:
- lib/sqlfinder/application.rb
Overview
Parses and validates command line options and runs the application.
Instance Method Summary collapse
-
#initialize ⇒ Application
constructor
A new instance of Application.
-
#prepare_options ⇒ Object
Parses command line options.
-
#run ⇒ Object
Executes the program.
-
#validate_file_to_search ⇒ Object
Ensures that the file exists.
-
#validate_keywords ⇒ Object
Ensures that we have no duplicate SQL keywords.
-
#validate_options ⇒ Object
Manages option validation.
-
#validate_required_options ⇒ Object
Ensures that required options are in place.
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
9 10 11 12 |
# File 'lib/sqlfinder/application.rb', line 9 def initialize end |
Instance Method Details
#prepare_options ⇒ Object
Parses command line options
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/sqlfinder/application.rb', line 15 def @options = {} OptionParser.new do |opts| opts. = "usage: findsql [options] file_to_search" opts.on("-h", "--help", "Display help screen.") do puts opts exit end opts.on("-k", "--keywords KEYWORDS", Array, "Using a comma-separated list, indicate whether you want to search for SELECT, INSERT, UPDATE, and/or DELETE statements (case-insensitive). By default, sqlfinder will search for all of them.") do |keywords| @options[:keywords] = keywords end opts.on("-t", "--table_name TABLE_NAME", "Indicate whether you want to restrict your search to a single table (case-insensitive).") do |t| @options[:table_name] = t end opts.on("-o", "--output_file OUTPUT_FILE", "If you want to write the results to a file rather than to standard out, provide the output file name.") do |o| @options[:output_file] = o end end.parse! @file_to_search = ARGV[0] end |
#run ⇒ Object
Executes the program
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/sqlfinder/application.rb', line 87 def run input_file_handle = File.new(@file_to_search) output_file_handle = STDOUT is_std_out = true; if @options[:output_file] output_file_handle = File.new((@options[:output_file]), "w") is_std_out = false; end keywords = @options[:keywords] table_name = @options[:table_name] SqlFinder::Find.run do @input_file_handle = input_file_handle @output_file_handle = output_file_handle @keywords = keywords @table_name = table_name do_find end input_file_handle.close() output_file_handle.close() unless is_std_out end |
#validate_file_to_search ⇒ Object
Ensures that the file exists
77 78 79 80 81 82 83 84 |
# File 'lib/sqlfinder/application.rb', line 77 def validate_file_to_search if !@file_to_search raise "Please provide the name of the file to search as a command-line argument." end if !(File.file?(@file_to_search)) raise "The file to search does not exist: #{@file_to_search}." end end |
#validate_keywords ⇒ Object
Ensures that we have no duplicate SQL keywords
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sqlfinder/application.rb', line 56 def validate_keywords if @options[:keywords] duplicate_checker = [] if @options[:keywords].size > 4 raise "The maximum number of keywords is 4. Valid keywords include(ignoring case): SELECT, INSERT, UPDATE, and DELETE." end @options[:keywords].each do |keyword| if !(keyword.match (/(?:select)|(?:insert)|(?:update)|(?:delete)/i)) raise "All keywords must match one of the following (ignoring case): SELECT, INSERT, UPDATE, and DELETE. Your keyword \"#{keyword}\" did not match." end duplicate_checker.each do |keyword2| if (keyword.downcase == keyword2.downcase) raise "You used the keyword \"#{keyword}\" more than once." end end duplicate_checker << keyword end end end |
#validate_options ⇒ Object
Manages option validation
37 38 39 40 41 |
# File 'lib/sqlfinder/application.rb', line 37 def validate_keywords validate_file_to_search end |
#validate_required_options ⇒ Object
Ensures that required options are in place
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/sqlfinder/application.rb', line 44 def missing_arg = false if ( @options[:table_name] && (@options[:table_name].start_with?("-")) ) raise "If you use the -t option, you must provide a table name." elsif ( @options[:output_file] && (@options[:output_file].start_with?("-")) ) raise "If you use the -o option, you must provide an output file name." elsif (@options[:keywords] && @options[:keywords][0] && (@options[:keywords][0].start_with?("-"))) raise "If you use the -k option, you must provide at least one of the following SQL keywords (ignoring case): SELECT, INSERT, UPDATE, and DELETE." end end |