Skip to content

Instantly share code, notes, and snippets.

@milkfarm
Last active January 3, 2016 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milkfarm/8496545 to your computer and use it in GitHub Desktop.
Save milkfarm/8496545 to your computer and use it in GitHub Desktop.
Thor task to check config/config.yml against config/config.template.yml for missing keys.
class Config < Thor
PATH = 'config/config.yml'
desc 'get KEY', "get value of key in #{PATH}"
def get(key)
%w(development production).each do |env|
puts "#{env}:"
begin
value = get_config(key, env)
puts " #{key} => '#{get_config(key, env)}'"
rescue
puts " #{key} => UNDEFINED"
end
end
end
desc 'set KEY VALUE', "set key to value in #{PATH} then restart app"
method_option :debug, :type => :boolean, :default => false, :aliases => "-d", :desc => "Debug"
method_option :verbose, :type => :boolean, :default => true, :aliases => "-v", :desc => "Verbose"
def set(key, value)
set_config(key, value)
end
desc 'check', "check #{PATH} keys against template"
method_option :env, :type => :string, :default => 'development', :aliases => "-e", :desc => "Environment must be either 'development' or 'production'"
def check
env = options[:env].to_s.downcase
abort "Environment value not acceptable" unless %w(development production).include?(env)
check_config_keys(env)
end
private
def get_config(key, env)
config = load_yml(PATH, env)
raise "Key '#{key}' not defined" unless config.has_key?(key)
config[key]
end
def set_config(key, value)
temp = Tempfile.new('temp_config.txt')
error = true
File.read(PATH).each_line do |line|
if match = /#{key}: (.*)/.match(line)
error = false
puts "Key Found. Current value '#{match[1]}'." if options[:verbose]
temp.puts line.sub(/#{key}: .*/, "#{key}: #{value}")
else
temp.puts line
end
end
if error
puts "! Error: Key '#{key}' not found"
else
puts "Moving new file into place..." if options[:verbose]
FileUtils.mv(temp.path, PATH, :noop => options[:debug], :verbose => options[:verbose])
puts "Restarting application..." if options[:verbose]
FileUtils.touch('tmp/restart.txt', :noop => options[:debug], :verbose => options[:verbose])
end
return error
end
def check_config_keys(env = nil)
ext = File.extname(PATH)
config_name = File.basename(PATH)
template_name = "#{File.basename(PATH, ext)}.template#{ext}"
config = load_yml(PATH, env)
template = load_yml("config/#{template_name}", env)
missing = template.reject { |k, v| config.has_key?(k) }
error = false
puts "Checking '#{config_name}' keys..."
if missing.any?
puts "Config file '#{config_name}' missing keys:"
missing.each {|k, v| puts " #{k}"}
error = true
end
missing = config.reject {|k, v| template.has_key?(k)}
if missing.any?
puts "Template file '#{template_name}' missing keys:"
missing.each {|k, v| puts " #{k}"}
error = true
end
puts "=> #{error ? 'FAIL' : 'PASS'}"
end
def load_yml(file, env = nil)
yml = YAML.load(File.read(file))
if env.nil?
yml
else
abort "! Error: Environment '#{env}' not defined" unless yml.has_key?(env)
yml[env]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment