cli/lib/peas/config.rb

Summary

Maintainability
A
45 mins
Test Coverage
module Peas
  # The port for Peas' Switchboard socket server
  SWITCHBOARD_PORT = ENV['SWITCHBOARD_PORT'] || 9345

  def self.config_file
    "#{ENV['HOME']}/.peas"
  end

  # Read JSON config from file
  def self.config
    file = File.open config_file, "a+"
    contents = file.read
    contents = '{}' if contents == ''
    JSON.parse contents
  end

  # Merge new key/values into the config file
  def self.update_config(hash)
    content = Peas.config.merge(hash).to_json
    File.open(Peas.config_file, 'w+') { |f| f.write(content) }
  end

  # Hierarchy of sources for the Peas API domain
  def self.api_domain
    git_domain = Git.sh 'git config peas.domain'
    domain =
    if ENV['PEAS_API_ENDPOINT']
      ENV['PEAS_API_ENDPOINT']
    elsif !git_domain.nil? && git_domain != ''
      git_domain
    elsif Peas.config['domain']
      Peas.config['domain']
    else
      'vcap.me:4443'
    end
    unless domain[/\Ahttp:\/\//] || domain[/\Ahttps:\/\//]
      "https://#{domain}"
    else
      domain
    end
  end

  def self.host
    URI.parse(Peas.api_domain).host
  end

  def self.error_message(string)
    puts string.color(:red)
  end

  def self.warning_message(string)
    puts string.color(:magenta)
  end
end