Class: Puppet::FileServing::Configuration::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/file_serving/configuration/parser.rb

Constant Summary collapse

Mount =
Puppet::FileServing::Mount
MODULES =
'modules'

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Parser


57
58
59
# File 'lib/puppet/file_serving/configuration/parser.rb', line 57

def initialize(filename)
  @file = Puppet::Util::WatchedFile.new(filename)
end

Instance Method Details

#changed?Boolean


61
62
63
# File 'lib/puppet/file_serving/configuration/parser.rb', line 61

def changed?
  @file.changed?
end

#parseObject

Parse our configuration file.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/file_serving/configuration/parser.rb', line 9

def parse
  raise(_("File server configuration %{config_file} does not exist") % { config_file: @file }) unless Puppet::FileSystem.exist?(@file)
  raise(_("Cannot read file server configuration %{config_file}") % { config_file: @file }) unless FileTest.readable?(@file)

  @mounts = {}
  @count = 0

  File.open(@file) do |f|
    mount = nil
    f.each_line do |line|
      # Have the count increment at the top, in case we throw exceptions.
      @count += 1

      case line
      when /^\s*#/; next # skip comments
      when /^\s*$/; next # skip blank lines
      when /\[([-\w]+)\]/
        mount = newmount($1)
      when /^\s*(\w+)\s+(.+?)(\s*#.*)?$/
        var = $1
        value = $2
        value.strip!
        raise(ArgumentError, _("Fileserver configuration file does not use '=' as a separator")) if value =~ /^=/
        case var
        when "path"
          path(mount, value)
        when "allow"
          allow(mount, value)
        when "deny"
          deny(mount, value)
        else
          error_location_str = Puppet::Util::Errors.error_location(@file.filename, @count)
          raise ArgumentError.new(_("Invalid argument '%{var}' at %{error_location}") %
                                      { var: var, error_location: error_location_str })
        end
      else
        error_location_str = Puppet::Util::Errors.error_location(@file.filename, @count)
        raise ArgumentError.new(_("Invalid entry at %{error_location}: '%{file_text}'") %
                                    { file_text: line.chomp, error_location: error_location_str })
      end
    end
  end

  validate

  @mounts
end