Skip to content

Commit

Permalink
Added cuttlefish escript
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe DeVivo committed Aug 8, 2013
1 parent 2140106 commit 08a71e5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ ebin/*
.eunit
src/conf_parse.erl
deps
cuttlefish

22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.PHONY: deps

all: deps compile
./rebar skip_deps=true escriptize

deps:
./rebar get-deps

docs:
./rebar skip_deps=true doc

docsclean:
@rm -rf doc/*.png doc/*.html doc/*.css edoc-info

compile: deps
./rebar compile

clean:
@./rebar clean

distclean: clean
@rm -rf cuttlefish deps
Binary file added rebar
Binary file not shown.
7 changes: 5 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

{erl_opts, [warnings_as_errors, {parse_transform, lager_transform}, debug_info]}.
{eunit_opts, [verbose]}.
{escript_emu_args, "%%! -escript main cuttlefish_escript\n"}.
{escript_incl_apps, [getopt]}.

{deps, [
{lager, "2.0.0", {git, "git://github.com/basho/lager", {tag, "2.0.0"}}},
{neotoma, "1.6.2", {git, "git://github.com/seancribbs/neotoma", {tag, "1.6.2"}}}
{getopt, ".*", {git, "git://github.com/jcomellas/getopt", {tag, "v0.4"}}},
{lager, "2.0.0", {git, "git://github.com/basho/lager", {tag, "2.0.0"}}},
{neotoma, "1.6.2", {git, "git://github.com/seancribbs/neotoma", {tag, "1.6.2"}}}
]}.
62 changes: 62 additions & 0 deletions src/cuttlefish_escript.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-module(cuttlefish_escript).

-define(STDOUT(Str, Args), io:format(Str ++ "~n", Args)).
-define(STDERR(Str, Args), io:format(standard_error, Str ++ "~n", Args)).

-export([main/1]).

cli_options() ->
%% Option Name, Short Code, Long Code, Argument Spec, Help Message
[
{help, $h, "help", undefined, "Print this usage page"},
{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"}
].

%% LOL! I wanted this to be halt 0, but honestly, if this escript does anything
%% except return the path to a generated config file, it should return a non-zero
%% return code
print_help() ->
getopt:usage(cli_options(),
escript:script_name()),
halt(1).

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};
_ -> print_help()
end,

case run_help(ParsedArgs) of
true -> print_help();
_ -> 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),
DestinationFilename = proplists:get_value(dest_file, ParsedArgs),
Destination = filename:join(DestinationPath, DestinationFilename),

?STDERR("Generating config in: ~p", [Destination]),
?STDERR("ConfFiles: ~p", [ConfFiles]),
?STDERR("SchemaFiles: ~p", [SchemaFiles]),

%% 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])),
%% todo: write out dated archived version
%%file:write_file(filename:join(DestinationPath, DestinationFilename ++ "." ++ ),io_lib:fwrite("~p.\n",[NewConfig])),
?STDOUT("~s", [Destination]),
ok.

0 comments on commit 08a71e5

Please sign in to comment.