Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hunt for config in parent directories #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions lib/app_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

end