lib/visage/config/init.rb in visage-app-0.2.7 vs lib/visage/config/init.rb in visage-app-0.3.0
- old
+ new
@@ -1,38 +1,57 @@
#!/usr/bin/env ruby
@root = Pathname.new(File.dirname(__FILE__)).parent.parent.parent.expand_path
-@config_directory = Pathname.new(File.dirname(__FILE__)).expand_path
require @root.join('lib/visage/config')
require 'yaml'
-Visage::Config.use do |c|
- # setup profiles file
- profile_filename = @config_directory.join('profiles.yaml')
- unless File.exists?(profile_filename)
- FileUtils.touch(profile_filename)
- end
+module Visage
+ class Config
+ class File
+ @@config_directories = []
+ @@config_directories << Pathname.new(::File.dirname(__FILE__)).expand_path
+ @@config_directories << Pathname.new(ENV["CONFIG_PATH"]).expand_path if ENV["CONFIG_PATH"]
+ @@config_directories.reverse!
- # setup plugin colors file
- plugin_colors_filename = @config_directory.join('plugin-colors.yaml')
- unless File.exists?(plugin_colors_filename)
- puts "It's highly recommended you specify graph line colors in config/plugin-colors.yaml!"
- end
+ def self.find(filename, opts={})
+ range = opts[:ignore_bundled] ? (0..-2) : (0..-1)
+ potential_filenames = @@config_directories[range].map {|d| d.join(filename)}
+ potential_filenames.find { |f| ::File.exists?(f) }
+ end
- # load config from profiles + plugin colors file
- [profile_filename, plugin_colors_filename].each do |filename|
- if File.exists?(filename)
- config = YAML::load_file(filename) || {}
- config.each_pair {|key, value| c[key] = value}
- end
- end
+ def self.load(filename, opts={})
+ unless path = self.find(filename, opts)
+ if opts[:create]
+ path = @@config_directories.first.join(filename)
+ begin
+ FileUtils.touch(path)
+ rescue Errno::EACCES => e
+ raise Errno::EACCES, "Couldn't write #{path}. Do you have CONFIG_PATH set?"
+ end
+ end
+ end
- # load fallback colors
- c['fallback_colors'] = YAML::load(File.read(@config_directory.join('fallback-colors.yaml')))
+ YAML::load_file(path)
+ end
- # Location of collectd's RRD - you may want to edit this!
- c['rrddir'] = "/var/lib/collectd/rrd"
+ def self.open(filename, &block)
+ path = self.find(filename)
+ ::File.open(path, 'r+') do |f|
+ block.call(f)
+ end
+ end
- # whether to shade in graphs
- c['shade'] = false
+ def initialize(filename, opts={})
+ unless ::File.exists?(filename)
+ path = @@config_directories.first.join(filename)
+ FileUtils.touch(path)
+ end
+ @file = ::File.open(filename, 'r+')
+ end
+
+ def to_s
+ @file.path
+ end
+ end
+ end
end