Module: Bandshell::ConfigStore

Defined in:
lib/bandshell/config_store.rb

Constant Summary collapse

@@path =
nil

Class Method Summary collapse

Class Method Details

.config_exists?(name) ⇒ Boolean

check if a config exists

Returns:

  • (Boolean)


29
30
31
32
33
34
# File 'lib/bandshell/config_store.rb', line 29

def self.config_exists?(name)
  initialize_path if not @@path
  file = File.join(@@path, name)
  rofile = File.join(@@ropath, name)
  File.exist?(file) || File.exist?(rofile)
end

.delete_config(name) ⇒ Object

Delete a config from the filesystem



47
48
49
50
51
# File 'lib/bandshell/config_store.rb', line 47

def self.delete_config(name)
  initialize_path if not @@path
  file = File.join(@@path, name)
  FileUtils.rm(file)
end

.initialize_pathObject



53
54
55
56
57
58
59
60
61
# File 'lib/bandshell/config_store.rb', line 53

def self.initialize_path
  @@ropath = File.join(LiveImage.mountpoint, 'concerto', 'config')
  if LiveImage.readonly?
    @@path = '/tmp/concerto/config'
  else
    @@path = @@ropath
  end
  FileUtils.mkdir_p @@path
end

.read_config(name, default = '') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/bandshell/config_store.rb', line 10

def self.read_config(name, default='')
  initialize_path if not @@path
  file = File.join(@@path, name)
  rofile = File.join(@@ropath, name)

  # Check the read/write config location first. If nothing there,
  # check the read-only location. If nothing is there, return default.
  # This way writes can be made at runtime on read-only media while
  # still allowing some settings to be "baked into" the media.
  if File.exist?(file)
    IO.read(file)
  elsif File.exist?(rofile)
    IO.read(rofile)
  else
    default
  end
end

.write_config(name, value) ⇒ Object

Write a config to the read/write configuration location.



37
38
39
40
41
42
43
44
# File 'lib/bandshell/config_store.rb', line 37

def self.write_config(name, value)
  initialize_path if not @@path
  file = File.join(@@path, name)

  File.open(file, 'w') do |f|
    f.write value
  end
end