Class: VGH::Configuration
- Inherits:
-
Object
- Object
- VGH::Configuration
- Defined in:
- lib/vgh/configuration.rb
Overview
Description:
See Configuration Section in the README file.
Usage:
config = Configuration.new.config
mysetting = config[:mysetting]
Instance Attribute Summary collapse
-
#config ⇒ Hash
readonly
Global configuration.
Instance Method Summary collapse
-
#aws_config ⇒ Object
Configures AWS.
-
#aws_logging ⇒ Hash
Implements our own Logging class.
-
#confdir ⇒ String
IF specified, use the confdir specified in the command line options.
-
#config_correct?(path) ⇒ Boolean
Checks if the configuration is a valid YAML file.
-
#config_exists?(path) ⇒ Boolean
Checks if the configuration file exists.
-
#config_file ⇒ String
The main configuration file.
-
#global_config_dir ⇒ String
The global configuration directory.
-
#initialize ⇒ Hash
constructor
Parse the main configuration.
-
#load_error(path) ⇒ Object
Returns the error message in case the configuration os not right.
-
#parse(path) ⇒ Hash
Returns a parsed configuration.
-
#user_config_dir ⇒ String
The user configuration directory.
-
#validate(path) ⇒ Hash
Returns error if the configuration is not right.
Constructor Details
#initialize ⇒ Hash
Parse the main configuration
35 36 37 38 39 |
# File 'lib/vgh/configuration.rb', line 35 def initialize .info "Loading configuration..." @config ||= validate(config_file) aws_config end |
Instance Attribute Details
#config ⇒ Hash (readonly)
Global configuration
31 32 33 |
# File 'lib/vgh/configuration.rb', line 31 def config @config end |
Instance Method Details
#aws_config ⇒ Object
Configures AWS
103 104 105 106 |
# File 'lib/vgh/configuration.rb', line 103 def aws_config AWS.config(config) AWS.config(aws_logging) end |
#aws_logging ⇒ Hash
Implements our own Logging class
110 111 112 113 114 115 |
# File 'lib/vgh/configuration.rb', line 110 def aws_logging aws_logging ||= { :logger => log, :log_formatter => AWS::Core::LogFormatter.colored } end |
#confdir ⇒ String
IF specified, use the confdir specified in the command line options
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vgh/configuration.rb', line 55 def confdir cli_confdir = cli[:confdir] global = global_config_dir if !cli_confdir.nil? return cli_confdir elsif File.directory?(global) return global else return user_config_dir end end |
#config_correct?(path) ⇒ Boolean
Checks if the configuration is a valid YAML file
92 93 94 |
# File 'lib/vgh/configuration.rb', line 92 def config_correct?(path) parse(path).kind_of?(Hash) end |
#config_exists?(path) ⇒ Boolean
Checks if the configuration file exists
86 87 88 |
# File 'lib/vgh/configuration.rb', line 86 def config_exists?(path) File.exist?(path) end |
#config_file ⇒ String
The main configuration file
69 70 71 |
# File 'lib/vgh/configuration.rb', line 69 def config_file "#{confdir}/config.yml" end |
#global_config_dir ⇒ String
The global configuration directory
43 44 45 |
# File 'lib/vgh/configuration.rb', line 43 def global_config_dir '/etc/vgh' end |
#load_error(path) ⇒ Object
Returns the error message in case the configuration os not right
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/vgh/configuration.rb', line 118 def load_error(path) log.fatal("#{path} is either missing or formatted incorrectly!") load_error = <<END #{path} is either missing or formatted incorrectly! To run this script, you need to create a file named #{path} with your configuration in the following format: # Comment :key: 'value' :string: 'string value' :integer: 123 :boolean: true/false :array: - '1st' - '2nd' :hash: - :sub_key: 'sub value' END return load_error end |
#parse(path) ⇒ Hash
Returns a parsed configuration
98 99 100 |
# File 'lib/vgh/configuration.rb', line 98 def parse(path) YAML.load(File.read(path)) end |
#user_config_dir ⇒ String
The user configuration directory
49 50 51 |
# File 'lib/vgh/configuration.rb', line 49 def user_config_dir File.('~/.vgh') end |
#validate(path) ⇒ Hash
Returns error if the configuration is not right
75 76 77 78 79 80 81 82 |
# File 'lib/vgh/configuration.rb', line 75 def validate(path) if config_exists?(path) and config_correct?(path) parse(path) else puts load_error(path) exit 1 end end |