Class: Aldagai::VariableManager

Inherits:
Object
  • Object
show all
Defined in:
lib/aldagai/variable_managers/base.rb

Constant Summary collapse

DEFAULT_ENVIRONMENTS =
[
  'development',
  'staging',
  'production',
]

Instance Method Summary collapse

Constructor Details

#initializeVariableManager

Returns a new instance of VariableManager.



14
15
16
17
# File 'lib/aldagai/variable_managers/base.rb', line 14

def initialize
  @config    = Aldagai::Config.new
  @encryptor = Aldagai::Encryptor.new(@config.secret)
end

Instance Method Details

#add(name, variables) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/aldagai/variable_managers/base.rb', line 19

def add(name, variables)
  if environments.length == variables.length
    environments.zip(variables).each do |environment, value|
      lines = lines_for_environment_without(environment, name)

      rewrite_file_with_lines(environment, lines) do |file|
        file.puts @encryptor.encrypt("#{name.upcase}=#{value}")
      end
    end
  else
    raise Aldagai::NotEnoghtVariablesException.new(environments.length)
  end
end

#clearObject



85
86
87
88
89
# File 'lib/aldagai/variable_managers/base.rb', line 85

def clear
  environments.each do |environment|
    rewrite_file_with_lines(environment, [])
  end
end

#delete(name) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/aldagai/variable_managers/base.rb', line 33

def delete(name)
  environments.each do |environment|
    lines = lines_for_environment_without(environment, name)

    rewrite_file_with_lines(environment, lines) do |file|
      file.puts @encryptor.encrypt("#{name.upcase}=")
    end
  end
end

#listObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aldagai/variable_managers/base.rb', line 62

def list
  puts "\n  ==== Variable actions ====\n".blue

  environments.each do |environment|
    lines = lines_for_environment(environment)

    if lines.empty?
      log_no_variables_promoted(environment)
    else
      lines.each do |line|
        name, value = line.split('=')
        value = value_presence(value)

        log_variable_will_be_added_or_removed(name, value, environment)
      end

      log_empty_line
    end
  end

  log_empty_line
end

#raw_listObject



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/aldagai/variable_managers/base.rb', line 104

def raw_list
  lines  = lines_for_environment(ENV['ALDAGAI_PIPELINE_ENV'])

  if lines.empty?
    log_no_variables_promoted(ENV['ALDAGAI_PIPELINE_ENV'])
  else
    variables = get_variables_from_lines(lines)

    variables.each do |(key, value)|
      puts "#{key}=#{value}"
    end
  end
end

#setObject



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aldagai/variable_managers/base.rb', line 91

def set
  heroku = PlatformAPI.connect_oauth(ENV['ALDAGAI_HEROKU_TOKEN'])
  lines  = lines_for_environment(ENV['ALDAGAI_PIPELINE_ENV'])

  if lines.empty?
    log_no_variables_promoted(ENV['ALDAGAI_PIPELINE_ENV'])
  else
    variables = get_variables_from_lines(lines)

    heroku.config_var.update(ENV['ALDAGAI_APP_NAME'], variables)
  end
end

#show(name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/aldagai/variable_managers/base.rb', line 43

def show(name)
  puts "\n  ==== Variable actions ====\n".blue

  environments.each do |environment|
    line = line_for_environment_with(environment, name)

    if line
      name, value = line.split('=')
      value = value_presence(value)

      log_variable_will_be_added_or_removed(name, value, environment)
    else
      log_no_variables_promoted(environment)
    end
  end

  log_empty_line
end