Class: Pantry::CLI

Inherits:
Client show all
Defined in:
lib/pantry/cli.rb

Overview

Pantry’s Command Line Interface.

Instance Attribute Summary

Attributes inherited from Client

#last_received_message

Instance Method Summary collapse

Methods inherited from Client

#receive_file, #send_file, #send_message, #send_request, #shutdown

Constructor Details

#initialize(command_line, **args) ⇒ CLI



6
7
8
9
10
11
# File 'lib/pantry/cli.rb', line 6

def initialize(command_line, **args)
  @command_line = Pantry::CommandLine.new(command_line)

  args[:identity] ||= ENV["USER"]
  super(**args)
end

Instance Method Details

#perform(options, arguments) ⇒ Object

Given the parsed options and the arguments left over, figure out what Command was requested, build up the appropriate structures and start the communication process.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/pantry/cli.rb', line 65

def perform(options, arguments)
  command_info, command_options = @command_line.triggered_command

  if command_info
    client_filter = Pantry::Communication::ClientFilter.new(
      application: options['application'],
      environment: options['environment'],
      roles:       options['roles']
    )

    command = command_info[:class].new(*arguments)
    command_options = command_options.merge(options)

    request(client_filter, command, command_options)
  else
    $stderr.puts "I don't know the #{arguments.first} command"
  end
end

#prepare_local_pantry_rootObject



25
26
27
28
29
30
31
# File 'lib/pantry/cli.rb', line 25

def prepare_local_pantry_root
  if Pantry.config.root_dir.nil?
    # TODO Find a .pantry up the chain vs building one
    Pantry.config.root_dir = File.join(Dir.pwd, ".pantry")
    FileUtils.mkdir_p(Pantry.root)
  end
end

#process_global_options(options) ⇒ Object



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

def process_global_options(options)
  if options["verbose"]
    Pantry.config.log_level = :info
    Pantry.config.refresh
  end

  if options["debug"]
    Pantry.config.log_level = :debug
    Pantry.config.refresh
  end

  if server_host = options["host"]
    Pantry.config.server_host = server_host
  end

  if curve_key_file = options["curve-key-file"]
    Pantry.config.security = "curve"
    copy_keys_file_into_pantry_root(curve_key_file)
  end

  if options["version"]
    Pantry.ui.say Pantry::VERSION
    terminate
    return false
  end

  true
end

#receive_message(message) ⇒ Object

All messages received by this client are assumed to be responses from previous commands.



114
115
116
117
118
# File 'lib/pantry/cli.rb', line 114

def receive_message(message)
  if @command
    @command.receive_response(message)
  end
end

#request(filter, command, options) ⇒ Object

Fire off the requested Command.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pantry/cli.rb', line 85

def request(filter, command, options)
  @command = command
  @command.client = self

  # We don't use send_request here because we don't want to deal with the
  # wait-list future system. This lets command objects handle responses
  # as they come back to the CLI as the command sees fit.
  # If the command isn't meant directly for the Server, the Server will always
  # respond first with the list of clients who will be executing the command
  # and responding with the results. See Pantry::Commands::Echo for an example of how
  # to work with this flow.
  begin
    message = @command.prepare_message(options)
    message.to = filter.stream
    message.requires_response!

    send_message(message)

    @command.wait_for_finish

    copy_full_keys_back_to_curve_key_file
  rescue Exception => ex
    Pantry.ui.say("Error: #{ex.message}")
    Pantry.logger.debug(ex.backtrace.join("\n"))
  end
end

#runObject



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

def run
  prepare_local_pantry_root

  options, arguments = @command_line.parse!
  if options && process_global_options(options)
    super
    perform(options, arguments)
  end

  terminate
end