class Bow::Application

Constants

COLUMN_WIDTH
EMPTY_COLUMN

Attributes

argv[RW]
debug[RW]
options[RW]

Public Class Methods

new(argv) click to toggle source
# File lib/bow/application.rb, line 11
def initialize(argv)
  @options = {
    user: 'root',
    group: 'all',
    inventory: 'hosts.json'
  }
  @argv = argv.dup
  @ssh_helpers = {}
  @debug = false
end

Public Instance Methods

config() click to toggle source
# File lib/bow/application.rb, line 40
def config
  Config
end
debug?() click to toggle source
# File lib/bow/application.rb, line 53
def debug?
  !!@debug
end
inventory() click to toggle source

rubocop:enable Lint/ShadowingOuterLocalVariable

# File lib/bow/application.rb, line 34
def inventory
  return @inventory if @inventory
  @inventory ||= Inventory.new
  @inventory.ensure!
end
run() click to toggle source

rubocop:disable Lint/ShadowingOuterLocalVariable

# File lib/bow/application.rb, line 23
def run
  opts = OptionParser.new do |opts|
    opts.banner = build_banner
    options_parser.parse(opts)
  end
  opts.parse!(argv)
  command = parse_arguments(opts)
  command.run
end
ssh_helper(host) click to toggle source
# File lib/bow/application.rb, line 44
def ssh_helper(host)
  conn = host.conn
  @ssh_helpers[conn] ||= SshHelper.new(conn, self)
end
targets(user) click to toggle source
# File lib/bow/application.rb, line 49
def targets(user)
  @targets ||= Targets.new(inventory.targetfile, user)
end

Private Instance Methods

build_banner() click to toggle source
# File lib/bow/application.rb, line 59
def build_banner
  banner = BANNER.dup.to_s
  banner << "\n\nCOMMANDS\n\n"
  Command.all.each do |command|
    banner << format_command(command)
  end
  banner << "OPTIONS\n\n"
  banner
end
build_command(name, argv = {}) click to toggle source
# File lib/bow/application.rb, line 77
def build_command(name, argv = {})
  raise "Unknown command #{name}!" unless command_exists? name
  Command.find(name).new(self, argv)
end
command_exists?(name) click to toggle source
# File lib/bow/application.rb, line 82
def command_exists?(name)
  Command.names.include? name.to_s
end
format_command(command) click to toggle source
# File lib/bow/application.rb, line 86
def format_command(command)
  tail = ' ' * (32 - command.command_name.length)
  str = "     #{command.command_name}#{tail}#{command.description}\n"
  str << "     #{EMPTY_COLUMN}Usage: #{command.usage}\n\n"
end
options_parser() click to toggle source
# File lib/bow/application.rb, line 92
def options_parser
  @options_parser ||= Options.new(@options)
end
parse_arguments(opts) click to toggle source
# File lib/bow/application.rb, line 69
def parse_arguments(opts)
  if argv.empty?
    argv << '-h'
    opts.parse!(argv)
  end
  build_command(argv.shift, argv)
end