Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/standalone vm args generation #12

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/cuttlefish_escript.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ cli_options() ->
{help, $h, "help", undefined, "Print this usage page"},
{etc_dir, $e, "etc_dir", {string, "/etc"}, "etc dir"},
{dest_dir, $d, "dest_dir", string, "specifies the directory to write the config file to"},
{dest_file, $f, "dest_file", {string, "app"}, "the file name to write"},
{dest_file, $f, "dest_file", string, "the file name to write"},
{schema_dir, $s, "schema_dir", string, "a directory containing .schema files"},
%% one or more schema file paths
{schema_file, $i, "schema_file", string, "individual schema file, will be processed in command line order, after -s"},
Expand Down Expand Up @@ -357,13 +357,14 @@ engage_cuttlefish(ParsedArgs) ->
Path -> Path
end,

DestinationFilename = filename_maker(proplists:get_value(dest_file, ParsedArgs), "config"),
DestinationFilename = filename_maker(proplists:get_value(dest_file, ParsedArgs, "app"), "config"),
Destination = filename:join(AbsPath, DestinationFilename),

DestinationVMArgsFilename = filename_maker("vm", "args"),
DestinationVMArgsFilename = filename_maker(proplists:get_value(dest_file, ParsedArgs, "vm"), "args"),
DestinationVMArgs = filename:join(AbsPath, DestinationVMArgsFilename),

lager:debug("Generating config in: ~p", [Destination]),
lager:debug("Generating vm.args in: ~p", [DestinationVMArgs]),

Schema = load_schema(ParsedArgs),
Conf = load_conf(ParsedArgs),
Expand Down Expand Up @@ -407,10 +408,9 @@ engage_cuttlefish(ParsedArgs) ->
prune(Destination, MaxHistory),
prune(DestinationVMArgs, MaxHistory),

case { file:write_file(Destination, io_lib:fwrite("~p.\n",[FinalAppConfig])),
FinalVMArgs =/= [] andalso file:write_file(DestinationVMArgs, string:join(FinalVMArgs, "\n"))} of
{ok, VMArgsWriteResult} when VMArgsWriteResult =:= ok orelse
VMArgsWriteResult =:= false ->
case {maybe_write_file(Destination, "~p.\n", FinalAppConfig),
maybe_write_file(DestinationVMArgs, "~s", string:join(FinalVMArgs, "\n"))} of
{ok, ok} ->
{Destination, DestinationVMArgs};
{Err1, Err2} ->
maybe_log_file_error(Destination, Err1),
Expand All @@ -420,6 +420,15 @@ engage_cuttlefish(ParsedArgs) ->

end.

-spec maybe_write_file(Filename :: string(),
Format :: string(),
Data :: string()) -> ok | {error, file:posix() | badarg | terminated | system_limit}.
maybe_write_file(_, _, []) ->
% nothing to write, write nothing
ok;
maybe_write_file(Filename, Format, Data) ->
file:write_file(Filename, io_lib:fwrite(Format, [Data])).

-spec prune(file:name_all(), integer()) -> ok.
prune(Filename, MaxHistory) ->
%% A Filename comes in /Abs/Path/To/something.YYYY.MM.DD.HH.mm.SS.ext
Expand Down
6 changes: 6 additions & 0 deletions test/cuttlefish_escript_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ silent_test() ->
?assertPrinted("", [exact])
end).

vm_args_test() ->
?capturing(begin
cuttlefish_escript:main(["--schema_file", "priv/erlang_vm.schema", "--conf_file", "test/riak.conf", "--etc_dir", "etc", "--dest_file", "vm.generated.args", "--allow_extra", "--silent"]),
?assertPrinted("", [exact])
end).

-endif.