Class: Organo::CLI::Commands::Remote::AddEntry

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

Instance Method Summary collapse

Instance Method Details

#call(env:, url:, username:, password:, directory:) ⇒ Object



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_add.rb', line 18

def call(env:, url:, username:, password:, directory:, **)
  if File.exist?('./.organo')
    config = File.exist?(Config::DEFAULT_FILE) ? JSON.parse(File.read(Config::DEFAULT_FILE)) : []
    section = config.find { |option| option['section'] == 'remotes' }
    remote = {
      env: env, url: url, username: username, password: password,
      directory: directory, database_filename: Config::DATABASE_FILE
    }
    if section.nil?
      section = { section: 'remotes', entries: [] }
      config.push(section)
      section[:entries].push(remote)
    elsif section['entries'].find { |entry| entry['env'] == env }.nil?
      section['entries'].push(remote)
    else
      puts 'Environment name is already used'
    end
    File.write(Config::DEFAULT_FILE, JSON.pretty_generate(config))
  else
    puts 'This directory is not initialized.'
    puts 'Run command `organo init` to create the configuration directory.'
  end
end