Class: MrMurano::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/MrMurano/Config.rb

Defined Under Namespace

Classes: ConfigFile

Constant Summary collapse

CFG_SCOPES =
%w{internal specified env project private user system defaults}.map{|i| i.to_sym}.freeze
CFG_FILE_NAME =
'.mrmuranorc'.freeze
CFG_PRVT_NAME =

Going away.

'.mrmuranorc.private'.freeze
CFG_DIR_NAME =
'.mrmurano'.freeze
CFG_ALTRC_NAME =
'.mrmurano/config'.freeze
CFG_SYS_NAME =
'/etc/mrmuranorc'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/MrMurano/Config.rb', line 44

def initialize
  @paths = []
  @paths << ConfigFile.new(:internal, nil, IniFile.new())
  # :specified --configfile FILE goes here. (see load_specific)
  unless ENV['MR_CONFIGFILE'].nil? then
    # if it exists, must be a file
    # if it doesn't exist, that's ok
    ep = Pathname.new(ENV['MR_CONFIGFILE'])
    if ep.file? or not ep.exist? then
      @paths << ConfigFile.new(:env, ep)
    end
  end
  @projectDir = findProjectDir()
  unless @projectDir.nil? then
    if (@projectDir + CFG_PRVT_NAME).exist? then
      say_warning "!!! Using .mrmuranorc.private is deprecated"
    end
    @paths << ConfigFile.new(:private, @projectDir + CFG_PRVT_NAME)
    @paths << ConfigFile.new(:project, @projectDir + CFG_FILE_NAME)
  end
  @paths << ConfigFile.new(:user, Pathname.new(Dir.home) + CFG_FILE_NAME)
  @paths << ConfigFile.new(:system, Pathname.new(CFG_SYS_NAME))
  @paths << ConfigFile.new(:defaults, nil, IniFile.new())


  set('tool.verbose', false, :defaults)
  set('tool.debug', false, :defaults)
  set('tool.dry', false, :defaults)

  set('net.host', 'bizapi.hosted.exosite.io', :defaults)

  set('location.base', @projectDir, :defaults) unless @projectDir.nil?
  set('location.files', 'files', :defaults)
  set('location.endpoints', 'endpoints', :defaults)
  set('location.modules', 'modules', :defaults)
  set('location.eventhandlers', 'eventhandlers', :defaults)
  set('location.roles', 'roles.yaml', :defaults)
  set('location.users', 'users.yaml', :defaults)

  set('files.default_page', 'index.html', :defaults)

  set('eventhandler.skiplist', 'websocket webservice device.service_call', :defaults)

  set('diff.cmd', 'diff -u', :defaults)
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



34
35
36
# File 'lib/MrMurano/Config.rb', line 34

def paths
  @paths
end

#projectDirObject (readonly)

Returns the value of attribute projectDir.



35
36
37
# File 'lib/MrMurano/Config.rb', line 35

def projectDir
  @projectDir
end

Instance Method Details

#[](key) ⇒ Object

key is <section>.<key>



222
223
224
# File 'lib/MrMurano/Config.rb', line 222

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object

For setting internal, this-run-only values



227
228
229
# File 'lib/MrMurano/Config.rb', line 227

def []=(key, value)
  set(key, value, :internal)
end

#dumpObject

Dump out a combined config



192
193
194
195
196
197
198
199
# File 'lib/MrMurano/Config.rb', line 192

def dump()
  # have a fake, merge all into it, then dump it.
  base = IniFile.new()
  @paths.reverse.each do |ini|
    base.merge! ini.data
  end
  base.to_s
end

#file_at(name, scope = :project) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/MrMurano/Config.rb', line 138

def file_at(name, scope=:project)
  case scope
  when :internal
    root = nil
  when :specified
    root = nil
  when :project
    root = @projectDir + CFG_DIR_NAME
  when :user
    root = Pathname.new(Dir.home) + CFG_DIR_NAME
  when :system
    root = nil
  when :defaults
    root = nil
  end
  return nil if root.nil?
  root.mkpath
  root + name
end

#get(key, scope = CFG_SCOPES) ⇒ Object

Get a value for key, looking at the specificed scopes key is <section>.<key>



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/MrMurano/Config.rb', line 174

def get(key, scope=CFG_SCOPES)
  scope = [scope] unless scope.kind_of? Array
  paths = @paths.select{|p| scope.include? p.kind}

  section, ikey = key.split('.')
  paths.each do |path|
    if path.data.has_section?(section) then
      sec = path.data[section]
      return sec if ikey.nil?
      if sec.has_key?(ikey) then
        return sec[ikey]
      end
    end
  end
  return nil
end

#loadObject

Load all of the potential config files



159
160
161
162
# File 'lib/MrMurano/Config.rb', line 159

def load()
  # - read/write config file in [Project, User, System] (all are optional)
  @paths.each { |cfg| cfg.load }
end

#load_specific(file) ⇒ Object

Load specified file into the config stack This can be called multiple times and each will get loaded into the config



166
167
168
169
170
# File 'lib/MrMurano/Config.rb', line 166

def load_specific(file)
  spc = ConfigFile.new(:specified, Pathname.new(file))
  spc.load
  @paths.insert(1, spc)
end

#set(key, value, scope = :project) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/MrMurano/Config.rb', line 201

def set(key, value, scope=:project)
  section, ikey = key.split('.', 2)
  raise "Invalid key" if section.nil?
  if not section.nil? and ikey.nil? then
    # If key isn't dotted, then assume the tool section.
    ikey = section
    section = 'tool'
  end

  paths = @paths.select{|p| scope == p.kind}
  raise "Unknown scope" if paths.empty?
  cfg = paths.first
  data = cfg.data
  tomod = data[section]
  tomod[ikey] = value unless value.nil?
  tomod.delete(ikey) if value.nil?
  data[section] = tomod
  cfg.write
end