... just so I don't try to reinvent my wheel again.
-
-
Save juliancheal/678e01f7e1bee2d6448606d47697a139 to your computer and use it in GitHub Desktop.
Ruby: Load the best available config file using ::YAML, of course
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment