Class: Organo::CLI::Commands::Remote::Upload

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/organo/commands/remote/remote_upload.rb

Instance Method Summary collapse

Instance Method Details

#call(env:) ⇒ Object



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
# File 'lib/organo/commands/remote/remote_upload.rb', line 14

def call(env:, **)
  if File.exist?('./.organo')
    config = File.exist?(Config::DEFAULT_FILE) ? JSON.parse(File.read(Config::DEFAULT_FILE)) : nil
    section = config.find { |option| option['section'] == 'remotes' }
    if section.nil?
      puts 'No remote is configured in the configuration file'
    else
      credentials = section['entries'].find { |entry| entry['env'] == env }
      if credentials.nil?
        puts 'Could not find the specified environment'
      else
        local_file = Config::DATABASE_FILE
        Net::FTP.open(credentials['url']) do |ftp|
          ftp.(credentials['username'], credentials['password'])
          ftp.chdir(credentials['directory'])
          puts "Uploading file: #{local_file}"
          ftp.putbinaryfile(local_file, credentials['database_filename'])
        end
      end
    end
  else
    puts 'This directory is not initialized.'
    puts 'Run command `organo init` to create the configuration directory.'
  end
rescue Net::FTPPermError
  puts 'Login to FTP server failed.'
end