Class: Pantry::Commands::SyncDirectory

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

Overview

Base class for any Command that needs to sync a set of files in a directory from the Server down to the Client.

Subclasses need to define where on the server the files live and where on the client the files will be written to. Both #server_directory and #client_directory are executed on the Client so #client is accessible for added information.

This command expects simple directories with a small number of files that are themselves small in size, as this command reads every file into memory and sends that raw content back to the Client. If there are more substantial files to transfer use #send_file and #receive_file instead.

Instance Method Summary collapse

Methods inherited from Pantry::Command

command, #finished, #finished?, #initialize, message_type, #prepare_message, #receive_client_response, #receive_response, #receive_server_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

#client_directory(local_root) ⇒ Object



21
22
23
# File 'lib/pantry/commands/sync_directory.rb', line 21

def client_directory(local_root)
  raise "Specify the write directory on the client"
end

#perform(message) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pantry/commands/sync_directory.rb', line 25

def perform(message)
  dir_contents = send_request!(
    Pantry::Commands::DownloadDirectory.new(
      server_directory(Pathname.new(""))
    ).to_message
  )

  write_to = client_directory(Pantry.root)
  FileUtils.mkdir_p(write_to)

  dir_contents.body.each do |(file_name, file_contents)|
    file_path = write_to.join(file_name).cleanpath
    FileUtils.mkdir_p(File.dirname(file_path))

    File.open(file_path, "w+") do |file|
      file.write(file_contents)
    end
  end

  true
end

#server_directory(local_root) ⇒ Object



17
18
19
# File 'lib/pantry/commands/sync_directory.rb', line 17

def server_directory(local_root)
  raise "Specify the read directory on the server"
end