Class: Organo::CLI::Commands::Remote::Download

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

Instance Method Summary collapse

Instance Method Details

#call(env:) ⇒ Object



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
41
42
43
# File 'lib/organo/commands/remote/remote_download.rb', line 15

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
        Net::FTP.open(credentials['url']) do |ftp|
          ftp.(credentials['username'], credentials['password'])
          ftp.chdir(credentials['directory'])
          if File.exist?(Config::DATABASE_FILE)
            File.delete("#{Config::DATABASE_FILE}.old") if File.exist?("#{Config::DATABASE_FILE}.old")
            File.rename(Config::DATABASE_FILE, "#{Config::DATABASE_FILE}.old")
          end
          ftp.getbinaryfile(credentials['database_filename'], Config::DATABASE_FILE)
        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