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 user defaults}.map{|i| i.to_sym}.freeze
CFG_FILE_NAME =
'.mrmuranorc'.freeze
CFG_DIR_NAME =
'.mrmurano'.freeze
CFG_ALTRC_NAME =
'.mrmurano/config'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



41
42
43
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/MrMurano/Config.rb', line 41

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
    @paths << ConfigFile.new(:project, @projectDir + CFG_FILE_NAME)
    fixModes(@projectDir + CFG_DIR_NAME)
  else
    @paths << ConfigFile.new(:project, Pathname.new(Dir.home) + CFG_FILE_NAME)
  end
  @paths << ConfigFile.new(:user, Pathname.new(Dir.home) + CFG_FILE_NAME)
  fixModes(Pathname.new(Dir.home) + CFG_DIR_NAME)
  @paths << ConfigFile.new(:defaults, nil, IniFile.new())


  set('tool.verbose', false, :defaults)
  set('tool.debug', false, :defaults)
  set('tool.dry', false, :defaults)
  set('tool.fullerror', false, :defaults)
  set('tool.outformat', 'best', :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('location.cors', 'cors.yaml', :defaults)
  set('location.specs', 'specs', :defaults)

  set('sync.bydefault', SyncRoot.bydefault.join(' '), :defaults) if defined? SyncRoot

  set('files.default_page', 'index.html', :defaults)
  set('files.searchFor', '**/*', :defaults)
  set('files.ignoring', '', :defaults)

  set('endpoints.searchFor', '*.lua */*.lua', :defaults)
  set('endpoints.ignoring', '*_test.lua *_spec.lua .*', :defaults)

  set('eventhandler.searchFor', '*.lua */*.lua', :defaults)
  set('eventhandler.ignoring', '*_test.lua *_spec.lua .*', :defaults)
  set('eventhandler.skiplist', 'websocket webservice device.service_call', :defaults)

  set('modules.searchFor', '*.lua */*.lua', :defaults)
  set('modules.ignoring', '*_test.lua *_spec.lua .*', :defaults)

  set('product.spec', 'resources.yaml', :defaults)

  if Gem.win_platform? then
    set('diff.cmd', 'fc', :defaults)
  else
    set('diff.cmd', 'diff -u', :defaults)
  end
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



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

def paths
  @paths
end

#projectDirObject (readonly)

Returns the value of attribute projectDir.



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

def projectDir
  @projectDir
end

Instance Method Details

#[](key) ⇒ Object

key is <section>.<key>



235
236
237
# File 'lib/MrMurano/Config.rb', line 235

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object

For setting internal, this-run-only values



240
241
242
# File 'lib/MrMurano/Config.rb', line 240

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

#dumpObject

Dump out a combined config



205
206
207
208
209
210
211
212
# File 'lib/MrMurano/Config.rb', line 205

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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/MrMurano/Config.rb', line 153

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 :defaults
    root = nil
  end
  return nil if root.nil?
  root.mkpath
  root + name
end

#fixModes(path) ⇒ Object



145
146
147
148
149
150
151
# File 'lib/MrMurano/Config.rb', line 145

def fixModes(path)
  if path.directory? then
    path.chmod(0700)
  elsif path.file? then
    path.chmod(0600)
  end
end

#get(key, scope = CFG_SCOPES) ⇒ Object

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/MrMurano/Config.rb', line 187

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



172
173
174
175
# File 'lib/MrMurano/Config.rb', line 172

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



179
180
181
182
183
# File 'lib/MrMurano/Config.rb', line 179

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

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



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/MrMurano/Config.rb', line 214

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