Class: Pantry::Commands::EditApplication

Inherits:
Pantry::Command show all
Defined in:
lib/pantry/commands/edit_application.rb

Overview

Edit the configuration of the requested Application.

Application configuration is stored on the Server under Pantry.root/applications/[app name]/config.yml and is where all configuration lives for how Pantry manages this application.

Instance Method Summary collapse

Methods inherited from Pantry::Command

command, #finished, #finished?, #initialize, message_type, #receive_client_response, #receive_response, #send_request, #send_request!, #server_or_client, #server_or_client=, #to_message, #wait_for_finish

Constructor Details

This class inherits a constructor from Pantry::Command

Instance Method Details

#perform(message) ⇒ Object

Read or create a new config file for the given application and return the contents of this config file, which will always be a YAML document



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pantry/commands/edit_application.rb', line 31

def perform(message)
  application = message.body[0]

  config_file = Pantry.root.join("applications", application, "config.yml")
  FileUtils.mkdir_p(File.dirname(config_file))

  if File.exists?(config_file)
    [File.read(config_file)]
  else
    [{"name" => application}.to_yaml]
  end
end

#prepare_message(options) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pantry/commands/edit_application.rb', line 16

def prepare_message(options)
  @application = options[:application]
  raise Pantry::MissingOption, 'Missing required option "application"' unless @application

  # Let the EDITOR check run before we do any communication
  @editor = Pantry::FileEditor.new

  super.tap do |msg|
    msg << @application
  end
end

#receive_server_response(message) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pantry/commands/edit_application.rb', line 44

def receive_server_response(message)
  current_config = message.body[0]
  new_config     = @editor.edit(current_config, :yaml)

  if new_config != current_config
    send_request!(
      Pantry::Commands::UpdateApplication.new(
        @application, new_config
      ).to_message
    )
  end
end