diff --git a/src/gmconfig.erl b/src/gmconfig.erl index 411b3d7..25410b1 100644 --- a/src/gmconfig.erl +++ b/src/gmconfig.erl @@ -60,8 +60,9 @@ -include_lib("kernel/include/logger.hrl"). -type gmconfig() :: #{ os_env_prefix => string() - , config_file_basename => string - , config_file_search_path => [string()] + , config_file_basename => string() | 'undefined' + , config_file_os_env => string() | 'undefined' + , config_file_search_path => [string() | fun(() -> string())] , system_suffix => string() , schema => string() | map() | fun(() -> map())}. @@ -124,10 +125,30 @@ gmconfig_env(Key) -> gmconfig_env(Key, Default) -> maps:get(Key, gmconfig_env(), Default). +search_path() -> search_path(config_file). + +search_path(Kind) -> + Key = case Kind of + config_file -> config_file_search_path; + system_defaults -> system_defaults_search_path + end, + case gmconfig_env(Key, undefined) of + undefined -> []; + Path0 when is_list(Path0) -> + lists:map( + fun(F) when is_function(F, 0) -> + F(); + (D) when is_list(D) -> + D + end, Path0) + end. + default_gmconfig() -> #{ os_env_prefix => "GM" , config_file_basename => "gmconfig" + , config_file_os_env => undefined , config_file_search_path => ["."] + , system_defaults_search_path => [fun setup:data_dir/0] , system_suffix => "" }. %% This function is similar to application:get_env/2, except @@ -606,22 +627,25 @@ command_line_config_file() -> end. default_config_file() -> - case os:getenv("GAJUMARU_CONFIG") of - false -> - case setup:get_env(aecore, config) of - {ok, F} -> F; - _ -> search_default_config() - end; - F -> - F + case gmconfig_env(config_file_os_env, undefined) of + undefined -> + search_default_config(); + E -> + case os:getenv(E) of + false -> search_default_config(); + F -> F + end end. search_default_config() -> - Dirs = [filename:join([os:getenv("HOME"), ".gajumaru", "gajumaru"]), - setup:home()], - SystemSuffix = get_system_suffix(), - Fname = "gajumaru" ++ SystemSuffix ++ ".{json,yaml}", - search_for_config_file(Dirs, Fname). + case gmconfig_env(config_file_basename, undefined) of + undefined -> undefined; + Basename -> + Dirs = search_path(), + SystemSuffix = get_system_suffix(), + Fname = Basename ++ SystemSuffix ++ ".{json,yaml}", + search_for_config_file(Dirs, Fname) + end. search_for_config_file(Dirs, FileWildcard) -> lists:foldl( @@ -1006,14 +1030,20 @@ load_system_defaults() -> load_system_defaults(report). load_system_defaults(Mode) -> - SystemSuffix = get_system_suffix(), - Fname = "gajumaru_defaults" ++ SystemSuffix ++ ".{yaml,json}", - case search_for_config_file([setup:data_dir()], Fname) of + case gmconfig_env(config_file_basename, undefined) of undefined -> ok; - ConfigFile -> - ?LOG_DEBUG("Loading system defaults from ~s", [ConfigFile]), - load_config_file(ConfigFile, Mode) + Basename -> + SystemSuffix = get_system_suffix(), + Fname = Basename ++ "_defaults" ++ SystemSuffix ++ ".{yaml,json}", + Path = search_path(system_defaults), + case search_for_config_file(Path, Fname) of + undefined -> + ok; + ConfigFile -> + ?LOG_DEBUG("Loading system defaults from ~s", [ConfigFile]), + load_config_file(ConfigFile, Mode) + end end. get_system_suffix() ->