From fc16fd2c961ced389e206a96f02913a77a1e760b Mon Sep 17 00:00:00 2001 From: Will Boyce Date: Thu, 11 Jun 2015 12:17:25 +0100 Subject: [PATCH] hunt for config in parent directories --- lib/app_config.rb | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/lib/app_config.rb b/lib/app_config.rb index 14c89ae..b0fb729 100644 --- a/lib/app_config.rb +++ b/lib/app_config.rb @@ -2,39 +2,47 @@ class AppConfig FILENAME = ".pt.yaml" - + def self.load require "yaml" # Read YAML config or quit. + path = Dir.pwd config = nil - begin - config = YAML.load_file FILENAME - rescue Exception => e + while config.nil? + candidate = File.join(path, FILENAME) + if not File.exists?(candidate) + path = File.absolute_path(File.join(path, '..')) + else + config = YAML.load_file(candidate) + end + break if path == File.absolute_path(File.join(path, '..')) + end + if config.nil? raise "No #{FILENAME} file found in current directory; please create one." end - + AppConfig.new :config => config end - + def initialize opts={:config => Hash.new} @config = opts[:config] - + raise "Config must include \"project\" key" if not @config.has_key? "project" raise "Config must include \"api_key\" key" if not @config.has_key? "api_key" raise "Config must include \"user\" key" if not @config.has_key? "user" end - + def project @config["project"] end - + def api_key @config["api_key"] end - + def user @config["user"] end - -end \ No newline at end of file + +end