Module: PuppetX::Bodgit::Postfix::Util

Defined in:
lib/puppet_x/bodgit/postfix/util.rb

Overview

Postfix type utility methods

Constant Summary collapse

PARAMETER_REGEXP =

Match the following provided it's not preceeded by a $:

  • $foo_bar_baz

  • $(foo_bar_baz)

  • ${foo_bar_baz}

  • ${foo_bar_baz?value}

  • ${foo_bar_baz:value}

However, due to Ruby 1.8.7 we have to do this backwards as there's no look-behind operator without pulling in Oniguruma. So anywhere this Regexp is used the target string needs to be reversed and then any captures need to be un-reversed again.

%r{
  (?:
    (
      [[:alnum:]]+
      (?:
        _
        [[:alnum:]]+
      )*
    )
    |
    \)
    (
      [[:alnum:]]+
      (?:
        _
        [[:alnum:]]+
      )*
    )
    \(
    |
    \}
    (?:
      (
        [^?:]+
      )
      (
        [?:]
      )
    )?
    (
      [[:alnum:]]+
      (?:
        _
        [[:alnum:]]+
      )*
    )
    \{
  )
  \$
  (?!
    \$
  )
}x.freeze

Instance Method Summary collapse

Instance Method Details

#expand(value) ⇒ Object

Expand variables where possible



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/puppet_x/bodgit/postfix/util.rb', line 66

def expand(value)
  v = value.reverse.clone
  loop do
    old = v.clone
    v.gsub!(PARAMETER_REGEXP) do |_s|
      replacement = Regexp.last_match(0)
      # We want all non-nil $1..n captures
      match = $LAST_MATCH_INFO.to_a[1..-1].compact.reverse.map { |x| x.reverse }
      types = catalog.resources.select do |r|
        r.is_a?(Puppet::Type.type(:postfix_main)) && r.should(:ensure) == :present
      end
      types.each do |r|
        if r.name.eql?(match[0]) && !(match[1])
          replacement = r.should(:value).reverse
        end
      end
      replacement
    end
    break if old.eql?(v)
  end
  v.reverse
end

#file_autorequires(values) ⇒ Object

Generate a list of potential candidates for file dependencies



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/puppet_x/bodgit/postfix/util.rb', line 90

def file_autorequires(values)
  requires = []
  values.each do |v|
    case v
    when %r{^(?:\/[^\/]+)+\/?$}
      # File
      requires << v
    when %r{^(?:proxy:)?([a-z]+):((?:\/[^\/]+)+)$}
      # Lookup table
      case Regexp.last_match(1)
      when 'btree', 'hash'
        requires << "#{Regexp.last_match(2)}.db"
      when 'cdb'
        requires << "#{Regexp.last_match(2)}.cdb"
      when 'dbm', 'sdbm'
        requires << "#{Regexp.last_match(2)}.dir"
        requires << "#{Regexp.last_match(2)}.pag"
      when 'lmdb'
        requires << "#{Regexp.last_match(2)}.lmdb"
      else
        # Apart from the above exceptions, target the source file
        requires << Regexp.last_match(2)
      end
    end
  end
  requires
end

#value_scan(value) ⇒ Object

Generate a list of variable names



119
120
121
122
123
124
# File 'lib/puppet_x/bodgit/postfix/util.rb', line 119

def value_scan(value)
  value.reverse.scan(PARAMETER_REGEXP).each do |s|
    s.compact!.reverse!
    yield s[0].reverse if block_given?
  end
end