Module: Dreamy::Command

Defined in:
lib/dreamy/command.rb,
lib/dreamy/commands/ps.rb,
lib/dreamy/commands/dns.rb,
lib/dreamy/commands/base.rb,
lib/dreamy/commands/help.rb,
lib/dreamy/commands/mysql.rb,
lib/dreamy/commands/users.rb,
lib/dreamy/commands/domains.rb,
lib/dreamy/commands/announce.rb

Defined Under Namespace

Classes: Announce, Base, Dns, Domains, Help, InvalidCommand, Mysql, Ps, Users

Class Method Summary collapse

Class Method Details

.error(msg) ⇒ Object



26
27
28
29
# File 'lib/dreamy/command.rb', line 26

def error(msg)
  STDERR.puts(msg)
  exit 1
end

.namespacesObject



48
49
50
51
52
# File 'lib/dreamy/command.rb', line 48

def namespaces
  @@namespaces ||= Dir["#{File.dirname(__FILE__)}/commands/*"].map do |namespace|
    namespace.gsub(/.*\//, '').gsub(/\.rb/, '')
  end
end

.parse(command) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dreamy/command.rb', line 31

def parse(command)
  parts = command.split(':')
  case parts.size
  when 1
    if namespaces.include? command
      return command, 'index'
    else
      raise InvalidCommand
    end
  when 2
    raise InvalidCommand unless namespaces.include? parts[0]
    return parts
  else
    raise InvalidCommand
  end
end

.run(command, args) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/dreamy/command.rb', line 8

def run(command, args)
  run_internal(command, args)
rescue InvalidCommand
  error "Unknown command. Run 'dh help' for usage information."
rescue ApiError => e
  error "DreamHost returned this error: #{e}"
rescue Interrupt => e
  error "\n[canceled]"
end

.run_internal(command, args) ⇒ Object

Raises:



18
19
20
21
22
23
24
# File 'lib/dreamy/command.rb', line 18

def run_internal(command, args)
  namespace, command = parse(command)
  require "commands/#{namespace}"
  klass = Dreamy::Command.const_get(namespace.capitalize).new(args)
  raise InvalidCommand unless klass.respond_to?(command)
  klass.send(command)
end