Created
October 8, 2009 16:38
-
-
Save reinh/205143 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment