Add gmconfig:process_plain_args()

This commit is contained in:
Ulf Wiger 2025-05-06 11:29:15 +02:00
parent d0a0d039c0
commit 32c1ed5c4b

View File

@ -31,6 +31,7 @@
-export([search_for_config_file/2]).
-export([apply_os_env/0,
apply_os_env/3]).
-export([process_plain_args/0]).
-export([data_dir/1]).
-export([check_config/1]).
@ -74,6 +75,7 @@
, config_file_basename => string() | 'undefined'
, config_file_os_env => string() | 'undefined'
, config_file_search_path => [string() | fun(() -> string())]
, config_plain_args => string() | 'undefined'
, system_suffix => string()
, config_formats => #{ extension() => decoder_fun() }
, schema => string() | map() | fun(() -> map())}.
@ -159,6 +161,7 @@ default_gmconfig_env() ->
, config_file_search_path => ["."]
, config_formats => #{ "json" => fun json_decode/1
, "eterm" => fun eterm_consult/1 }
, config_plain_args => undefined
, system_defaults_search_path => [fun setup:data_dir/0]
, system_suffix => "" }.
@ -508,7 +511,6 @@ load_config_file(File, Mode) when Mode =:= check;
do_load_user_config(File, store, Mode).
apply_os_env() ->
ok = application:ensure_started(gproc),
Pfx = os_env_prefix(),
ConfigMap = pt_get_config(),
case apply_os_env(Pfx, schema(), ConfigMap) of
@ -548,6 +550,35 @@ apply_os_env(Pfx, Schema, ConfigMap) ->
error(E)
end.
process_plain_args() ->
case gmconfig_env(config_plain_args, undefined) of
undefined ->
ok;
Str ->
PlainArgs = init:get_plain_arguments(),
Schema = schema(),
Map = process_plain_args_(PlainArgs, Str, schema(), #{}),
?LOG_INFO("Map from plain args: ~p", [Map]),
if map_size(Map) > 0 ->
update_config_(Map, pt_get_config(), Schema, report);
true ->
no_change
end
end.
process_plain_args_([Tag, K, V | Rest], Tag, Schema, Map) ->
Path = plain_path_arg(K),
Value = coerce_type(Path, V, Schema),
Map1 = update_map(to_map(Path, Value), Map),
process_plain_args_(Rest, Tag, Schema, Map1);
process_plain_args_([_ | T], Tag, Schema, Map) ->
process_plain_args_(T, Tag, Schema, Map);
process_plain_args_([], _, _, Map) ->
Map.
plain_path_arg(Str) ->
re:split(Str, <<"__">>, [{return, binary}]).
to_map(K, V) ->
to_map(K, V, #{}).