module NewRelicYML

Constants

CRITICAL
DEFAULTS
PROCS

Don’t evaluate Procs, instead use set values

SECURITY_BEGIN
SECURITY_END
SKIP

Skip because not configurable via yml

Public Class Methods

agent_configs_yml(agent_configs) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 176
def self.agent_configs_yml(agent_configs)
  agent_yml = ''
  agent_configs.each do |key, value|
    agent_yml += "#{value[:description]}\n  # #{key}: #{value[:default]}\n\n"
  end

  agent_yml
end
build_config(key, value) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 121
def self.build_config(key, value)
  sanitized_description = sanitize_description(value[:description])
  description = format_description(sanitized_description)
  default = default_value(key, value)

  [description, default]
end
build_string(defaults) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 194
def self.build_string(defaults)
  agent_configs, security_configs = get_configs(defaults)
  agent_string = agent_configs_yml(agent_configs)
  security_string = security_configs_yml(security_configs)

  agent_string + SECURITY_BEGIN + security_string + SECURITY_END + "\n"
end
default_value(key, config_hash) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 164
def self.default_value(key, config_hash)
  if PROCS.include?(key)
    PROCS[key]
  else
    default = config_hash[:documentation_default].nil? ? config_hash[:default] : config_hash[:documentation_default]
    default = 'nil' if default.nil?
    default = '""' if default == ''

    default
  end
end
deprecated?(value) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 133
def self.deprecated?(value)
  value[:deprecated] == true
end
format_description(description) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 152
def self.format_description(description)
  # remove leading and trailing whitespace
  description.strip!
  # wrap text after 80 characters, assuming we're at one tabstop's (two
  # spaces') level of indentation already
  description.gsub!(/(.{1,78})(\s+|\Z)/, "\\1\n")
  # add hashtags to lines
  description = description.split("\n").map { |line| "  # #{line}" }.join("\n")

  description
end
get_configs(defaults) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 97
def self.get_configs(defaults)
  agent_configs = {}
  security_configs = {}

  defaults.sort.each do |key, value|
    next if CRITICAL.include?(key) || SKIP.include?(key)

    next unless public_config?(value) && !deprecated?(value)

    # TODO: OLD RUBIES < 2.6
    # Remove `to_s`. `start_with?` doesn't accept symbols in Ruby <2.6
    if key.to_s.start_with?('security.')
      description, default = build_config(key, value)
      security_configs[key] = {description: description, default: default}
      next
    end

    description, default = build_config(key, value)
    agent_configs[key] = {description: description, default: default}
  end

  [agent_configs, security_configs]
end
public_config?(value) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 129
def self.public_config?(value)
  value[:public] == true
end
sanitize_description(description) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 137
def self.sanitize_description(description)
  # remove callouts
  description = description.split("\n").reject { |line| line.match?('</?Callout') }.join("\n")
  # remove InlinePopover, keep the text inside type
  description.gsub!(/<InlinePopover type="(.*)" \/>/, '\1')
  # remove hyperlinks
  description.gsub!(/\[([^\]]+)\]\([^\)]+\)/, '\1')
  # remove single pairs of backticks
  description.gsub!(/`([^`]+)`/, '\1')
  # removed href links
  description.gsub!(/<a href="(.*)">(.*)<\/a>/, '\2')

  description
end
security_configs_yml(security_configs) click to toggle source
# File lib/tasks/helpers/newrelicyml.rb, line 185
def self.security_configs_yml(security_configs)
  security_yml = ''
  security_configs.each do |key, value|
    security_yml += "#{value[:description]}\n  # #{key}: #{value[:default]}\n\n"
  end

  security_yml
end
write_file(defaults = DEFAULTS) click to toggle source

:nocov:

# File lib/tasks/helpers/newrelicyml.rb, line 203
def self.write_file(defaults = DEFAULTS)
  File.write('newrelic.yml', HEADER + build_string(defaults) + FOOTER)
end