Codebase list facter / 1f387a5
[#4552] Apply patch from Dean Wilson Rein Henrichs 13 years ago
5 changed file(s) with 77 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
2020 # yaml::
2121 # Emit facts in YAML format.
2222 #
23 # debug::
24 # Enable debugging.
25 #
26 # timing::
27 # Enable timing.
28 #
29 # help::
30 # Print this help message
31 #
2332 # puppet::
2433 # Load the Puppet libraries, thus allowing Facter to load Puppet-specific facts.
2534 #
2635 # version::
2736 # Print the version and exit.
28 #
29 # help::
30 # Print this help message.
31 #
32 # debug::
33 # Enable debugging.
3437 #
3538 # = Example
3639 #
5353 options = {}
5454 OptionParser.new do |opts|
5555 opts.on("-y", "--yaml") { |v| options[:yaml] = v }
56
5756 opts.on("-d", "--debug") { |v| Facter.debugging(1) }
57 opts.on("-t", "--timing") { |v| Facter.timing(1) }
5858 opts.on("-p", "--puppet") { |v| load_puppet }
5959
6060 opts.on_tail("-v", "--version") do
134134 def value
135135 result = nil
136136 return result if @code == nil and @interpreter == nil
137
138 starttime = Time.now.to_i
139
137140 begin
138141 Timeout.timeout(limit) do
139142 if @code.is_a?(Proc)
155158 return nil
156159 end
157160
161 finishtime = Time.now.to_i
162
163 if Facter.timing?
164 Facter.show_time "Executing #{self.name} took #{finishtime - starttime} seconds"
165 end
166
158167 return nil if result == ""
159168 return result
160169 end
4747 GREEN = ""
4848 RESET = ""
4949 @@debug = 0
50 @@timing = 0
5051
5152 # module methods
5253
7475
7576 def self.debugging?
7677 @@debug != 0
78 end
79
80 # show the timing information
81 def self.show_time(string)
82 if string.nil?
83 return
84 end
85 if self.timing?
86 puts GREEN + string + RESET
87 end
88 end
89
90 def self.timing?
91 @@timing != 0
7792 end
7893
7994 # Return a fact object by name. If you use this, you still have to call
176191 end
177192 end
178193
194 # Set timing on or off.
195 def self.timing(bit)
196 if bit
197 case bit
198 when TrueClass; @@timing = 1
199 when Fixnum
200 if bit > 0
201 @@timing = 1
202 else
203 @@timing = 0
204 end
205 end
206 else
207 @@timing = 0
208 end
209 end
179210
180211 def self.warn(msg)
181212 if Facter.debugging? and msg and not msg.empty?
134134 Facter.should respond_to(:debugging?)
135135 end
136136
137 it "should have a method to query timing mode" do
138 Facter.should respond_to(:timing?)
139 end
140
141 it "should have a method to show timing information" do
142 Facter.should respond_to(:show_time)
143 end
144
137145 it "should have a method to warn" do
138146 Facter.should respond_to(:warn)
139147 end
203211 end
204212 end
205213
214 describe "when setting timing mode" do
215 it "should have timing enabled using 1" do
216 Facter.timing(1)
217 Facter.should be_timing
218 end
219 it "should have timing enabled using true" do
220 Facter.timing(true)
221 Facter.should be_timing
222 end
223 it "should have timing disabled using 0" do
224 Facter.timing(0)
225 Facter.should_not be_timing
226 end
227 it "should have timing disabled using false" do
228 Facter.timing(false)
229 Facter.should_not be_timing
230 end
231 end
232
206233 describe "when registering directories to search" do
207234 after { Facter.instance_variable_set("@search_path", []) }
208235