class JiraMule::Config

Constants

CFG_ALTRC_NAME
CFG_DIR_NAME
CFG_FILE_NAME
CFG_SCOPES
CFG_SYS_NAME
ConfigFile

internal transient this-run-only things (also -c options) specified from –configfile env from ENV project .jiramulerc at project dir user .jiramulerc at $HOME system .jiramulerc at /etc defaults Internal hardcoded defaults

Attributes

paths[R]
projectDir[R]

Public Class Methods

new() click to toggle source
# File lib/jiraMule/Config.rb, line 43
def initialize
  @paths = []
  @paths << ConfigFile.new(:internal, nil, IniFile.new())
  # :specified --configfile FILE goes here. (see load_specific)
  unless ENV['JM_CONFIGFILE'].nil? then
    # if it exists, must be a file
    # if it doesn't exist, that's ok
    ep = Pathname.new(ENV['JM_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)
  end
  @paths << ConfigFile.new(:user, Pathname.new(Dir.home) + CFG_FILE_NAME)
  fixModes(Pathname.new(Dir.home) + CFG_DIR_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)

end

Public Instance Methods

[](key) click to toggle source

key is <section>.<key>

# File lib/jiraMule/Config.rb, line 211
def [](key)
  get(key)
end
[]=(key, value) click to toggle source

For setting internal, this-run-only values

# File lib/jiraMule/Config.rb, line 216
def []=(key, value)
  set(key, value, :internal)
end
dump() click to toggle source

Dump out a combined config

# File lib/jiraMule/Config.rb, line 181
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) click to toggle source
# File lib/jiraMule/Config.rb, line 127
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
fixModes(path) click to toggle source
# File lib/jiraMule/Config.rb, line 119
def fixModes(path)
  if path.directory? then
    path.chmod(0700)
  elsif path.file? then
    path.chmod(0600)
  end
end
get(key, scope=CFG_SCOPES) click to toggle source
Get a value for key, looking at the specificed scopes

key is <section>.<key>

# File lib/jiraMule/Config.rb, line 163
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
load() click to toggle source

Load all of the potential config files

# File lib/jiraMule/Config.rb, line 148
def load()
  # - read/write config file in [Project, User, System] (all are optional)
  @paths.each { |cfg| cfg.load }
end
load_specific(file) click to toggle source
Load specified file into the config stack

This can be called multiple times and each will get loaded into the config

# File lib/jiraMule/Config.rb, line 155
def load_specific(file)
  spc = ConfigFile.new(:specified, Pathname.new(file))
  spc.load
  @paths.insert(1, spc)
end
set(key, value, scope=:project) click to toggle source
# File lib/jiraMule/Config.rb, line 190
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

Private Instance Methods

findProjectDir() click to toggle source
Find the root of this project Directory.

The Project dir is the directory between PWD and HOME that has one of (in order of preference):

  • .jiramulerc

  • .jiramule/config

  • .jiramule/

  • .git/

# File lib/jiraMule/Config.rb, line 80
def findProjectDir()
  result=nil
  fileNames=[CFG_FILE_NAME, CFG_ALTRC_NAME]
  dirNames=[CFG_DIR_NAME]
  home = Pathname.new(Dir.home)
  pwd = Pathname.new(Dir.pwd)
  return nil if home == pwd
  pwd.dirname.ascend do |i|
    break unless result.nil?
    break if i == home
    fileNames.each do |f|
      if (i + f).exist? then
        result = i
      end
    end
    dirNames.each do |f|
      if (i + f).directory? then
        result = i
      end
    end
  end

  # If nothing found, do a last ditch try by looking for .git/
  if result.nil? then
    pwd.dirname.ascend do |i|
      break unless result.nil?
      break if i == home
      if (i + '.git').directory? then
        result = i
      end
    end
  end

  # Now if nothing found, assume it will live in pwd.
  result = Pathname.new(Dir.pwd) if result.nil?
  return result
end