Class: Compliance::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/bundles/inspec-compliance/configuration.rb

Overview

stores configuration on local filesystem

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/bundles/inspec-compliance/configuration.rb', line 8

def initialize
  @config_path = File.join(Dir.home, '.inspec', '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



23
24
25
# File 'lib/bundles/inspec-compliance/configuration.rb', line 23

def [](key)
  @config[key]
end

#[]=(key, value) ⇒ Object



27
28
29
# File 'lib/bundles/inspec-compliance/configuration.rb', line 27

def []=(key, value)
  @config[key] = value
end

#destroyObject

deletes data



49
50
51
# File 'lib/bundles/inspec-compliance/configuration.rb', line 49

def destroy
  File.delete(@config_file)
end

#getObject

return the json data



32
33
34
35
36
37
38
# File 'lib/bundles/inspec-compliance/configuration.rb', line 32

def get
  if File.exist?(@config_file)
    file = File.read(@config_file)
    @config = JSON.parse(file)
  end
  @config
end

#storeObject

stores a hash to json



41
42
43
44
45
46
# File 'lib/bundles/inspec-compliance/configuration.rb', line 41

def store
  File.open(@config_file, 'w') do |f|
    f.chmod(0600)
    f.write(@config.to_json)
  end
end