lib/kakine/config.rb in kakine-0.4.0 vs lib/kakine/config.rb in kakine-0.5.0
- old
+ new
@@ -1,39 +1,54 @@
require 'yao'
require 'yaml'
module Kakine
class Config
+ OS_PARAMS = %w[auth_url username password]
+
+ @@config = {}
+
def self.setup
load_config
+ load_env
+ validate_config
setup_yao
end
private
def self.load_config
- config_file = File.join(Dir.home, '.kakine')
- raise '~/.kakine is missing' unless File.exists?(config_file)
+ config =
+ begin
+ YAML.load_file(File.join(Dir.home, '.kakine'))
+ rescue Errno::ENOENT
+ return
+ end
- config = YAML.load_file(config_file)
+ @@config.merge!(config)
+ end
- %w[auth_url tenant username password].each do |conf_item|
- raise "Configuration '#{conf_item}' is missing. Check your ~/.kakine" unless config[conf_item]
+ def self.load_env
+ OS_PARAMS.each do |param|
+ env = "OS_#{param.upcase}"
+ @@config[param] = ENV[env] if ENV[env]
end
+ end
- @@auth_url = config['auth_url']
- @@tenant = config['tenant']
- @@username = config['username']
- @@password = config['password']
- true
+ def self.validate_config
+ OS_PARAMS.each do |param|
+ unless @@config[param]
+ raise "Configuration '#{param}' is missing. Check your ~/.kakine or export OS_#{param.upcase}."
+ end
+ end
end
def self.setup_yao
Yao.configure do
- auth_url @@auth_url
- tenant_name @@tenant
- username @@username
- password @@password
+ auth_url @@config['auth_url']
+ tenant_name Kakine::Option.tenant_name
+ username @@config['username']
+ password @@config['password']
end
end
end
end