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