Class: VGH::Configuration

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeHash

Parse the main configuration



35
36
37
38
39
# File 'lib/vgh/configuration.rb', line 35

def initialize
  message.info "Loading configuration..."
  @config ||= validate(config_file)
  aws_config
end

Instance Attribute Details

#configHash (readonly)

Global configuration

Returns:

  • (Hash)

    The configuration hash



31
32
33
# File 'lib/vgh/configuration.rb', line 31

def config
  @config
end

Instance Method Details

#aws_configObject

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_loggingHash

Implements our own Logging class

Returns:

  • (Hash)


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

#confdirString

IF specified, use the confdir specified in the command line options

Returns:

  • (String)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


86
87
88
# File 'lib/vgh/configuration.rb', line 86

def config_exists?(path)
  File.exist?(path)
end

#config_fileString

The main configuration file

Returns:

  • (String)


69
70
71
# File 'lib/vgh/configuration.rb', line 69

def config_file
  "#{confdir}/config.yml"
end

#global_config_dirString

The global configuration directory

Returns:

  • (String)


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

Returns:

  • (Hash)


98
99
100
# File 'lib/vgh/configuration.rb', line 98

def parse(path)
  YAML.load(File.read(path))
end

#user_config_dirString

The user configuration directory

Returns:

  • (String)


49
50
51
# File 'lib/vgh/configuration.rb', line 49

def user_config_dir
  File.expand_path('~/.vgh')
end

#validate(path) ⇒ Hash

Returns error if the configuration is not right

Returns:

  • (Hash)


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