class Wpxf::Cli::Console

Main class for running the WPXF console.

Public Class Methods

new() click to toggle source
Calls superclass method Wpxf::Cli::Workspace::new
# File lib/wpxf/cli/console.rb, line 34
def initialize
  super
  setup_auto_complete
end

Public Instance Methods

can_handle?(command) click to toggle source
# File lib/wpxf/cli/console.rb, line 70
def can_handle?(command)
  return true if respond_to?(command) && permitted_commands.include?(command)
  puts
  print_bad "\"#{command}\" is not a recognised command."
  false
end
check_cache() click to toggle source
# File lib/wpxf/cli/console.rb, line 123
def check_cache
  return if cache_valid?
  rebuild_cache
  puts
end
clear() click to toggle source
# File lib/wpxf/cli/console.rb, line 48
def clear
  Gem.win_platform? ? (system 'cls') : (system 'clear')
end
commands_without_output() click to toggle source
# File lib/wpxf/cli/console.rb, line 39
def commands_without_output
  %w[back]
end
correct_number_of_args?(command, args) click to toggle source
# File lib/wpxf/cli/console.rb, line 77
def correct_number_of_args?(command, args)
  return true if command.match?(/workspace|loot|creds/)

  # Make an exception for set, unset and search, due to them taking
  # a variable number of strings that can contain white space.
  return true if command =~ /^(un)?set|search$/ && args.size > 1

  expected_args = Console.instance_method(command).parameters.size
  return true if expected_args == args.size

  print_bad "#{args.size} arguments specified for \"#{command}\", expected #{expected_args}."
  false
end
execute_user_command(command, args) click to toggle source
# File lib/wpxf/cli/console.rb, line 114
def execute_user_command(command, args)
  command = normalise_alised_commands(command)
  if can_handle? command
    puts unless commands_without_output.include? command
    send(command, *args) if correct_number_of_args?(command, args)
  end
  puts unless commands_without_output.include? command
end
normalise_alised_commands(command) click to toggle source
# File lib/wpxf/cli/console.rb, line 102
def normalise_alised_commands(command)
  if command == 'exit'
    'quit'
  elsif command == 'setg'
    'gset'
  elsif command == 'unsetg'
    'gunset'
  else
    command
  end
end
on_event_emitted(event) click to toggle source
# File lib/wpxf/cli/console.rb, line 91
def on_event_emitted(event)
  return unless !event[:verbose] || context.verbose?

  if event[:type] == :table
    indent_cursor(2) { print_table event[:rows], true }
  else
    handlers = { error: 'print_bad', success: 'print_good', info: 'print_info', warning: 'print_warning' }
    send(handlers[event[:type]], event[:msg])
  end
end
permitted_commands() click to toggle source
# File lib/wpxf/cli/console.rb, line 43
def permitted_commands
  %w[use back set show quit run unset check info gset setg gunset loot
     unsetg search clear reload help workspace rebuild_cache creds]
end
prompt_for_input() click to toggle source
# File lib/wpxf/cli/console.rb, line 52
def prompt_for_input
  prompt = 'wpxf'.underline.light_blue

  # The ANSI characters cause problems with the Readline lib and
  # Windows, so if it's a Windows platform, use only plain chars.
  prompt = 'wpxf' if Gem.win_platform?
  prompt += " [#{context.module_path}]" if module_loaded?

  begin
    input = Readline.readline("#{prompt} > ", true).to_s
  rescue SignalException
    input = ''
  end

  puts if input.empty?
  input
end
start() click to toggle source
# File lib/wpxf/cli/console.rb, line 129
def start
  loop do
    begin
      input = prompt_for_input
      break if input =~ /exit|quit/

      command, *args = input.split(/\s/)
      execute_user_command(command, args) if command
    rescue StandardError => e
      print_bad "Uncaught error: #{e}"
      print_bad e.backtrace.join("\n\t")
      puts
    end
  end
end