Class: InspecPlugins::Compliance::Configuration
- Inherits:
-
Object
- Object
- InspecPlugins::Compliance::Configuration
- Defined in:
- lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb
Overview
stores configuration on local filesystem
Instance Method Summary collapse
-
#[](key) ⇒ Object
direct access to config.
- #[]=(key, value) ⇒ Object
- #clean ⇒ Object
-
#destroy ⇒ Object
deletes data.
-
#get ⇒ Object
return the json data.
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
- #key?(key) ⇒ Boolean
-
#legacy_check!(feature) ⇒ Object
exit 1 if the version of compliance that we’re working with doesn’t support odic.
-
#store ⇒ Object
stores a hash to json.
-
#supported?(feature) ⇒ Boolean
return if the (stored) api version does not support a certain feature.
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 7 def initialize @config_path = File.join(Inspec.config_dir, "compliance") # ensure the directory is available unless File.directory?(@config_path) FileUtils.mkdir_p(@config_path) end # set config file path @config_file = File.join(@config_path, "/config.json") @config = {} # load the data get end |
Instance Method Details
#[](key) ⇒ Object
direct access to config
22 23 24 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 22 def [](key) @config[key] end |
#[]=(key, value) ⇒ Object
26 27 28 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 26 def []=(key, value) @config[key] = value end |
#clean ⇒ Object
34 35 36 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 34 def clean @config = {} end |
#destroy ⇒ Object
deletes data
56 57 58 59 60 61 62 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 56 def destroy if File.exist?(@config_file) File.delete(@config_file) else true end end |
#get ⇒ Object
return the json data
39 40 41 42 43 44 45 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 39 def get if File.exist?(@config_file) file = File.read(@config_file) @config = JSON.parse(file) end @config end |
#key?(key) ⇒ Boolean
30 31 32 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 30 def key?(key) @config.key?(key) end |
#legacy_check!(feature) ⇒ Object
exit 1 if the version of compliance that we’re working with doesn’t support odic
80 81 82 83 84 85 86 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 80 def legacy_check!(feature) return if supported?(feature) puts "This feature (#{feature}) is not available for legacy installations." puts "Please upgrade to a recent version of Chef Compliance." exit 1 end |
#store ⇒ Object
stores a hash to json
48 49 50 51 52 53 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 48 def store File.open(@config_file, "w") do |f| f.chmod(0600) f.write(@config.to_json) end end |
#supported?(feature) ⇒ Boolean
return if the (stored) api version does not support a certain feature
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/plugins/inspec-compliance/lib/inspec-compliance/configuration.rb', line 65 def supported?(feature) sup = version_with_support(feature) # we do not know the version, therefore we do not know if its possible to use the feature return if self["version"].nil? || self["version"]["version"].nil? if sup.is_a?(Array) Gem::Version.new(self["version"]["version"]) >= sup[0] && Gem::Version.new(self["version"]["version"]) < sup[1] else Gem::Version.new(self["version"]["version"]) >= sup end end |