lib/cloudstack_client/configuration.rb in cloudstack_client-1.5.0 vs lib/cloudstack_client/configuration.rb in cloudstack_client-1.5.1
- old
+ new
@@ -1,33 +1,46 @@
module CloudstackClient
require "yaml"
module Configuration
+
def self.load(configuration)
- unless File.exists?(configuration[:config_file])
- message = "Configuration file '#{configuration[:config_file]}' not found."
- raise message
+ file = configuration[:config_file] || Configuration.locate_config_file
+ unless File.exists?(file)
+ raise ConfigurationError, "Configuration file '#{file}' not found."
end
+
begin
- config = YAML::load(IO.read(configuration[:config_file]))
+ config = YAML::load(IO.read file)
rescue => e
- message = "Can't load configuration from file '#{configuration[:config_file]}'."
- message += "Message: #{e.message}" if configuration[:debug]
- message += "Backtrace:\n\t#{e.backtrace.join("\n\t")}" if configuration[:debug]
+ message = "Can't load configuration from file '#{file}'."
+ if configuration[:debug]
+ message += "\nMessage: #{e.message}"
+ message += "\nBacktrace:\n\t#{e.backtrace.join("\n\t")}"
+ end
raise message
end
- env ||= config[:default]
- if env
+ if env ||= config[:default]
unless config = config[env]
- raise "Can't find environment #{env}."
+ raise ConfigurationError, "Can't find environment #{env}."
end
end
unless config.key?(:url) && config.key?(:api_key) && config.key?(:secret_key)
message = "The environment #{env || '\'-\''} does not contain all required keys."
- raise message
+ raise ConfigurationError, message
end
+
config.merge(environment: env)
end
+
+ def self.locate_config_file
+ %w(.cloudstack .cloudstack-cli).each do |file|
+ file = File.join(Dir.home, "#{file}.yml")
+ return file if File.exists?(file)
+ end
+ nil
+ end
+
end
end