Current section
Files
Jump to
Current section
Files
src/cactus@run.erl
-module(cactus@run).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([as_string/1, run/2]).
-export_type([action_kind/0, action/0]).
-type action_kind() :: module | sub_command | binary.
-type action() :: {action, binary(), action_kind(), list(binary())}.
-spec as_string(tom:toml()) -> binary().
as_string(T) ->
case T of
{string, X} ->
X;
_ ->
gleam@io:println_error(<<"Invalid toml field"/utf8>>),
shellout_ffi:os_exit(1),
erlang:error(#{gleam_error => panic,
message => <<"unreachable"/utf8>>,
module => <<"cactus/run"/utf8>>,
function => <<"as_string"/utf8>>,
line => 91})
end.
-spec parse(tom:toml()) -> {ok, action()} | {error, nil}.
parse(Raw) ->
_pipe@5 = case Raw of
{inline_table, T} ->
gleam@result:'try'(
begin
_pipe = tom:get_string(T, [<<"command"/utf8>>]),
gleam@result:nil_error(_pipe)
end,
fun(Command) ->
gleam@result:'try'(
begin
_pipe@1 = tom:get_string(T, [<<"kind"/utf8>>]),
_pipe@2 = gleam@result:map(
_pipe@1,
fun gleam@string:lowercase/1
),
gleam@result:nil_error(_pipe@2)
end,
fun(Kind) ->
Args = begin
_pipe@3 = tom:get_array(T, [<<"args"/utf8>>]),
_pipe@4 = gleam@result:unwrap(_pipe@3, []),
gleam@list:map(_pipe@4, fun as_string/1)
end,
Action_kind = case Kind of
<<"module"/utf8>> ->
module;
<<"sub_command"/utf8>> ->
sub_command;
<<"binary"/utf8>> ->
binary;
_ ->
module
end,
{ok, {action, Command, Action_kind, Args}}
end
)
end
);
_ ->
{error, nil}
end,
gleam@result:nil_error(_pipe@5).
-spec run(binary(), binary()) -> {ok, list(nil)} | {error, nil}.
run(Path, Action) ->
gleam@result:'try'(
cactus@util:parse_manifest(Path),
fun(Manifest) ->
gleam@result:'try'(
begin
_pipe = tom:get_table(Manifest, [<<"cactus"/utf8>>, Action]),
gleam@result:nil_error(_pipe)
end,
fun(Action_body) ->
gleam@result:'try'(
begin
_pipe@1 = tom:get_array(
Action_body,
[<<"actions"/utf8>>]
),
gleam@result:nil_error(_pipe@1)
end,
fun(Actions) -> _pipe@2 = Actions,
_pipe@3 = gleam@list:map(_pipe@2, fun parse/1),
_pipe@4 = gleam@list:map(
_pipe@3,
fun(Parse_result) ->
gleam@result:'try'(
Parse_result,
fun(Action@1) ->
{Bin, Args} = case erlang:element(
3,
Action@1
) of
module ->
{<<"gleam"/utf8>>,
lists:append(
[<<"run"/utf8>>,
<<"-m"/utf8>>,
erlang:element(
2,
Action@1
),
<<"--"/utf8>>],
erlang:element(
4,
Action@1
)
)};
sub_command ->
{<<"gleam"/utf8>>,
lists:append(
[erlang:element(
2,
Action@1
)],
erlang:element(
4,
Action@1
)
)};
binary ->
{erlang:element(2, Action@1),
erlang:element(
4,
Action@1
)}
end,
case shellout:command(
Bin,
Args,
<<"."/utf8>>,
[]
) of
{ok, Res} ->
gleam@io:print(Res),
{ok, nil};
{error, {_, Err}} ->
gleam@io:print_error(Err),
{error, nil}
end
end
)
end
),
_pipe@5 = gleam@result:all(_pipe@4),
gleam@result:nil_error(_pipe@5) end
)
end
)
end
).