forked from aeolus-incubator/thor-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aeolus-incubator#42 from jistr/config_loading
Config loading refactored
- Loading branch information
Showing
9 changed files
with
315 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
require 'active_support/core_ext/hash/deep_merge' | ||
require 'aeolus/cli/errors' | ||
|
||
# push_config requirements | ||
require 'aeolus/client/base' | ||
require 'logger' | ||
|
||
module Aeolus::Cli | ||
class Config < Hash | ||
DEFAULT_CONFIG_FILE = | ||
File.expand_path(File.join(File.dirname(__FILE__), | ||
'..', '..', '..', # thor-cli gem dir | ||
'templates', 'default_config.yml')) | ||
|
||
def push | ||
ActiveResource::Base.logger = case self[:logging][:file] | ||
when /\Astdout\Z/i | ||
Logger.new(STDOUT) | ||
when /\Astderr\Z/i | ||
Logger.new(STDERR) | ||
else | ||
Logger.new(File.expand_path(self[:logging][:file])) | ||
end | ||
ActiveResource::Base.logger.level = Logger.const_get(self[:logging][:level].upcase) | ||
|
||
Aeolus::Client::Base.site = self[:conductor][:url] | ||
Aeolus::Client::Base.user = self[:conductor][:username] | ||
Aeolus::Client::Base.password = self[:conductor][:password] | ||
end | ||
|
||
def validate! | ||
error_class = Aeolus::Cli::ConfigError | ||
required_attributes = { | ||
'--conductor-url' => self[:conductor][:url], | ||
'--username' => self[:conductor][:username], | ||
'--password' => self[:conductor][:password], | ||
} | ||
required_attributes.each do |name, value| | ||
raise error_class, "Setting #{name} is required." unless value | ||
end | ||
true | ||
end | ||
|
||
|
||
class << self | ||
def new_from_hash(hash) | ||
self.new.merge!(hash) | ||
end | ||
|
||
def load_config(options) | ||
config_hash = {} | ||
config_hash.deep_merge!(config_file_hash(DEFAULT_CONFIG_FILE)) | ||
config_hash.deep_merge!(config_file_hash(config_file_to_load)) if config_file_to_load | ||
config_hash.deep_merge!(options_hash(options)) | ||
self.new_from_hash(config_hash) | ||
end | ||
|
||
private | ||
|
||
def config_file_to_load | ||
env_config = ENV['AEOLUS_CLI_CONF'] | ||
home_config = "#{ENV['HOME']}/.aeolus-cli" | ||
global_config = "/etc/aeolus-cli" | ||
|
||
return env_config if env_config | ||
return home_config if File.exists?(home_config) | ||
return global_config if File.exists?(global_config) | ||
nil | ||
end | ||
|
||
def config_file_hash(file_name) | ||
unless File.file?(file_name) | ||
raise Aeolus::Cli::ConfigError.new("Config file '#{file_name}' does not exist.") | ||
end | ||
|
||
YAML::load(File.read(File.expand_path(file_name))) | ||
end | ||
|
||
def options_hash(options) | ||
hash = {} | ||
hash.deep_merge!({ :conductor => { :url => options[:conductor_url] } }) if options[:conductor_url] | ||
hash.deep_merge!({ :conductor => { :username => options[:username] } }) if options[:username] | ||
hash.deep_merge!({ :conductor => { :password => options[:password] } }) if options[:password] | ||
hash | ||
end | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Aeolus::Cli::Error < StandardError | ||
attr_reader :message | ||
|
||
def initialize(message) | ||
@message = message | ||
end | ||
end | ||
|
||
class Aeolus::Cli::ConfigError < Aeolus::Cli::Error; end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.