Module: Grably::Core::Configuration

Defined in:
lib/grably/core/configuration.rb,
lib/grably/core/configuration/pretty_print.rb

Overview

:nodoc:

Defined Under Namespace

Classes: ConfigurationVisitor

Constant Summary collapse

ENV_PROFILE_KEY =

Key where required profile is placed. To load grably with profile ‘foo,bar` one need to run `rake mp=foo,bar task1, task2, … taskN`

'mp'.freeze
ENV_BINCONFIG_KEY =

Key where binary configuration stored if any

'BIN_CONFIG'.freeze
CONFIGURATION_FILES =

Default configuration file names

%w(grably.yml grably.user.yml grably.override.yml).freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bin_configObject

Reads binary configuration as YAML configuration stream so it could be merged with other streams



60
61
62
63
64
65
66
67
68
# File 'lib/grably/core/configuration.rb', line 60

def bin_config
  data = hex_to_string ENV[ENV_BINCONFIG_KEY]
  # Data will be walid YAML string. Trick is ident its contend and
  # attach to ^top profile
  [
    "^top:\n" + data.split("\n").map { |x| '  ' + x }.join("\n"),
    ENV_BINCONFIG_KEY
  ]
end

.bin_config?Boolean

Tells if binary configuration is provided

Returns:

  • (Boolean)


79
80
81
# File 'lib/grably/core/configuration.rb', line 79

def bin_config?
  ENV[ENV_BINCONFIG_KEY]
end

.hex_to_string(str) ⇒ Object

Converts hex string representation to plain string



72
73
74
75
76
# File 'lib/grably/core/configuration.rb', line 72

def hex_to_string(str)
  str.each_char.each_slice(2).inject('') do |acc, elem|
    acc + elem.map(&:chr).inject(&:+).hex.chr
  end
end

.load(dir: nil, profile: []) ⇒ OpenStruct

Read configuration from configuration files.

Parameters:

  • dir (String) (defaults to: nil)

    base directory path for provided files may be nil

Returns:

  • (OpenStruct)

    resolved profile values



41
42
43
44
45
# File 'lib/grably/core/configuration.rb', line 41

def load(dir: nil, profile: [])
  profile += (ENV[ENV_PROFILE_KEY] || 'default').split(',')
  puts 'Loding profile ' + profile.join('/')
  read(profile, *load_configuration_streams(dir))
end

.load_configuration_streams(dir) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/grably/core/configuration.rb', line 47

def load_configuration_streams(dir)
  # Read all known files
  streams = CONFIGURATION_FILES
            .map { |f| [dir ? File.join(Dir.pwd, f) : f, f] }
            .select { |path, _name| File.exist?(path) }
            .map { |path, name| [IO.read(path), name] }

  streams << bin_config if bin_config?
  streams
end

.read(profile, *streams) ⇒ OpenStruct

Generates configuration object for given profile and list of streams with YAML document names to read

Parameters:

  • profile (Array)

    list of profile names to merge

  • streams (Array)

    list of YAML documents and their

Returns:

  • (OpenStruct)

    instance which contains all resolved profile fields



31
32
33
34
35
# File 'lib/grably/core/configuration.rb', line 31

def read(profile, *streams)
  obj = Jac::Configuration.read(profile, *streams)
  obj.extend(self)
  obj
end

Instance Method Details

#pretty_printObject

rubocop:enable all



15
16
17
18
19
# File 'lib/grably/core/configuration/pretty_print.rb', line 15

def pretty_print
  visitor = ConfigurationVisitor.create
  visitor << to_h
  visitor.tree.yaml STDOUT, {}
end