Skip to content

Overwrite configuration with development configuration

schut edited this page Jan 11, 2013 · 2 revisions

Problem

I want to overwrite some settings in boss.config with development settings. I'm used to at least environments a development and a production. But in addition an system integration and an acceptance environment are common in my environments as well.

Solution

First look for 'boss_env' in the operating system environment, if not found fallback to the boss_env environment. Use the result read 'boss_{environment}.config' and overwrite boss.config settings with the settings found in this file.

Warning: I'm not sure which boss configuration environment parameters can be overwritten. I use it to overwrite my application specific environment parameters.

Code

-module(app_00_config).
-export([init/0]).
-define(CONFIG_BASENAME, "boss").

init() ->
	ConfigFile = ?CONFIG_BASENAME ++ "_" ++ env() ++ ".config",
	case file:consult(ConfigFile) of
		{ok, [Config]}  -> config(Config);
		{error, enoent} -> ok			% no configuration overrides present
	end.

env() ->
	case os:getenv("boss_env") of
		false -> atom_to_list(boss_env:boss_env());
		Env   -> Env
	end.

config(Config) ->
	true = lists:all(fun app_config/1, Config),
	ok.

app_config({App, Config}) ->
	lists:all(fun({Par, Val}) -> app_config_set_env(App, Par, Val) end, Config).

app_config_set_env(App, Par, Val) ->
	application:set_env(App, Par, Val),
	true.