Class: Performa::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/performa/configuration.rb

Constant Summary collapse

DEFAULT_FILES =
%w[
  .performa.yml performa.yml config/performa.yml
].freeze
ERR_READING_CONFIG_FILE =
"Could not read config file %s (%s)"
ERR_INVALID_FILE =
"Invalid YAML file %s: %s"
ERR_NO_FILE =
"Could not find a default config file (#{DEFAULT_FILES.join(', ')})"
InvalidDataError =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file) ⇒ Configuration

Returns a new instance of Configuration.

Raises:



19
20
21
22
23
24
25
# File 'lib/performa/configuration.rb', line 19

def initialize(config_file)
  config_file ||= find_default_config_file
  raise(Error, ERR_NO_FILE) unless config_file

  @data = load_config_file(config_file)
  validate_data
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



17
18
19
# File 'lib/performa/configuration.rb', line 17

def data
  @data
end

Class Method Details

.generate_file(file) ⇒ Object

Raises:

  • (Errno::EEXIST)


56
57
58
59
60
61
# File 'lib/performa/configuration.rb', line 56

def self.generate_file(file)
  raise Errno::EEXIST if File.exist?(file)

  template_file = File.join(__dir__, "configuration-template.yml")
  FileUtils.cp(template_file, file, verbose: true)
end

Instance Method Details

#[](name) ⇒ Object



48
49
50
# File 'lib/performa/configuration.rb', line 48

def [](name)
  @data[name]
end

#cachable_envs?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/performa/configuration.rb', line 52

def cachable_envs?
  !(@data["cache_environments"] == false || @data["stages"].nil?)
end

#find_default_config_fileObject



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

def find_default_config_file
  DEFAULT_FILES.find do |file|
    File.exist?(file)
  end
end

#load_config_file(config_file) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/performa/configuration.rb', line 27

def load_config_file(config_file)
  YAML.safe_load(File.read(config_file))
rescue Errno::EACCES => error
  raise Error, format(ERR_READING_CONFIG_FILE, config_file, error.message)
rescue Psych::SyntaxError => error
  raise Error, format(ERR_INVALID_FILE, config_file, error.message)
end

#validate_dataObject



41
42
43
44
45
46
# File 'lib/performa/configuration.rb', line 41

def validate_data
  raise InvalidDataError if !@data["images"]&.is_a?(Array) ||
                            @data["command"]&.empty?
rescue InvalidDataError
  raise Error, "Invalid config"
end