class BBC::Cosmos::Tools::Commands::Config

Attributes

cosmos_config[RW]

Public Class Methods

new(args = [], local_options = {}, thor_config = {}) click to toggle source
Calls superclass method BBC::Cosmos::Tools::Commands::Base::new
# File lib/bbc/cosmos/tools/commands/config.rb, line 14
def initialize(args = [], local_options = {}, thor_config = {})
  super(args, local_options, thor_config)
  @cosmos_config = BBC::Cosmos::Tools::CosmosConfiguration.new(config)
end

Public Instance Methods

generate(component) click to toggle source
# File lib/bbc/cosmos/tools/commands/config.rb, line 23
def generate(component)
  if options[:write]
    File.open("#{component}.json", "w") do |f|
      f.write(JSON.pretty_generate(cosmos_config.component(component)))
    end
  end

  if options[:output] == "machine"
    say JSON.pretty_generate(cosmos_config.cosmos_component(component)), :white

    exit 0
  end

  begin

    say "#{banner(component)}\n"

    if options[:cosmos_format]
      say JSON.pretty_generate(cosmos_config.cosmos_component(component)), :white
    else
      say JSON.pretty_generate(cosmos_config.component(component)), :white
    end
  rescue Exception => e
    error e.message
  end
end
list() click to toggle source
# File lib/bbc/cosmos/tools/commands/config.rb, line 52
def list
  if options[:output] == "machine"
    cosmos_config.component_keys.each do |component_id|
      puts "#{component_id}"
    end

    exit 0
  end

  begin

    say banner
    say "\n#{get_key_value('Components', '')}\n"

    cosmos_config.component_keys.each do |component_id|
      say "* #{component_id}", :white
    end
  rescue Exception => e
    error e.message
  end
end
push(component_id = nil) click to toggle source
# File lib/bbc/cosmos/tools/commands/config.rb, line 76
def push(component_id = nil)
  components = component_id.nil? ? cosmos_config.component_keys : [component_id]
  say "#{banner}\n"

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

    components.each do |id|
      say "\n#{get_key_value('Component', id)}\n"

      component_config = JSON.generate(cosmos_config.cosmos_component id)

      @api = BBC::Cosmos::Tools::API.new(config.app["api"], options[:key_path])
      response = @api.put sprintf(config.app["config_endpoint"], options[:env], id), component_config

      if response.success?
        say "Successfully saved", :green
      else
        api_error response
      end
    end

  else
    error "Action cancelled"
  end

rescue Exception => e
  error e.message
end