Class: Wordlist::CLI Private
- Inherits:
-
Object
- Object
- Wordlist::CLI
- Defined in:
- lib/wordlist/cli.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Represents the wordlist
command's logic.
Constant Summary collapse
- PROGRAM_NAME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The program name.
"wordlist"
- BUG_REPORT_URL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
The URL to report bugs to.
"https://github.com/postmodern/wordlist.rb/issues/new"
- FORMATS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Mapping of
--format
option values andformat:
Symbols. { 'txt' => :txt, 'gzip' => :gzip, 'bzip2'=> :bzip2, 'xz' => :xz, 'zip' => :zip, '7zip' => :"7zip" }
Instance Attribute Summary collapse
-
#builder_options ⇒ Hash{Symbol => Object}
readonly
private
Additional options for Builder#initialize.
-
#command ⇒ String?
readonly
private
The command to run with each word from the wordlist.
-
#format ⇒ :txt, ...
readonly
private
The explicit wordlist format to use.
-
#mode ⇒ :build, :read
readonly
private
Command mode (building or reading).
-
#modifiers ⇒ Array<(Symbol, ...)>
readonly
private
Wordlist modifiers to apply.
-
#operators ⇒ Array<(Symbol, ...)>
readonly
private
Wordlist operators to apply.
-
#option_parser ⇒ OptionParser
readonly
private
The option parser.
-
#output ⇒ String?
readonly
private
The path to the output wordlist file.
Class Method Summary collapse
-
.run(argv = ARGV) ⇒ Integer
private
Initializes and runs the command.
Instance Method Summary collapse
-
#add_modifier(name, *args) ⇒ Object
private
Adds a modifier to be applied to the wordlist(s) later.
-
#add_operator(name, *args) ⇒ Object
private
Adds an operator to be applied to the wordlist(s) later.
-
#build_mode(argv) ⇒ Object
private
Wordlist building mode.
-
#initialize(mode: :read, format: nil, command: nil) ⇒ CLI
constructor
private
Initializes the command.
-
#open_wordlist(path) ⇒ Wordlist::File
private
Opens a wordlist file.
-
#print_backtrace(exception) ⇒ Object
private
Prints a backtrace to stderr.
-
#print_error(error) ⇒ Object
private
Prints an error message to stderr.
-
#read_mode(argv) ⇒ Object
private
Wordlist reading mode.
-
#run(argv = ARGV) ⇒ Integer
private
Runs the command.
Constructor Details
#initialize(mode: :read, format: nil, command: nil) ⇒ CLI
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes the command.
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# 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 |
Instance Attribute Details
#builder_options ⇒ Hash{Symbol => Object} (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Additional options for Builder#initialize.
72 73 74 |
# File 'lib/wordlist/cli.rb', line 72 def @builder_options end |
#command ⇒ String? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The command to run with each word from the wordlist.
57 58 59 |
# File 'lib/wordlist/cli.rb', line 57 def command @command end |
#format ⇒ :txt, ... (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The explicit wordlist format to use.
47 48 49 |
# File 'lib/wordlist/cli.rb', line 47 def format @format end |
#mode ⇒ :build, :read (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Command mode (building or reading).
42 43 44 |
# File 'lib/wordlist/cli.rb', line 42 def mode @mode end |
#modifiers ⇒ Array<(Symbol, ...)> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wordlist modifiers to apply.
67 68 69 |
# File 'lib/wordlist/cli.rb', line 67 def modifiers @modifiers end |
#operators ⇒ Array<(Symbol, ...)> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wordlist operators to apply.
62 63 64 |
# File 'lib/wordlist/cli.rb', line 62 def operators @operators end |
#option_parser ⇒ OptionParser (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The option parser.
37 38 39 |
# File 'lib/wordlist/cli.rb', line 37 def option_parser @option_parser end |
#output ⇒ String? (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The path to the output wordlist file.
52 53 54 |
# File 'lib/wordlist/cli.rb', line 52 def output @output end |
Class Method Details
.run(argv = ARGV) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes and runs the command.
152 153 154 155 156 157 158 159 160 |
# 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 |
Instance Method Details
#add_modifier(name, *args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds a modifier to be applied to the wordlist(s) later.
119 120 121 |
# File 'lib/wordlist/cli.rb', line 119 def add_modifier(name,*args) @modifiers << [name, args] end |
#add_operator(name, *args) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds an operator to be applied to the wordlist(s) later.
106 107 108 |
# File 'lib/wordlist/cli.rb', line 106 def add_operator(name,*args) @operators << [name, args] end |
#build_mode(argv) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wordlist building mode.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# 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.) 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 |
#open_wordlist(path) ⇒ Wordlist::File
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Opens a wordlist file.
132 133 134 135 136 137 138 139 140 141 |
# 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.) exit -1 end |
#print_backtrace(exception) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints a backtrace to stderr.
451 452 453 454 455 456 457 458 |
# 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.}" $stderr.puts "```" end |
#print_error(error) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prints an error message to stderr.
441 442 443 |
# File 'lib/wordlist/cli.rb', line 441 def print_error(error) $stderr.puts "#{PROGRAM_NAME}: #{error}" end |
#read_mode(argv) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Wordlist reading mode.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# 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.) return -1 end return 0 end |
#run(argv = ARGV) ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Runs the command.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/wordlist/cli.rb', line 171 def run(argv=ARGV) argv = begin @option_parser.parse(argv) rescue OptionParser::ParseError => error print_error(error.) return -1 end case @mode when :build then build_mode(argv) else read_mode(argv) end rescue => error print_backtrace(error) return -1 end |