class Wordlist::CLI
Represents the ‘wordlist` command’s logic.
@api private
@since 1.0.0
Constants
- BUG_REPORT_URL
The URL to report bugs to.
- FORMATS
Mapping of ‘–format` option values and `format:` Symbols.
- PROGRAM_NAME
The program name.
Attributes
Additional options for {Builder#initialize}.
@return [Hash{Symbol => Object}]
The command to run with each word from the wordlist.
@return [String, nil]
The explicit wordlist format to use.
@return [:txt, :gzip, :bzip2, :xz, nil]
Command mode (building or reading).
@return [:build, :read]
Wordlist
modifiers to apply.
@return [Array<(Symbol, …)>]
Wordlist
operators to apply.
@return [Array<(Symbol, …)>]
The command’s option parser.
@return [OptionParser]
The path to the output wordlist file.
@return [String, nil]
Public Class Methods
Initializes the command.
@param [:read, :build] mode
@param [:txt, :gzip, :bzip2, :xz, :zip, :7zip, nil] format
@param [String, nil] command
# File lib/wordlist/cli.rb, line 83 def initialize(mode: :read, format: nil, command: nil) @option_parser = option_parser @mode = mode @format = format @command = command @output = nil @operators = [] @modifiers = [] @builder_options = {} end
Initializes and runs the command.
@param [Array<String>] argv
Command-line arguments.
@return [Integer]
The exit status of the command.
# File lib/wordlist/cli.rb, line 152 def self.run(argv=ARGV) new().run(argv) rescue Interrupt # https://tldp.org/LDP/abs/html/exitcodes.html return 130 rescue Errno::EPIPE # STDOUT pipe broken return 0 end
Public Instance Methods
Adds a modifier to be applied to the wordlist(s) later.
@param [Symbol] name
The modifier method name.
@param [Array<Object>] args
Additional arguments for the modifier.
# File lib/wordlist/cli.rb, line 119 def add_modifier(name,*args) @modifiers << [name, args] end
Adds an operator to be applied to the wordlist(s) later.
@param [Symbol] name
The operator method name.
@param [Array<Object>] args
Additional arguments for the operator.
# File lib/wordlist/cli.rb, line 106 def add_operator(name,*args) @operators << [name, args] end
Wordlist
building mode.
@param [Array<String>] argv
Additional command-line arguments.
# File lib/wordlist/cli.rb, line 194 def build_mode(argv) builder = begin if @format Builder.open(@output, format: @format, **@builder_options) else Builder.open(@output, **@builder_options) end rescue UnknownFormat, CommandNotFound => error print_error(error.message) return -1 end begin if argv.empty? $stdin.each_line do |line| builder.parse(line) end else argv.each do |file| builder.parse_file(file) end end ensure builder.close end return 0 end
Opens a wordlist file.
@param [String] path
The path to the wordlist file.
@return [Wordlist::File]
The opened wordlist.
# File lib/wordlist/cli.rb, line 132 def open_wordlist(path) if @format Wordlist::File.open(path, format: @format) else Wordlist::File.open(path) end rescue WordlistNotFound, UnknownFormat => error print_error(error.message) exit -1 end
Prints a backtrace to stderr.
@param [Exception] exception
The exception.
# File lib/wordlist/cli.rb, line 451 def print_backtrace(exception) $stderr.puts "Oops! Looks like you've found a bug!" $stderr.puts "Please report the following text to: #{BUG_REPORT_URL}" $stderr.puts $stderr.puts "```" $stderr.puts "#{exception.full_message}" $stderr.puts "```" end
Prints an error message to stderr.
@param [String] error
The error message.
# File lib/wordlist/cli.rb, line 441 def print_error(error) $stderr.puts "#{PROGRAM_NAME}: #{error}" end
Wordlist
reading mode.
@param [Array<String>] argv
Additional command-line arguments.
# File lib/wordlist/cli.rb, line 229 def read_mode(argv) unless argv.length >= 1 print_error "too few arguments given, requires at least one WORDLIST argument" print_error "usage: #{PROGRAM_NAME} [options] WORDLIST ..." return -1 end # open the first wodlist wordlist = open_wordlist(argv.first) # append the additional wordlists argv[1..].each { |arg| wordlist += (open_wordlist(arg)) } # apply operators first @operators.each do |(operator,args)| wordlist = wordlist.send(operator,*args) end # then apply modifiers @modifiers.each do |(method,args)| wordlist = wordlist.send(method,*args) end begin if @command wordlist.each do |word| system(@command.gsub('{}',word)) end else wordlist.each do |word| puts word end end rescue CommandNotFound => error print_error(error.message) return -1 end return 0 end
Runs the command.
@param [Array<String>] argv
Command-line arguments.
@return [Integer]
The return status code.
# File lib/wordlist/cli.rb, line 171 def run(argv=ARGV) argv = begin @option_parser.parse(argv) rescue OptionParser::ParseError => error print_error(error.message) return -1 end case @mode when :build then build_mode(argv) else read_mode(argv) end rescue => error print_backtrace(error) return -1 end