Class: Joumae::CLI

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/joumae/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ CLI

Returns a new instance of CLI.



10
11
12
# File 'lib/joumae/cli.rb', line 10

def initialize(argv)
  @argv = argv
end

Class Method Details

.run!Object



68
69
70
# File 'lib/joumae/cli.rb', line 68

def self.run!
  new(ARGV).run!
end

Instance Method Details

#parse!Object



14
15
16
17
18
19
20
21
22
# File 'lib/joumae/cli.rb', line 14

def parse!
  argv = @argv.dup

  opt = OptionParser.new
  opt.on('--resource-name VALUE') { |v| @resource_name = v }
  opt.parse!(argv)
  @sub, *@args= argv
  debug [@sub, @args]
end


59
60
61
62
63
64
65
66
# File 'lib/joumae/cli.rb', line 59

def print_as_json(&block)
  begin
    puts block.call.to_json
  rescue => e
    $stderr.puts "#{e.message} Aborting."
    exit 1
  end
end

#run!Object



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
# File 'lib/joumae/cli.rb', line 24

def run!
  parse!

  client = Joumae::Client.create

  case @sub
  when "run"
    cmd = @args.join(" ")
    command = Joumae::Command.new(cmd, resource_name: @resource_name, client: client)
    begin
      command.run!
    rescue Joumae::CommandFailedError => e
      $stderr.puts "#{e.message} Aborting."
      exit e.status
    rescue Joumae::Client::ResourceAlreadyLockedError => e
      $stderr.puts "#{e.message} Aborting."
      exit 1
    end
  when "create"
    print_as_json do
      client.create(@resource_name)
    end
  when "acquire-lock"
    print_as_json do
      client.acquire(@resource_name)
    end
  when "release-lock"
    print_as_json do
      client.release(@resource_name)
    end
  else
    fail "The sub-command #{@sub} does not exist."
  end
end