Packages

🛒 A small library of helper functions, that works as a companion of gleam_stdlib

Current section

Files

Jump to
gxyz src gxyz@cli.erl
Raw

src/gxyz@cli.erl

-module(gxyz@cli).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gxyz/cli.gleam").
-export([strip_js_from_argv/1, hard_fail/1, hard_fail_with_msg/2, custom_hard_fail/2, hard_fail_with_opts/3]).
-export_type([fail_conf/0, fail_opt/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-type fail_conf() :: {fail_conf,
gleam@option:option(binary()),
integer(),
boolean()}.
-type fail_opt() :: {fail_opt_message, binary()} |
{fail_opt_exit_code, integer()} |
fail_opt_echo.
-file("src/gxyz/cli.gleam", 20).
-spec do_hard_fail({ok, FDT} | {error, any()}, fail_conf()) -> FDT.
do_hard_fail(Res, Conf) ->
case Res of
{ok, A} ->
A;
{error, Err} ->
gleam@option:map(
erlang:element(2, Conf),
fun gleam_stdlib:print_error/1
),
case erlang:element(4, Conf) of
false ->
nil;
true ->
_pipe = Err,
_pipe@1 = gleam@string:inspect(_pipe),
gleam_stdlib:print_error(_pipe@1)
end,
erlang:halt(erlang:element(3, Conf)),
erlang:error(#{gleam_error => panic,
message => <<"Unreachable, please create an issue in https://github.com/bwireman/gxyz if you see this"/utf8>>,
file => <<?FILEPATH/utf8>>,
module => <<"gxyz/cli"/utf8>>,
function => <<"do_hard_fail"/utf8>>,
line => 34})
end.
-file("src/gxyz/cli.gleam", 71).
?DOC(" strip js/ts files from argv (often present in node and deno implementation)\n").
-spec strip_js_from_argv(list(binary())) -> list(binary()).
strip_js_from_argv(Args) ->
_pipe = Args,
_pipe@1 = gxyz@list:reject(
_pipe,
fun(_capture) ->
gleam_stdlib:string_ends_with(_capture, <<".js"/utf8>>)
end
),
_pipe@2 = gxyz@list:reject(
_pipe@1,
fun(_capture@1) ->
gleam_stdlib:string_ends_with(_capture@1, <<".mjs"/utf8>>)
end
),
_pipe@3 = gxyz@list:reject(
_pipe@2,
fun(_capture@2) ->
gleam_stdlib:string_ends_with(_capture@2, <<".cjs"/utf8>>)
end
),
_pipe@4 = gxyz@list:reject(
_pipe@3,
fun(_capture@3) ->
gleam_stdlib:string_ends_with(_capture@3, <<".ts"/utf8>>)
end
),
_pipe@5 = gxyz@list:reject(
_pipe@4,
fun(_capture@4) ->
gleam_stdlib:string_ends_with(_capture@4, <<".mts"/utf8>>)
end
),
gxyz@list:reject(
_pipe@5,
fun(_capture@5) ->
gleam_stdlib:string_ends_with(_capture@5, <<".cts"/utf8>>)
end
).
-file("src/gxyz/cli.gleam", 40).
?DOC(" Hard exit the program if the result is an Error and return the value if an Ok, exit 1, without a message\n").
-spec hard_fail({ok, FDX} | {error, any()}) -> FDX.
hard_fail(Res) ->
do_hard_fail(Res, {fail_conf, none, 1, false}).
-file("src/gxyz/cli.gleam", 45).
?DOC(" Hard exit the program if the result is an Error and return the value if an Ok, with the exit 1 and printing msg to stderror\n").
-spec hard_fail_with_msg({ok, FEB} | {error, any()}, binary()) -> FEB.
hard_fail_with_msg(Res, Msg) ->
do_hard_fail(
Res,
{fail_conf,
{some, Msg},
erlang:element(3, {fail_conf, none, 1, false}),
erlang:element(4, {fail_conf, none, 1, false})}
).
-file("src/gxyz/cli.gleam", 49).
-spec custom_hard_fail({ok, FEF} | {error, any()}, list(fail_opt())) -> FEF.
custom_hard_fail(Res, Options) ->
_pipe = gleam@list:fold(
Options,
{fail_conf, none, 1, false},
fun(Conf, Opt) -> case Opt of
{fail_opt_message, Msg} ->
{fail_conf,
{some, Msg},
erlang:element(3, Conf),
erlang:element(4, Conf)};
{fail_opt_exit_code, Status} ->
{fail_conf,
erlang:element(2, Conf),
Status,
erlang:element(4, Conf)};
fail_opt_echo ->
{fail_conf,
erlang:element(2, Conf),
erlang:element(3, Conf),
true}
end end
),
do_hard_fail(Res, _pipe).
-file("src/gxyz/cli.gleam", 62).
?DOC(" Hard exit the program if the result is an Error and return the value if an Ok, with the exit code and printing msg to stderror\n").
-spec hard_fail_with_opts({ok, FEL} | {error, any()}, binary(), integer()) -> FEL.
hard_fail_with_opts(Res, Msg, Status_code) ->
do_hard_fail(
Res,
{fail_conf,
{some, Msg},
Status_code,
erlang:element(4, {fail_conf, none, 1, false})}
).