class BBC::Cosmos::Tools::Config::App

Public Class Methods

new(args = [], local_options = {}, config = {}) click to toggle source
Calls superclass method
# File lib/bbc/cosmos/tools/config/app.rb, line 15
def initialize(args = [], local_options = {}, config = {})
  super(args, local_options, config)
end

Public Instance Methods

generate(project, component_id) click to toggle source
# File lib/bbc/cosmos/tools/config/app.rb, line 24
def generate(project, component_id)
  begin
    config_base(options[:config])
    config = config_from(project, options[:env])

    say "\nComponent: #{component_id}\nProject: #{project}\nEnvironement: #{options[:env]}\n", :green
    if options[:cosmos_format]
      say JSON.pretty_generate(config.generate_cosmos component_id)
    else
      say JSON.pretty_generate(config.generate component_id)
    end
  rescue Exception => e
    error e.message
  end

end
list(project) click to toggle source
# File lib/bbc/cosmos/tools/config/app.rb, line 44
def list(project)
  begin
    config_base(options[:config])
    config = config_from(project, options[:env])

    say "\nComponents: \n", :green
    config.components.each do |component_id|
      say "#{component_id}", :blue
    end
  rescue Exception => e
    error e.message
  end

end
push(project, key_path = nil, component_id = nil) click to toggle source
# File lib/bbc/cosmos/tools/config/app.rb, line 62
def push(project, key_path = nil, component_id = nil)
  begin
    config_base(options[:config])
    config = config_from(project, options[:env])
    components = component_id.nil? ? config.components : [component_id]

    reply = yes?("Are you sure you want to push changes for #{components.length} components(s) to #{options[:env]}?", :blue) unless options[:force]
    if reply || reply.nil?

      components.each do |id|
        cosmos_config = JSON.generate(config.generate_cosmos id)

        @api = BBC::Cosmos::Tools::Config::API.new(app_config['api'], key_path)
        response = @api.put sprintf(app_config['endpoint'], options[:env], id), cosmos_config

        if response.success?
          say "Configuration for component '#{id}' successfully saved", :green
        else
          say "There was an error saving your config: #{response.body}", :red
        end
      end

    else
      say "Action cancelled", :red
    end

  rescue Exception => e
    error e.message
  end
end

Private Instance Methods

app_config() click to toggle source
# File lib/bbc/cosmos/tools/config/app.rb, line 104
def app_config
  @app_config ||= BBC::Cosmos::Tools::Config::Configuration::get(@config_base)['bbc']['cosmos']
end
config_base(override = nil) click to toggle source
# File lib/bbc/cosmos/tools/config/app.rb, line 108
def config_base(override = nil)
  @config_base = !override.nil? && File.directory?(override) ? Pathname.new(override) : Pathname.pwd.join('configs')
  raise("Config directory doesn't exist") unless File.directory?(@config_base)
end
config_from(project, env) click to toggle source
# File lib/bbc/cosmos/tools/config/app.rb, line 100
def config_from(project, env)
  BBC::Cosmos::Tools::Config::ProjectConfig.new(@config_base, project, env)
end
error(message) click to toggle source
# File lib/bbc/cosmos/tools/config/app.rb, line 95
def error(message)
  say message, :red
  exit 1
end