class Puppet::Settings::ConfigFile

@api private

Parses puppet configuration files

Public Class Methods

new(value_converter) click to toggle source

@param value_converter [Proc] a function that will convert strings into ruby types

# File lib/puppet/settings/config_file.rb, line 13
def initialize(value_converter)
  @value_converter = value_converter
end

Public Instance Methods

parse_file(file, text) click to toggle source
# File lib/puppet/settings/config_file.rb, line 17
def parse_file(file, text)
  result = {}
  count = 0

  # Default to 'main' for the section.
  section_name = :main
  result[section_name] = empty_section
  text.split(/\n/).each do |line|
    count += 1
    case line
    when /^\s*\[(\w+)\]\s*$/
      section_name = $1.intern
      fail_when_illegal_section_name(section_name, file, line)
      if result[section_name].nil?
        result[section_name] = empty_section
      end
    when /^\s*#/; next # Skip comments
    when /^\s*$/; next # Skip blanks
    when /^\s*(\w+)\s*=\s*(.*?)\s*$/ # settings
      var = $1.intern

      # We don't want to munge modes, because they're specified in octal, so we'll
      # just leave them as a String, since Puppet handles that case correctly.
      if var == :mode
        value = $2
      else
        value = @value_converter[$2]
      end

      # Check to see if this is a file argument and it has extra options
      begin
        if value.is_a?(String) and options = extract_fileinfo(value)
          value = options[:value]
          options.delete(:value)
          result[section_name][:_meta][var] = options
        end
        result[section_name][var] = value
      rescue Puppet::Error => detail
        raise Puppet::Settings::ParseError.new(detail.message, file, line, detail)
      end
    else
      raise Puppet::Settings::ParseError.new("Could not match line #{line}", file, line)
    end
  end

  result
end