Skip to content

Commit

Permalink
modified escript to take etc, and use app.config if provided
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe DeVivo committed Aug 8, 2013
1 parent 08a71e5 commit 106b8b7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/cuttlefish.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{application, cuttlefish,
[
{description, ""},
{vsn, "1"},
{description, "cuttlefish configuration abstraction"},
{vsn, "0.1.0"},
{registered, []},
{applications, [
kernel,
Expand Down
38 changes: 33 additions & 5 deletions src/cuttlefish_escript.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ cli_options() ->
%% Option Name, Short Code, Long Code, Argument Spec, Help Message
[
{help, $h, "help", undefined, "Print this usage page"},
{ent_dir, $e, "etc_dir", {string, "/etc"}, "etc dir"},
{dest_dir, $d, "dest_dir", {string, "/tmp"}, "speficies the directory to write the config file to"},
{dest_file, $f, "dest_file", {string, "app.config"}, "the file name to write"},
{schema_file, $s, "schema_file", string, "a cuttlefish schema file, multiple files allowed"},
{conf_file, $c, "conf_file", string, "a cuttlefish conf file, multiple files allowed"}
{conf_file, $c, "conf_file", string, "a cuttlefish conf file, multiple files allowed"},
{app_config, $a, "app_config", string, "the advanced erlangy app.config"}
].

%% LOL! I wanted this to be halt 0, but honestly, if this escript does anything
Expand All @@ -27,8 +29,6 @@ run_help([]) -> true;
run_help(ParsedArgs) ->
lists:member(help, ParsedArgs).

%% Arg1 is destination path
%% Assume args are a space separated list of filenames
main(Args) ->
{ParsedArgs, _GarbageFile} = case getopt:parse(cli_options(), Args) of
{ok, {P, H}} -> {P, H};
Expand All @@ -40,6 +40,23 @@ main(Args) ->
_ -> ok
end,

%% If /etc/app.config exists, use it and disable cuttlefish
%% even though cuttlefish is awesome
EtcDir = proplists:get_value(etc_dir, ParsedArgs),
case filelib:is_file(filename:join(EtcDir, "app.config")) of
true ->
AppConf = filename:join(EtcDir, "app.config"),
?STDERR("~s exists, disabling cuttlefish.", [AppConf]),
%% TODO: placeholder to basho's cuttlefish documentation url
?STDERR("If you'd like to know more about cuttlefish, check your local library!", []),
?STDOUT("~s", [AppConf]),
halt(0);
_ ->
%% Just keep going
?STDERR("no app.config detected in ~s, activating cuttlefish", [EtcDir]),
ok
end,

ConfFiles = proplists:get_all_values(conf_file, ParsedArgs),
SchemaFiles = proplists:get_all_values(schema_file, ParsedArgs),
DestinationPath = proplists:get_value(dest_dir, ParsedArgs),
Expand All @@ -50,12 +67,23 @@ main(Args) ->
?STDERR("ConfFiles: ~p", [ConfFiles]),
?STDERR("SchemaFiles: ~p", [SchemaFiles]),

%% TODO: Support multiple files
%% TODO?: Support multiple files
{Translations, Schema} = cuttlefish_schema:file(hd(SchemaFiles)),
Conf = cuttlefish_conf:file(hd(ConfFiles)),
NewConfig = cuttlefish_generator:map(Translations, Schema, Conf),

file:write_file(Destination,io_lib:fwrite("~p.\n",[NewConfig])),
AdvancedConfig = filename:join(EtcDir, "advanced.config"),
FinalConfig = case filelib:is_file(AdvancedConfig) of
true ->
?STDERR("~s/advanced.config detected, overlaying proplists", [EtcDir]),
%% TODO: this should not be NewConfig
NewConfig;
_ ->
%% Nothing to see here, these aren't the droids you're looking for.
NewConfig
end,

file:write_file(Destination,io_lib:fwrite("~p.\n",[FinalConfig])),
%% todo: write out dated archived version
%%file:write_file(filename:join(DestinationPath, DestinationFilename ++ "." ++ ),io_lib:fwrite("~p.\n",[NewConfig])),
?STDOUT("~s", [Destination]),
Expand Down
1 change: 0 additions & 1 deletion src/cuttlefish_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
[cuttlefish_mapping:mapping()]
} | error.
file(Filename) ->
io:format("HI!~n"),
{ok, B} = file:read_file(Filename),
%% TODO: Hardcoded utf8
S = unicode:characters_to_list(B, utf8),
Expand Down

0 comments on commit 106b8b7

Please sign in to comment.