src/gleeps@dev.erl

-module(gleeps@dev).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeps/dev.gleam").
-export([main/0]).
-export_type([raw_args/0, push/0]).

-type raw_args() :: {raw_push_args, binary(), binary()}.

-type push() :: {push, binary(), binary(), youid@uuid:uuid(), youid@uuid:uuid()}.

-file("src/gleeps/dev.gleam", 137).
-spec modules_to_json_string(gleam@dict:dict(binary(), binary())) -> binary().
modules_to_json_string(Code) ->
    _pipe = gleam@json:object(
        [{<<"modules"/utf8>>,
                gleam@json:dict(
                    Code,
                    fun gleam@function:identity/1,
                    fun gleam@json:string/1
                )}]
    ),
    gleam@json:to_string(_pipe).

-file("src/gleeps/dev.gleam", 144).
-spec read_file_content(list(binary())) -> {ok,
        gleam@dict:dict(binary(), binary())} |
    {error, binary()}.
read_file_content(Files) ->
    _pipe = Files,
    _pipe@3 = gleam@list:map(
        _pipe,
        fun(Module) ->
            gleam@result:'try'(
                begin
                    _pipe@1 = Module,
                    _pipe@2 = simplifile:read(_pipe@1),
                    gleam@result:map_error(
                        _pipe@2,
                        fun(Error) ->
                            <<<<<<<<"Failed to read file content '"/utf8,
                                            Module/binary>>/binary,
                                        "' "/utf8>>/binary,
                                    (simplifile:describe_error(Error))/binary>>/binary,
                                " "/utf8>>
                        end
                    )
                end,
                fun(Content) -> {ok, {Module, Content}} end
            )
        end
    ),
    _pipe@4 = gleam@result:all(_pipe@3),
    gleam@result:map(_pipe@4, fun maps:from_list/1).

-file("src/gleeps/dev.gleam", 167).
-spec recursively_read_directory(binary()) -> {ok, list(binary())} |
    {error, simplifile:file_error()}.
recursively_read_directory(Path) ->
    gleam@result:'try'(
        simplifile_erl:is_directory(Path),
        fun(Is_dir) ->
            gleam@bool:guard(
                gleam@bool:negate(Is_dir),
                {ok, [Path]},
                fun() ->
                    gleam@result:'try'(
                        simplifile_erl:read_directory(Path),
                        fun(Directory) ->
                            _pipe@2 = gleam@list:map(
                                Directory,
                                fun(File) -> _pipe = Path,
                                    _pipe@1 = filepath:join(_pipe, File),
                                    recursively_read_directory(_pipe@1) end
                            ),
                            _pipe@3 = gleam@result:all(_pipe@2),
                            gleam@result:map(_pipe@3, fun lists:append/1)
                        end
                    )
                end
            )
        end
    ).

-file("src/gleeps/dev.gleam", 101).
-spec do_push(push()) -> {ok, nil} | {error, binary()}.
do_push(Args) ->
    gleam@result:'try'(
        begin
            _pipe = recursively_read_directory(erlang:element(2, Args)),
            gleam@result:map_error(
                _pipe,
                fun(Error) ->
                    <<"Failed to read directory: "/utf8,
                        (simplifile:describe_error(Error))/binary>>
                end
            )
        end,
        fun(Files) ->
            gleam@result:'try'(
                read_file_content(Files),
                fun(Code) ->
                    gleam@result:'try'(
                        begin
                            _pipe@1 = gleam@http@request:to(
                                erlang:element(3, Args)
                            ),
                            gleam@result:replace_error(
                                _pipe@1,
                                <<"The url you supplied is invalid"/utf8>>
                            )
                        end,
                        fun(Req) ->
                            Resp = begin
                                _pipe@2 = Req,
                                _pipe@3 = gleam@http@request:set_method(
                                    _pipe@2,
                                    post
                                ),
                                _pipe@4 = gleam@http@request:set_path(
                                    _pipe@3,
                                    <<"code/submit"/utf8>>
                                ),
                                _pipe@6 = gleam@http@request:set_header(
                                    _pipe@4,
                                    <<"user-id"/utf8>>,
                                    begin
                                        _pipe@5 = erlang:element(4, Args),
                                        youid@uuid:to_base64(_pipe@5)
                                    end
                                ),
                                _pipe@8 = gleam@http@request:set_header(
                                    _pipe@6,
                                    <<"api-key"/utf8>>,
                                    begin
                                        _pipe@7 = erlang:element(5, Args),
                                        youid@uuid:to_base64(_pipe@7)
                                    end
                                ),
                                _pipe@9 = gleam@http@request:set_body(
                                    _pipe@8,
                                    modules_to_json_string(Code)
                                ),
                                gleam@httpc:send(_pipe@9)
                            end,
                            gleam@result:'try'(
                                begin
                                    _pipe@10 = Resp,
                                    gleam@result:map_error(
                                        _pipe@10,
                                        fun(Error@1) ->
                                            <<"The request failed: "/utf8,
                                                (gleam@string:inspect(Error@1))/binary>>
                                        end
                                    )
                                end,
                                fun(Resp@1) ->
                                    _pipe@11 = (<<"Server responded:\n"/utf8,
                                        (erlang:element(4, Resp@1))/binary>>),
                                    _pipe@12 = gleam_stdlib:println(_pipe@11),
                                    {ok, _pipe@12}
                                end
                            )
                        end
                    )
                end
            )
        end
    ).

-file("src/gleeps/dev.gleam", 90).
-spec ok_or_print({ok, JZU} | {error, nil}, binary(), fun((JZU) -> nil)) -> nil.
ok_or_print(Result, Error_to_print, Continue) ->
    case Result of
        {ok, Value} ->
            Continue(Value);

        {error, _} ->
            gleam_stdlib:println_error(Error_to_print)
    end.

-file("src/gleeps/dev.gleam", 78).
-spec url_opt() -> clip@opt:opt(binary()).
url_opt() ->
    _pipe = clip@opt:new(<<"url"/utf8>>),
    _pipe@1 = clip@opt:default(_pipe, <<"http://localhost:3000"/utf8>>),
    clip@opt:help(_pipe@1, <<"The url of the server you want to push to"/utf8>>).

-file("src/gleeps/dev.gleam", 84).
-spec path_arg() -> clip@arg:arg(binary()).
path_arg() ->
    _pipe = clip@arg:new(<<"path"/utf8>>),
    _pipe@1 = clip@arg:default(_pipe, <<"./src"/utf8>>),
    clip@arg:help(_pipe@1, <<"Path to your ./src directory"/utf8>>).

-file("src/gleeps/dev.gleam", 66).
-spec push_command() -> clip:command(raw_args()).
push_command() ->
    _pipe = clip:command(
        begin
            clip:parameter(
                fun(Path) ->
                    clip:parameter(fun(Url) -> {raw_push_args, Path, Url} end)
                end
            )
        end
    ),
    _pipe@1 = clip:arg(_pipe, path_arg()),
    _pipe@2 = clip:opt(_pipe@1, url_opt()),
    clip:help(
        _pipe@2,
        clip@help:simple(
            <<"push"/utf8>>,
            <<"Submit your code to the gleeps server"/utf8>>
        )
    ).

-file("src/gleeps/dev.gleam", 34).
-spec clip() -> nil.
clip() ->
    Result = begin
        _pipe = clip:subcommands([{<<"push"/utf8>>, push_command()}]),
        _pipe@1 = clip:help(
            _pipe,
            clip@help:simple(
                <<"gleam run -m gleeps/dev"/utf8>>,
                <<"Tools for developing a gleeps bot"/utf8>>
            )
        ),
        clip:run(_pipe@1, erlang:element(4, argv:load()))
    end,
    case Result of
        {error, E} ->
            gleam_stdlib:println_error(E);

        {ok, {raw_push_args, _, _} = Args} ->
            ok_or_print(
                begin
                    _pipe@2 = envoy_ffi:get(<<"GLEEPS_USERID"/utf8>>),
                    gleam@result:'try'(_pipe@2, fun youid@uuid:from_base64/1)
                end,
                <<"The GLEEPS_USERID environment variable is missing"/utf8>>,
                fun(User_id) ->
                    ok_or_print(
                        begin
                            _pipe@3 = envoy_ffi:get(<<"GLEEPS_APIKEY"/utf8>>),
                            gleam@result:'try'(
                                _pipe@3,
                                fun youid@uuid:from_base64/1
                            )
                        end,
                        <<"The GLEEPS_APIKEY environment variable is missing"/utf8>>,
                        fun(Api_key) ->
                            case do_push(
                                {push,
                                    erlang:element(2, Args),
                                    erlang:element(3, Args),
                                    User_id,
                                    Api_key}
                            ) of
                                {ok, _} ->
                                    nil;

                                {error, Message} ->
                                    gleam_stdlib:println_error(Message)
                            end
                        end
                    )
                end
            )
    end.

-file("src/gleeps/dev.gleam", 30).
-spec main() -> nil.
main() ->
    clip().