Skip to content

Instantly share code, notes, and snippets.

@guyhughes
Created May 18, 2014 01:44
Show Gist options
  • Save guyhughes/2d314495eed3e1fcf1f5 to your computer and use it in GitHub Desktop.
Save guyhughes/2d314495eed3e1fcf1f5 to your computer and use it in GitHub Desktop.
Ruby: Load the best available config file using ::YAML, of course
require 'thor'
class_option :config, :type => :string, :aliases => "-c", :desc => "Use an alternative YAML config file."
def initialize(*args)
super
config_files = Array.new
config_files.push(options[:config]) if options[:config]
config_files.push("#{ENV['XDG_CONFIG_HOME']}/awesomeness/awesomeness.yaml") if ENV['XDG_CONFIG_HOME']
config_files.push("#{ENV['HOME'] || '~'}/.config/awesomeness.yaml")
config_files.each { |file|
if File::file?(file) and File::readable?(file)
if not File::stat(file).mode =~ /.*744/
File::chmod(0744,file)
end
@config_file = file
break
end
}
raise "No configuration file found, tried #{config_files.inspect}" unless defined? @config_file
@log.debug("initialize: loading YAML config file #{@config_file}")
@config = YAML.load_file(@config_file)
raise "Configuration empty. YAML file corrupt. Available data is #{@config.inspect}" if @config.empty?
end
# vim: set ts=2 sts=2 ft=ruby fdm=marker sw=2 et:

Ruby: Load the best available config file using ::YAML, of course

... just so I don't try to reinvent my wheel again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment