Module: Datadog::Core::Environment::VariableHelpers

Extended by:
VariableHelpers
Included in:
VariableHelpers
Defined in:
lib/datadog/core/environment/variable_helpers.rb

Overview

Defines helper methods for environment

Instance Method Summary collapse

Instance Method Details

#env_to_bool(var, default = nil, deprecation_warning: true) ⇒ Boolean, default

Reads an environment variable as a Boolean.

Parameters:

  • var (String)

    environment variable

  • var (Array<String>)

    list of environment variables

  • default (Boolean) (defaults to: nil)

    the default value if the keys in ‘var` are not present in the environment

  • deprecation_warning (Boolean) (defaults to: true)

    when ‘var` is a list, record a deprecation log when the first key in `var` is not used.

Returns:

  • (Boolean)

    if the environment value is the string ‘true`

  • (default)

    if the environment value is not found



19
20
21
22
# File 'lib/datadog/core/environment/variable_helpers.rb', line 19

def env_to_bool(var, default = nil, deprecation_warning: true)
  var = decode_array(var, deprecation_warning)
  var && ENV.key?(var) ? ENV[var].to_s.strip.downcase == 'true' : default
end

#env_to_float(var, default = nil, deprecation_warning: true) ⇒ Float, default

Reads an environment variable as a Float.

Parameters:

  • var (String)

    environment variable

  • var (Array<String>)

    list of environment variables

  • default (Float) (defaults to: nil)

    the default value if the keys in ‘var` are not present in the environment

  • deprecation_warning (Boolean) (defaults to: true)

    when ‘var` is a list, record a deprecation log when the first key in `var` is not used.

Returns:

  • (Float)

    if the environment value is a valid Float

  • (default)

    if the environment value is not found



47
48
49
50
# File 'lib/datadog/core/environment/variable_helpers.rb', line 47

def env_to_float(var, default = nil, deprecation_warning: true)
  var = decode_array(var, deprecation_warning)
  var && ENV.key?(var) ? ENV[var].to_f : default
end

#env_to_int(var, default = nil, deprecation_warning: true) ⇒ Integer, default

Reads an environment variable as an Integer.

Parameters:

  • var (String)

    environment variable

  • var (Array<String>)

    list of environment variables

  • default (Integer) (defaults to: nil)

    the default value if the keys in ‘var` are not present in the environment

  • deprecation_warning (Boolean) (defaults to: true)

    when ‘var` is a list, record a deprecation log when the first key in `var` is not used.

Returns:

  • (Integer)

    if the environment value is a valid Integer

  • (default)

    if the environment value is not found



33
34
35
36
# File 'lib/datadog/core/environment/variable_helpers.rb', line 33

def env_to_int(var, default = nil, deprecation_warning: true)
  var = decode_array(var, deprecation_warning)
  var && ENV.key?(var) ? ENV[var].to_i : default
end

#env_to_list(var, default = [], comma_separated_only:, deprecation_warning: true) ⇒ Array<Object>, default

Parses comma- or space-separated lists.

If a comma is present, then the list is considered comma-separated. Otherwise, it is considered space-separated.

After the entries are separated, commas and whitespaces that are either trailing or leading are trimmed.

Empty entries, after trimmed, are also removed from the result.

Parameters:

  • var (String)

    environment variable

  • var (Array<String>)

    list of environment variables

  • default (Array<Object>) (defaults to: [])

    the default value if the keys in ‘var` are not present in the environment

  • deprecation_warning (Boolean) (defaults to: true)

    when ‘var` is a list, record a deprecation log when the first key in `var` is not used.

Returns:

  • (Array<Object>)

    if the environment value is a valid list

  • (default)

    if the environment value is not found



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/datadog/core/environment/variable_helpers.rb', line 69

def env_to_list(var, default = [], comma_separated_only:, deprecation_warning: true)
  var = decode_array(var, deprecation_warning)
  if var && ENV.key?(var)
    value = ENV[var]

    values = if value.include?(',') || comma_separated_only
               value.split(',')
             else
               value.split(' ') # rubocop:disable Style/RedundantArgument
             end

    values.map! do |v|
      v.gsub!(/\A[\s,]*|[\s,]*\Z/, '')

      v.empty? ? nil : v
    end

    values.compact!
    values
  else
    default
  end
end