Module: Roast::CLI::Commands

Included in:
Roast::CLI
Defined in:
lib/roast/cli/commands.rb

Constant Summary collapse

ALIASES =
{
  'a'   => 'add',
  'e'   => 'enable',
  'eg'  => 'enable-group',
  'd'   => 'disable',
  'dg'  => 'disable-group',
  'l'   => 'list'
}

Instance Method Summary collapse

Instance Method Details

#add(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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?') ⇒ Object



29
30
31
32
# 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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# 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) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# 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

#dispatchObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# 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) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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