Module: ElasticDot::Command

Defined in:
lib/elasticdot/command.rb

Defined Under Namespace

Classes: Addons, Alias, Apps, Auth, Base, Config, Db, Domains, Help, Keys, Logs, Ps, Services

Class Method Summary collapse

Class Method Details

.run(args, opts) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/elasticdot/command.rb', line 8

def self.run(args, opts)
  cmd  = args.shift

  unless cmd
    ElasticDot::Command::Help.root_help
    exit 0
  end

  klass, act = cmd.split ':', 2
  klass = klass.downcase

  if klass == 'help'
    m = args.shift || 'root_help'
    m = m.split(':')[0]

    ElasticDot::Command::Help.send m
    exit 0
  end

  unless (klass == 'login' or cmd == 'auth:login')
    ElasticDot::Command::Auth.authenticate!
  end

  ElasticDot::Command::Base.api = ElasticDot::API.new

  if ElasticDot::Command::Alias.respond_to? klass
    ElasticDot::Command::Alias.send klass.downcase, args, opts
    exit 0
  end

  act ||= 'list'

  begin
    klass = "ElasticDot::Command::#{klass.capitalize}".constantize
  rescue
    unless ElasticDot::Command::Base.respond_to? cmd
      puts 'Invalid command: ' + cmd
      exit 1
    end

    return ElasticDot::Command::Base.send cmd
  end

  unless klass.respond_to? act
    puts 'Invalid action ' + act
    exit 1
  end

  method_args = klass.method(act).arity

  case method_args
  when 0
    klass.send act
  when 1
    klass.send act, opts
  when 2
    klass.send act, args, opts
  end
end