-module(gmhc_config). -vsn("0.4.8"). -export([ load_config/0 , get_config/1 ]). -include_lib("kernel/include/logger.hrl"). load_config() -> instrument_gmconfig(), ?LOG_DEBUG("Schema: ~p", [gmconfig:schema()]), gmconfig:load_user_config(report), gmconfig:apply_os_env(), gmconfig:process_plain_args(), check_application_env(), ok. instrument_gmconfig() -> gmconfig:set_gmconfig_env(gmconfig_env()). -spec gmconfig_env() -> gmconfig:gmconfig(). gmconfig_env() -> #{ os_env_prefix => "GMHC" , config_file_basename => "gmhive_client_config" , config_file_os_env => "GMHIVE_CLIENT_CONFIG" , config_file_search_path => [".", fun setup:home/0, fun setup:data_dir/0 ] , config_plain_args => "-gmhc" , system_suffix => "" , schema => fun gmhc_config_schema:to_json/0 }. get_config(Path) -> gmconfig:get_config(Path, cpath(Path)). check_application_env() -> case lists:foldl(fun appl_env/2, #{}, env_keys()) of C when map_size(C) == 0 -> ok; C -> gmconfig:update_config(C) end. appl_env(K, C) -> case application:get_env(gmhive_client, K) of {ok, V} -> appl_env(K, V, C); undefined -> C end. appl_env(bin_dir_hook , H , C) -> set_bin_dir_hook(H), C; appl_env(pubkey , K , C) -> C#{<<"pubkey">> => K}; appl_env(extra_pubkeys , Ks, C) -> C#{<<"extra_pubkeys">> => Ks}; appl_env(pool_admin_url, U , C) -> merge(C, #{<<"pool_admin">> => #{<<"url">> => U}}); appl_env(workers , Ws, C) -> C#{<<"workers">> => Ws}. set_bin_dir_hook({Mod, F}) when is_atom(Mod), is_atom(F) -> application:set_env(gmhive_worker, bin_dir_hook, {Mod, F}). env_keys() -> [pubkey, extra_pubkeys, pool_admin_url, workers, network, bin_dir_hook]. cpath([<<"pubkey">>]) -> [user_config ]; cpath([<<"extra_pubkeys">>]) -> [user_config, schema_default]; cpath([<<"pool_admin">>, <<"url">>]) -> [user_config, schema_default]; cpath([<<"workers">>]) -> [user_config, schema_default]; cpath(_) -> [user_config, schema_default]. merge(A, B) -> gmconfig:merge_config_maps(A, B).