This commit is contained in:
anlicheng 2025-05-05 23:20:27 +08:00
parent 469725d502
commit 7214a9d160

View File

@ -11,7 +11,7 @@
-record(manifest, {
work_dir = "" :: string(),
service_id = <<"">> :: binary(),
id = <<"">> :: binary(),
exec = <<"">>:: binary(),
args = [],
health_check = <<"">>
@ -29,8 +29,8 @@ new(WorkDir0) when is_binary(WorkDir0) ->
WorkDir = binary_to_list(WorkDir0),
case file:read_file(WorkDir ++ "manifest.json") of
{ok, ManifestInfo} ->
Manifest = catch jiffy:decode(ManifestInfo, [return_maps]),
case check_manifest(Manifest) of
Settings = catch jiffy:decode(ManifestInfo, [return_maps]),
case check_manifest(Settings) of
{ok, Manifest} ->
{ok, Manifest#manifest{work_dir = WorkDir}};
{error, Reason} ->
@ -54,21 +54,21 @@ startup(#manifest{work_dir = WorkDir, exec = ExecCmd0, args = Args0}) ->
{ok, Port}.
%%
-spec check_manifest(Manifest :: map()) -> ok | {error, Reason :: binary()}.
-spec check_manifest(Manifest :: map()) -> {ok, #manifest{}} | {error, Reason :: binary()}.
check_manifest(Manifest) when is_map(Manifest) ->
RequiredKeys = [<<"serivce_id">>, <<"exec">>, <<"args">>, <<"health_check">>],
RequiredKeys = [<<"id">>, <<"exec">>, <<"args">>, <<"health_check">>],
check_manifest0(RequiredKeys, Manifest, #manifest{});
check_manifest(_Manifest) ->
{error, <<"invalid manifest json">>}.
check_manifest0([], _Settings, Manifest) ->
{ok, Manifest};
check_manifest0([<<"service_id">>|T], Settings, Manifest) ->
case maps:find(<<"serivce_id">>, Settings) of
check_manifest0([<<"id">>|T], Settings, Manifest) ->
case maps:find(<<"id">>, Settings) of
error ->
{error, <<"miss service_id">>};
{ok, ServiceId} when is_binary(ServiceId) ->
check_manifest0(T, Settings, Manifest#manifest{service_id = ServiceId});
{ok, Id} when is_binary(Id) ->
check_manifest0(T, Settings, Manifest#manifest{id = Id});
{ok, _} ->
{error, <<"service_id is not string">>}
end;