Class: Inspec::Metadata
- Inherits:
-
Object
show all
- Defined in:
- lib/inspec/metadata.rb
Overview
Extract metadata.rb information
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.finalize(metadata, profile_id) ⇒ Object
-
.from_file(path, profile_id, logger = nil) ⇒ Object
-
.from_ref(ref, contents, profile_id, logger = nil) ⇒ Object
-
.from_ruby(ref, contents, profile_id, logger = nil) ⇒ Object
-
.from_yaml(ref, contents, profile_id, logger = nil) ⇒ Object
-
.symbolize_keys(hash) ⇒ Object
Instance Method Summary
collapse
Constructor Details
#initialize(ref, logger = nil) ⇒ Metadata
Returns a new instance of Metadata.
13
14
15
16
17
18
|
# File 'lib/inspec/metadata.rb', line 13
def initialize(ref, logger = nil)
@ref = ref
@logger = logger || Logger.new(nil)
@params = {}
@missing_methods = []
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sth, *args) ⇒ Object
126
127
128
129
|
# File 'lib/inspec/metadata.rb', line 126
def method_missing(sth, *args)
@logger.warn "#{ref} doesn't support: #{sth} #{args}"
@missing_methods.push(sth)
end
|
Instance Attribute Details
#params ⇒ Object
Returns the value of attribute params.
12
13
14
|
# File 'lib/inspec/metadata.rb', line 12
def params
@params
end
|
#ref ⇒ Object
rubocop:disable Metrics/ClassLength
11
12
13
|
# File 'lib/inspec/metadata.rb', line 11
def ref
@ref
end
|
Class Method Details
.finalize(metadata, profile_id) ⇒ Object
142
143
144
145
146
147
148
149
|
# File 'lib/inspec/metadata.rb', line 142
def self.finalize(metadata, profile_id)
return nil if metadata.nil?
param = metadata.params || {}
param['name'] = profile_id.to_s unless profile_id.to_s.empty?
param['version'] = param['version'].to_s unless param['version'].nil?
metadata.params = symbolize_keys(param)
metadata
end
|
.from_file(path, profile_id, logger = nil) ⇒ Object
178
179
180
181
182
183
184
185
186
|
# File 'lib/inspec/metadata.rb', line 178
def self.from_file(path, profile_id, logger = nil)
unless File.file?(path)
logger ||= Logger.new(nil)
logger.error "Can't find metadata file #{path}"
return nil
end
from_ref(File.basename(path), File.read(path), profile_id, logger)
end
|
.from_ref(ref, contents, profile_id, logger = nil) ⇒ Object
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/inspec/metadata.rb', line 163
def self.from_ref(ref, contents, profile_id, logger = nil)
case File.basename(ref)
when 'inspec.yml'
from_yaml(ref, contents, profile_id, logger)
when 'metadata.rb'
from_ruby(ref, contents, profile_id, logger)
else
logger ||= Logger.new(nil)
logger.error "Don't know how to handle metadata in #{ref}"
nil
end
end
|
.from_ruby(ref, contents, profile_id, logger = nil) ⇒ Object
157
158
159
160
161
|
# File 'lib/inspec/metadata.rb', line 157
def self.from_ruby(ref, contents, profile_id, logger = nil)
res = Metadata.new(ref, logger)
res.instance_eval(contents, ref, 1)
finalize(res, profile_id)
end
|
.from_yaml(ref, contents, profile_id, logger = nil) ⇒ Object
151
152
153
154
155
|
# File 'lib/inspec/metadata.rb', line 151
def self.from_yaml(ref, contents, profile_id, logger = nil)
res = Metadata.new(ref, logger)
res.params = YAML.load(contents)
finalize(res, profile_id)
end
|
.symbolize_keys(hash) ⇒ Object
135
136
137
138
139
140
|
# File 'lib/inspec/metadata.rb', line 135
def self.symbolize_keys(hash)
hash.each_with_object({}) {|(k, v), h|
v = symbolize_keys(v) if v.is_a?(Hash)
h[k.to_sym] = v
}
end
|
Instance Method Details
#is_supported(os, entry) ⇒ Object
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
|
# File 'lib/inspec/metadata.rb', line 43
def is_supported(os, entry)
name, family, release = support_fields(entry)
name_ok = name.nil? ||
os[:name] == name || os[:family] == name
family_check = family.to_s + '?'
family_ok = family.nil? || os[:family] == family ||
(
os.respond_to?(family_check) &&
os.method(family_check).call
)
release_ok = release.nil? || os[:release] == release
name_ok && family_ok && release_ok
end
|
#support_fields(entry) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/inspec/metadata.rb', line 69
def support_fields(entry)
if entry.is_a?(Hash)
try_support = self.class.symbolize_keys(entry)
name = try_support[:'os-name'] || try_support[:os]
family = try_support[:'os-family']
release = try_support[:release]
elsif entry.is_a?(String)
@logger.warn(
"Do not use deprecated `supports: #{entry}` syntax. Instead use "\
"`supports: {os-family: #{entry}}`.")
family = entry
end
[name, family, release]
end
|
#supports(sth, version = nil) ⇒ Object
37
38
39
40
41
|
# File 'lib/inspec/metadata.rb', line 37
def supports(sth, version = nil)
end
|
#supports_transport?(backend) ⇒ Boolean
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/inspec/metadata.rb', line 85
def supports_transport?(backend)
supp = params[:supports]
supp = supp.is_a?(Hash) ? [supp] : Array(supp)
return true if supp.empty?
found = supp.find do |entry|
is_supported(backend.os, entry)
end
!found.nil?
end
|
#unsupported ⇒ Object
131
132
133
|
# File 'lib/inspec/metadata.rb', line 131
def unsupported
@missing_methods
end
|
#valid ⇒ Object
return all warn and errors
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/inspec/metadata.rb', line 104
def valid
errors = []
warnings = []
%w{ name version }.each do |field|
next unless params[field.to_sym].nil?
errors.push("Missing profile #{field} in #{ref}")
end
%w{ title summary maintainer copyright }.each do |field|
next unless params[field.to_sym].nil?
warnings.push("Missing profile #{field} in #{ref}")
end
[errors, warnings]
end
|
#valid? ⇒ Boolean
121
122
123
124
|
# File 'lib/inspec/metadata.rb', line 121
def valid?
errors, _warnings = valid
errors.empty? && unsupported.empty?
end
|