class YleTf::VarsFile

Constants

ENV_DIR

Attributes

path[R]

Public Class Methods

find_env_vars_file(config) click to toggle source

Returns the env specific tfvars file path if it exists

# File lib/yle_tf/vars_file.rb, line 8
def self.find_env_vars_file(config)
  path = "#{config.module_dir}/#{ENV_DIR}/#{config.tf_env}.tfvars"
  VarsFile.new(path) if File.exist?(path)
end
list_all_envs(config) click to toggle source

Returns all envs that have tfvars files

# File lib/yle_tf/vars_file.rb, line 14
def self.list_all_envs(config)
  Dir.glob("#{config.module_dir}/#{ENV_DIR}/*.tfvars").sort.map do |path|
    File.basename(path, '.tfvars')
  end
end
new(path) click to toggle source
# File lib/yle_tf/vars_file.rb, line 22
def initialize(path)
  @path = path
end

Public Instance Methods

append_file(vars_file) click to toggle source
# File lib/yle_tf/vars_file.rb, line 30
def append_file(vars_file)
  File.open(path, 'a') do |file|
    file.puts # ensure we don't append to an existing line
    file.puts(vars_file.read)
  end
end
append_vars(vars) click to toggle source
# File lib/yle_tf/vars_file.rb, line 37
def append_vars(vars)
  File.open(path, 'a') do |file|
    file.puts # ensure we don't append to an existing line
    vars.each do |key, value|
      file.puts "#{key} = #{eval_value(value)}"
    end
  end
end
eval_value(value) click to toggle source
# File lib/yle_tf/vars_file.rb, line 46
def eval_value(value)
  case value
  when Hash
    %({ #{value.map { |k, v| "#{k} = #{eval_value(v)}" }.join(', ')} })
  when Array
    %([ #{value.map { |item| eval_value(item) }.join(', ')} ])
  else
    %("#{value}")
  end
end
read() click to toggle source
# File lib/yle_tf/vars_file.rb, line 26
def read
  IO.read(path)
end