/
Facter.pm
200 lines (165 loc) · 3.97 KB
/
Facter.pm
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
#
# a Perl 6 port of ruby facter
#
# http://github.com/puppetlabs/facter/
#
#--
# 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
#
#--
use v6;
class Facter;
use Facter::Util::Fact;
use Facter::Util::Collection;
our $VERSION = '0.02';
our $LAST_OBJECT;
# TODO: RT#77906
#%*ENV<LANG> = 'C';
# Static variables (@@debug)
our $debug = 1;
our $timing = 0;
# Private members
has $!collection is rw;
has @!search_path is rw = ();
method collection {
$!collection //= Facter::Util::Collection.new
}
method version {
$VERSION
}
method BUILD {
$LAST_OBJECT = self;
}
multi method debugging {
return $debug != 0
}
# Set debugging on or off (1/0)
multi method debugging($bit) {
if $bit {
$debug = 1;
}
else {
$debug = 0;
}
}
method debug($string) {
if ! defined $string {
return
}
if self.debugging {
say $string ~ "\n"
}
return;
}
method show_time($string) {
if $string and self.timing {
say $string
}
return;
}
multi method timing {
return $timing != 0;
}
# Set timing on or off.
multi method timing($bit) {
if $bit {
$timing = 1;
} else {
$timing = 0;
}
}
# Return a fact object by name. If you use this, you still have to call
# 'value' on it to retrieve the actual value.
method get_fact($name) {
self.collection.fact($name);
}
method fact(*@args) {
self.collection.fact(@args);
}
method flush(*@args) {
self.collection.flush(@args);
}
method value (*@args) {
self.collection.value(@args);
}
#for 'fact', 'flush', 'value' -> $name {
# Facter.^add_method($name, method (*@args) {
# self.collection.$name.(@args);
# });
#}
#for 'list', 'to_hash' -> $name {
# Facter.^add_method($name, method (*@args) {
# self.collection.load_all();
# self.collection.$name.(@args);
# });
#}
method list (*@args) {
self.collection.load_all();
self.collection.list(@args);
}
method to_hash (*@args) {
self.collection.load_all();
self.collection.to_hash(@args);
}
# 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.
method add ($name, Sub $block) {
# TODO add %options support
#multi method add ($name, %options = (), $block) {
Facter.debug("Facter: adding fact $name as " ~ $block.perl);
my $instance = self // Facter.get_instance;
$instance.collection.add($name, $block);
}
method get_instance {
if self { return self }
$LAST_OBJECT //= Facter.new;
}
method each {
self.collection.load_all();
gather for self.collection -> $fact {
take $fact;
}
}
# Clear all facts. Mostly used for testing.
method clear {
self.flush;
self.reset;
return;
}
method warn ($msg) {
if self.debugging and $msg and $msg != "" {
$msg = [ $msg ] unless $msg.^can('each');
for $msg -> $line {
warn $line;
}
}
}
method reset {
$!collection = ();
}
# Load all of the default facts, and then everything from disk.
method loadfacts {
self.collection.load_all();
}
# Register a directory to search through.
method search(@dirs) {
@!search_path.push(@dirs);
}
# Return our registered search directories.
method search_path {
return @!search_path;
}