mad.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. -module(mad).
  2. %%-author('Maxim Sokhatsky').
  3. -include("mad.hrl").
  4. -compile([export_all, nowarn_export_all]).
  5. -export([main/1]).
  6. main([]) -> help();
  7. main(Params) ->
  8. {Invalid, Valid} = lists:foldr(
  9. fun(X, {C, R}) when erlang:is_atom(X) ->
  10. {[], [{X, C}|R]};
  11. (X, {C, R}) ->
  12. {[X|C], R}
  13. end, {[], []}, lists:map(fun atomize/1, Params) ),
  14. return(
  15. lists:any(fun({error,_}) -> true; (_) -> false end,
  16. lists:flatten(
  17. lists:foldl(fun({Fun,Arg},[]) ->
  18. mad_hooks:run_hooks(pre, Fun),
  19. Errors = errors((profile()):Fun(Arg)),
  20. mad_hooks:run_hooks(post, Fun),
  21. Errors;
  22. ({_,_},Err) ->
  23. errors(Invalid), {return,Err}
  24. end,
  25. [], Valid)
  26. )
  27. )
  28. ).
  29. atomize("static") -> 'static';
  30. atomize("deploy") -> 'deploy';
  31. atomize("app"++_) -> 'app';
  32. atomize("dep") -> 'deps';
  33. atomize("deps") -> 'deps';
  34. atomize("cle"++_) -> 'clean';
  35. atomize("com"++_) -> 'compile';
  36. atomize("eunit") -> 'eunit';
  37. atomize("up") -> 'up';
  38. atomize("rel"++_) -> 'release';
  39. atomize("bun"++_) -> 'release';
  40. atomize("sta"++_) -> 'start';
  41. atomize("sto"++_) -> 'stop';
  42. atomize("att"++_) -> 'attach';
  43. atomize("sh") -> 'sh';
  44. atomize("rep"++_) -> 'sh';
  45. atomize("pla"++_) -> 'resolve';
  46. atomize("str"++_) -> 'strip';
  47. atomize(Else) -> Else.
  48. profile() -> application:get_env(mad,profile,mad_local).
  49. errors([]) -> [];
  50. errors(false) -> [];
  51. errors(true) -> {error,unknown};
  52. errors({error,L}) -> info("ERROR: ~tp~n",[L]), [{error,L}];
  53. errors({ok,_}) -> info("OK~n",[]), [];
  54. errors(X) -> info("RETURN: ~tp~n",[X]), [{error,X}].
  55. return(true) -> 1;
  56. return(false) -> 0;
  57. return(X) -> X.
  58. host() ->
  59. try
  60. {ok,H} = inet:gethostname(),
  61. H
  62. catch _:_ ->
  63. <<>>
  64. end.
  65. info(Format) -> io:format(lists:concat([Format,"\r"])).
  66. info(Format,Args) -> io:format(lists:concat([Format,"\r"]),Args).
  67. help(Reason,D) -> help(io_lib:format("~s ~p", [Reason, D])).
  68. help(_Msg) -> help().
  69. help() -> info("MAD Container Tool version ~s~n",[?VERSION]),
  70. info("~n"),
  71. info(" invoke = mad params~n"),
  72. info(" params = [] | command [ options ] params ~n"),
  73. info(" command = app | deps | clean | compile | up | eunit | strip~n"),
  74. info(" | release [ beam | ling | script | runc | depot ]~n"),
  75. info(" | deploy | start | stop | attach | sh | static [ <watch|min> ] ~n"),
  76. return(false).