Class: Contrast::Components::Config::Sources

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/components/config/sources.rb

Overview

This component encapsulates storing the source for each entry in the config, so that we can report on where the value was set from.

Constant Summary collapse

ENVIRONMENT_VARIABLE =

[ CORPORATE_RULE, COMMAND_LINE, JAVA_SYSTEM_PROPERTY, ENVIRONMENT_VARIABLE, APP_CONFIGURATION_FILE, USER_CONFIGURATION_FILE, CONTRAST_UI, DEFAULT_VALUE ]

'ENVIRONMENT_VARIABLE'
COMMAND_LINE =
'COMMAND_LINE'
CONTRAST_UI =
'CONTRAST_UI'
DEFAULT_VALUE =
'DEFAULT_VALUE'
APP_CONFIGURATION_FILE =
'USER_CONFIGURATION_FILE'
APP_CONFIGURATION_EXTENSIONS =

Order matters for the Configurations files. This is read when Agent starts up and will always go through the YAML as priority. Do not change the order!

%w[yaml yml].cs__freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Sources

Returns a new instance of Sources.



29
30
31
# File 'lib/contrast/components/config/sources.rb', line 29

def initialize data = {}
  @data = data
end

Instance Attribute Details

#dataHash (readonly)

Returns:



27
28
29
# File 'lib/contrast/components/config/sources.rb', line 27

def data
  @data
end

Instance Method Details

#configuration_file_source?(path, source: false) ⇒ Boolean

Returns true if the entry is from a YAML file.

Parameters:

  • path (String)

    the canonical name for the config entry (such as api.proxy.enable) or source if the source(CONTRAST_UI, ENVIRONMENT_VARIABLE…) flag is set true.

  • source (Boolean) (defaults to: false)

    flag to specify we are passing in a source, no need to look for it.

Returns:

  • (Boolean)

    true if the entry is from a YAML file



66
67
68
69
70
71
# File 'lib/contrast/components/config/sources.rb', line 66

def configuration_file_source? path, source: false
  s = source ? path : Contrast::CONFIG.sources.get(path)
  return true if s.include?(APP_CONFIGURATION_EXTENSIONS[0]) || s.include?(APP_CONFIGURATION_EXTENSIONS[1])

  false
end

#for(type) ⇒ Hash

Finds entries within config source data for the specified type, and returns them along with the current values for each.

Parameters:

  • type (String)

    a source type (ENV, CLI, ContrastUI, YAML)

Returns:

  • (Hash)

    the entries for the provided source, along with the associated values



58
59
60
# File 'lib/contrast/components/config/sources.rb', line 58

def for type
  deep_select(data.dup, type, [])
end

#get(path) ⇒ String

Retrieves the current config source for the specified config path. If no source is set then returns the Default value.

Parameters:

  • path (String)

    the canonical name for the config entry (such as api.proxy.enable)

Returns:

  • (String)

    the source for the entry



38
39
40
41
42
# File 'lib/contrast/components/config/sources.rb', line 38

def get path
  data.dig(*parts_for(path)) || DEFAULT_VALUE
rescue TypeError
  DEFAULT_VALUE
end

#set(path, source) ⇒ String

Assigns the config source for a specified config path.

Parameters:

  • path (String)

    the canonical name for the config entry (such as api.proxy.enable)

  • source (String)

    the source for the entry

Returns:

  • (String)

    the source type for the entry



49
50
51
# File 'lib/contrast/components/config/sources.rb', line 49

def set path, source
  assign_value(data, parts_for(path), source)
end

#source_overridden?(path) ⇒ Boolean

Check to see whether the source has been overridden by local settings.

Parameters:

  • path (String)

    the canonical name for the config entry (such as api.proxy.enable)

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/contrast/components/config/sources.rb', line 75

def source_overridden? path
  source = Contrast::CONFIG.sources.get(path)
  return true if [ENVIRONMENT_VARIABLE, COMMAND_LINE].include?(source)
  return true if configuration_file_source?(source, source: true)

  false
end