Commit 8fc4872e authored by Peter van 't Hof's avatar Peter van 't Hof
Browse files

Switch defaults from BIOPET_CONFIG env to a defaults var

parent d4b8ed2c
...@@ -27,7 +27,8 @@ import scala.reflect.io.Directory ...@@ -27,7 +27,8 @@ import scala.reflect.io.Directory
* @param map Map with value for new config * @param map Map with value for new config
* @constructor Load config with existing map * @constructor Load config with existing map
*/ */
class Config(var map: Map[String, Any]) extends Logging { class Config(var map: Map[String, Any],
protected[core] var defaults: Map[String, Any] = Map()) extends Logging {
logger.debug("Init phase of config") logger.debug("Init phase of config")
/** /**
...@@ -42,14 +43,14 @@ class Config(var map: Map[String, Any]) extends Logging { ...@@ -42,14 +43,14 @@ class Config(var map: Map[String, Any]) extends Logging {
* Loading a environmental variable as location of config files to merge into the config * Loading a environmental variable as location of config files to merge into the config
* @param valueName Name of value * @param valueName Name of value
*/ */
def loadConfigEnv(valueName: String) { def loadConfigEnv(valueName: String, default: Boolean) {
sys.env.get(valueName) match { sys.env.get(valueName) match {
case Some(globalFiles) => { case Some(globalFiles) => {
for (globalFile <- globalFiles.split(":")) { for (globalFile <- globalFiles.split(":")) {
val file: File = new File(globalFile) val file: File = new File(globalFile)
if (file.exists) { if (file.exists) {
logger.info("Loading config file: " + file) logger.info("Loading config file: " + file)
loadConfigFile(file) loadConfigFile(file, default)
} else logger.warn(valueName + " value found but file '" + file + "' does not exist, no global config is loaded") } else logger.warn(valueName + " value found but file '" + file + "' does not exist, no global config is loaded")
} }
} }
...@@ -61,20 +62,25 @@ class Config(var map: Map[String, Any]) extends Logging { ...@@ -61,20 +62,25 @@ class Config(var map: Map[String, Any]) extends Logging {
* Loading default value for biopet * Loading default value for biopet
*/ */
def loadDefaultConfig() { def loadDefaultConfig() {
loadConfigEnv("BIOPET_CONFIG") loadConfigEnv("BIOPET_CONFIG", true)
} }
/** /**
* Merge a json file into the config * Merge a json file into the config
* @param configFile Location of file * @param configFile Location of file
*/ */
def loadConfigFile(configFile: File) { def loadConfigFile(configFile: File, default: Boolean = false) {
val configMap = fileToConfigMap(configFile) val configMap = fileToConfigMap(configFile)
if (default) {
if (defaults.isEmpty) defaults = configMap
else defaults = mergeMaps(configMap, defaults)
logger.debug("New defaults: " + defaults)
} else {
if (map.isEmpty) map = configMap if (map.isEmpty) map = configMap
else map = mergeMaps(configMap, map) else map = mergeMaps(configMap, map)
logger.debug("New config: " + map) logger.debug("New config: " + map)
} }
}
protected[config] var notFoundCache: List[ConfigValueIndex] = List() protected[config] var notFoundCache: List[ConfigValueIndex] = List()
protected[config] var foundCache: Map[ConfigValueIndex, ConfigValue] = Map() protected[config] var foundCache: Map[ConfigValueIndex, ConfigValue] = Map()
......
...@@ -39,7 +39,7 @@ trait Configurable extends ImplicitConversions { ...@@ -39,7 +39,7 @@ trait Configurable extends ImplicitConversions {
/** Map to store defaults for config */ /** Map to store defaults for config */
def defaults: Map[String, Any] = { def defaults: Map[String, Any] = {
if (root != null) root.defaults if (root != null) root.defaults
else Map() else globalConfig.defaults
} }
val config = new ConfigFunctions val config = new ConfigFunctions
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment