ReinH (owner)

Forks

Revisions

gist: 205143 Download_button fork
public
Public Clone URL: git://gist.github.com/205143.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
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
56
57
58
59
60
61
62
63
64
module Factor
  def self.add(name, &block)
    Factor::Resolvers.add(name, block)
  end
 
  def self.value(name)
    # needs moar delegation
    Factor::Resolvers::RESOLVERS[name].value
  end
end
 
class Factor::Resolvers
  # resolver table
  RESOLVERS = {}
 
  def self.add(name, &block)
    RESOLVERS[name] = new.instance_eval(block)
  end
 
  def exec(command)
    Factor::Resolvers::Exec.new(command)
  end
 
  def command(&block)
    Factor::Resolvers::Command.new(block)
  end
 
  def setcode(str=nil, &block)
    exec(str, block) if str
    command(block)
  end
end
 
# Abstract class used to provide default behavior to resolvers
class Facter::Resolvers::Resolver
  IDENTITY_PROC = lambda {|x| x}
 
  def value
    raise NotImplementedError, "value must be implemented in subclasses"
  end
end
 
module Facter
  class Resolvers
    # Execute a shell command and optionally pass STDOUT to a block for
    # processing (similar to a unix pipe).
    #
    # Example:
    # Exec.new('hostname') do |name|
    # name and name.match(/^([\w-]+)\.(.+)$/).first
    # end
    class Exec < Resolver
      def initialize(exec, block=nil)
        @exec = exec
        @block = block || IDENTITY_PROC
      end
 
      def value
        @value ||= @block.call(`#{@exec}`)
      end
    end
  end
end