Class: BBC::Cosmos::Tools::Commands::Config

Inherits:
Base
  • Object
show all
Defined in:
lib/bbc/cosmos/tools/commands/config.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#config

Instance Method Summary collapse

Constructor Details

#initialize(args = [], local_options = {}, thor_config = {}) ⇒ Config

Returns a new instance of Config.



14
15
16
17
# 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

Instance Attribute Details

#cosmos_configObject

Returns the value of attribute cosmos_config.



12
13
14
# File 'lib/bbc/cosmos/tools/commands/config.rb', line 12

def cosmos_config
  @cosmos_config
end

Instance Method Details

#generate(component) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#listObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# 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