Class: Config

Inherits:
Object
  • Object
show all
Defined in:
lib/floatyhelper/config.rb

Constant Summary collapse

DEFAULT_CONF_FILE =
<<~EOT
.freeze
VALID_SETTINGS =
{
  'increaselife' => {
    'description' => 'Number of hours beyond the service default to increase the VM lifetime when adding a VM to floatyhelper.',
    'default' => 0,
  },
  'vertical_snapshot_status' => {
    'description' => 'When true, clear the screen and show the snapshot in progress status vertically. Otherwise, show status on a single line.',
    'default' => true,
  },
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#VALID_SETTINGSObject (readonly)

Returns the value of attribute VALID_SETTINGS.



26
27
28
# File 'lib/floatyhelper/config.rb', line 26

def VALID_SETTINGS
  @VALID_SETTINGS
end

Class Method Details

.fhfileObject



28
29
30
# File 'lib/floatyhelper/config.rb', line 28

def self.fhfile
  "#{Etc.getpwuid.dir}/.floatyhelper.yaml"
end

.get_config_setting(setting) ⇒ Object

NOTE: Anything currently set with ‘floatyhelper config set’ will return a string, regardless of its intended type. Need to add typing one of these days.



65
66
67
68
# File 'lib/floatyhelper/config.rb', line 65

def self.get_config_setting(setting)
  data = load_data
  data['config'][setting]
end

.load_dataObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/floatyhelper/config.rb', line 32

def self.load_data
  File.write(fhfile, DEFAULT_CONF_FILE) unless File.exist?(fhfile)
  data = YAML.load_file(fhfile)
  # If someone is using an older version, ensure the conf file has
  # all the correct top-level keys. Only write it back if we modify
  # the data to add a top-level key.
  writeback = false
  ['config', 'vms', 'snapshots'].each do |key|
    if !data.keys.include?(key)
      data[key] = {}
      writeback = true
    end
  end

  # Config setting defaults
  VALID_SETTINGS.each do |setting, info|
    writeback = true if data['config'][setting].nil?
    data['config'][setting] ||= info['default']
  end

  write_data(data) if writeback
  data
end

.set_config_setting(setting, value) ⇒ Object



70
71
72
73
74
# File 'lib/floatyhelper/config.rb', line 70

def self.set_config_setting(setting, value)
  data = load_data
  data['config'][setting] = value
  write_data(data)
end

.write_data(data) ⇒ Object

It is up to the calling function to ensure the data object being passed in is a properly formatted hash. It should only be used to modify after reading the current state with load_data.



59
60
61
# File 'lib/floatyhelper/config.rb', line 59

def self.write_data(data)
  File.write(fhfile, data.to_yaml)
end