module Roast::CLI::Commands

Constants

ALIASES

Public Instance Methods

add(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 34
def add(*args)
  if args.length < 2
    raise ArgumentError, "You must provide an ip address and a hostname to point it to: `roast add 127.0.0.1 something.dev'"
  elsif args.length == 3
    group = args.shift
  else
    group = nil
  end

  source, hostname = args

  if @hosts_file.add(group, source, hostname)
    @hosts_file.write
    puts "added host entry for `#{source}  \033[4m#{hostname}\033[0m'"
  end
end
confirm(message = 'Are you sure that you want to do that?') click to toggle source
# File lib/roast/cli/commands.rb, line 29
def confirm(message = 'Are you sure that you want to do that?')
  puts "#{message} [\"yes\" or \"no\"]"
  exit if $stdin.gets.chomp !~ /\Ay(?:es)?\Z/i
end
delete(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 73
def delete(*args)
  entry   = args.first
  confirm("Are you sure that you want to delete entries matching `#{entry}'?")
  results = @hosts_file.delete(entry)
  if results.empty?
    puts "no entries found matching `#{entry}'"
  else
    @hosts_file.write
    puts "deleted entry#{results.length > 1 ? 's' : ''} matching `#{entry}'"
  end
end
delete_group(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 107
def delete_group(*args)
  group = args.first
  confirm("Are you sure that you want to delete the group `#{group}'?")
  if @hosts_file.delete_group(group)
    @hosts_file.write
    puts "deleted group `#{group}'"
  else
    puts "Unable to delete the group `#{group}', it doesn't exist yet."
  end
end
disable(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 62
def disable(*args)
  entry   = args.first
  results = @hosts_file.disable(entry)
  if results.empty?
    puts "no entries found matching `#{entry}'"
  else
    @hosts_file.write
    puts "disabled entry#{results.length > 1 ? 's' : ''} matching `#{entry}'"
  end
end
disable_group(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 96
def disable_group(*args)
  group = args.first

  if @hosts_file.disable_group(group)
    @hosts_file.write
    puts "disabled group `#{group}'"
  else
    puts "Unable to disable the group `#{group}', it doesn't exist yet."
  end
end
dispatch() click to toggle source
# File lib/roast/cli/commands.rb, line 13
def dispatch
  command    = ARGV.shift
  command    = (ALIASES[command] || command).tr('-', '_')

  if respond_to? command
    @hosts_file = HostsFile.new.read
    send(command, *ARGV)
  else
    puts "`#{command}' is an unknown command, use --help to see available commands"
    exit
  end
rescue ArgumentError => e
  puts e.message
  exit 1
end
enable(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 51
def enable(*args)
  entry   = args.first
  results = @hosts_file.enable(entry)
  if results.empty?
    puts "no entries found matching `#{entry}'"
  else
    @hosts_file.write
    puts "enabled entry#{results.length > 1 ? 's' : ''} matching `#{entry}'"
  end
end
enable_group(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 85
def enable_group(*args)
  group = args.first

  if @hosts_file.enable_group(group)
    @hosts_file.write
    puts "enabled group `#{group}'"
  else
    puts "Unable to enable the group `#{group}', it doesn't exist yet."
  end
end
list(*args) click to toggle source
# File lib/roast/cli/commands.rb, line 118
def list(*args)
  # TODO: a bit awkward, use a class var?
  path, groups = @hosts_file.list

  if groups.empty?
    puts "there are no roast entries in `#{path}'\n"
  else
    entries = ''
    indent  = groups.map { |g| g.hosts.map { |h| h.hostname.size }.max }.max

    groups.each { |group| entries << group.to_cli(indent) }
    puts entries.chomp
  end
end