summaryrefslogtreecommitdiffstats
path: root/lib/facter.rb
blob: 63eea375e6335a9a3b60414a38e6a9681a475e36 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#--
# Copyright 2006 Luke Kanies <luke@madstop.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
#--

module Facter
    # This is just so the other classes have the constant.
    module Util; end

    require 'facter/util/fact'
    require 'facter/util/collection'

    include Comparable
    include Enumerable

    FACTERVERSION = '1.5.9'
    # = Facter
    # Functions as a hash of 'facts' you might care about about your
    # system, such as mac address, IP address, Video card, etc.
    # returns them dynamically

    # == Synopsis
    #
    # Generally, treat <tt>Facter</tt> as a hash:
    # == Example
    # require 'facter'
    # puts Facter['operatingsystem']
    #

    # Set LANG to force i18n to C
    #
    ENV['LANG'] = 'C'

    GREEN = ""
    RESET = ""
    @@debug = 0
    @@timing = 0

    # module methods

    def self.collection
        unless defined?(@collection) and @collection
            @collection = Facter::Util::Collection.new
        end
        @collection
    end

    # Return the version of the library.
    def self.version
        return FACTERVERSION
    end

    # Add some debugging
    def self.debug(string)
        if string.nil?
            return
        end
        if self.debugging?
            puts GREEN + string + RESET
        end
    end

    def self.debugging?
        @@debug != 0
    end

    # show the timing information
    def self.show_time(string)
        puts "#{GREEN}#{string}#{RESET}" if string and Facter.timing?
    end

    def self.timing?
        @@timing != 0
    end

    # Return a fact object by name.  If you use this, you still have to call
    # 'value' on it to retrieve the actual value.
    def self.[](name)
        collection.fact(name)
    end

    class << self
        [:fact, :flush, :list, :value].each do |method|
            define_method(method) do |*args|
                collection.send(method, *args)
            end
        end

        [:list, :to_hash].each do |method|
            define_method(method) do |*args|
                collection.load_all
                collection.send(method, *args)
            end
        end
    end


    # Add a resolution mechanism for a named fact.  This does not distinguish
    # between adding a new fact and adding a new way to resolve a fact.
    def self.add(name, options = {}, &block)
        collection.add(name, options, &block)
    end

    def self.each
        # Make sure all facts are loaded.
        collection.load_all

        collection.each do |*args|
            yield(*args)
        end
    end

    class << self
        # Allow users to call fact names directly on the Facter class,
        # either retrieving the value or comparing it to an existing value.
        def method_missing(name, *args)
            question = false
            if name.to_s =~ /\?$/
                question = true
                name = name.to_s.sub(/\?$/,'')
            end

            if fact = collection.fact(name)
                if question
                    value = fact.value.downcase
                    args.each do |arg|
                        if arg.to_s.downcase == value
                            return true
                        end
                    end

                    # If we got this far, there was no match.
                    return false
                else
                    return fact.value
                end
            else
                # Else, fail like a normal missing method.
                raise NoMethodError, "Could not find fact '%s'" % name
            end
        end
    end

    # Clear all facts.  Mostly used for testing.
    def self.clear
        Facter.flush
        Facter.reset
    end

    # Set debugging on or off.
    def self.debugging(bit)
        if bit
            case bit
            when TrueClass; @@debug = 1
            when FalseClass; @@debug = 0
            when Fixnum
                if bit > 0
                    @@debug = 1
                else
                    @@debug = 0
                end
            when String;
                if bit.downcase == 'off'
                    @@debug = 0
                else
                    @@debug = 1
                end
            else
                @@debug = 0
            end
        else
            @@debug = 0
        end
    end

    # Set timing on or off.
    def self.timing(bit)
        if bit
            case bit
            when TrueClass; @@timing = 1
            when Fixnum
                if bit > 0
                    @@timing = 1
                else
                    @@timing = 0
                end
            end
        else
            @@timing = 0
        end
    end

    def self.warn(msg)
        if Facter.debugging? and msg and not msg.empty?
            msg = [msg] unless msg.respond_to? :each
            msg.each { |line| Kernel.warn line }
        end
    end

    # Remove them all.
    def self.reset
        @collection = nil
    end

    # Load all of the default facts, and then everything from disk.
    def self.loadfacts
        collection.load_all
    end

    @search_path = []

    # Register a directory to search through.
    def self.search(*dirs)
        @search_path += dirs
    end

    # Return our registered search directories.
    def self.search_path
        @search_path.dup
    end
end