-module(gleeam_code@auth).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gleeam_code/auth.gleam").
-export([run/3, stdin_read_line/1]).
-file("src/gleeam_code/auth.gleam", 22).
-spec check_env_var(
fun((binary()) -> nil),
fun((binary()) -> {ok, binary()} | {error, nil})
) -> {ok, boolean()} | {error, binary()}.
check_env_var(Print, Read_line) ->
case envoy_ffi:get(<<"LEETCODE_SESSION"/utf8>>) of
{ok, Value} when Value =/= <<""/utf8>> ->
Print(
<<"LEETCODE_SESSION environment variable is already set."/utf8>>
),
Print(
<<"glc uses the session file (~/.gleeam/session) with higher priority."/utf8>>
),
case Read_line(<<"Save a separate session anyway? [y/N]: "/utf8>>) of
{ok, Input} ->
case begin
_pipe = gleam@string:trim(Input),
string:lowercase(_pipe)
end of
<<"y"/utf8>> ->
{ok, true};
<<"yes"/utf8>> ->
{ok, true};
_ ->
{ok, false}
end;
{error, _} ->
{error, <<"Failed to read input"/utf8>>}
end;
_ ->
{ok, true}
end.
-file("src/gleeam_code/auth.gleam", 45).
-spec check_existing_file(
fun((binary()) -> nil),
fun((binary()) -> {ok, binary()} | {error, nil})
) -> {ok, boolean()} | {error, binary()}.
check_existing_file(Print, Read_line) ->
case gleeam_code@internal@config:session_file_exists() of
false ->
{ok, true};
true ->
Print(<<"Session file already exists (~/.gleeam/session)."/utf8>>),
case Read_line(<<"Overwrite? [y/N]: "/utf8>>) of
{ok, Input} ->
case begin
_pipe = gleam@string:trim(Input),
string:lowercase(_pipe)
end of
<<"y"/utf8>> ->
{ok, true};
<<"yes"/utf8>> ->
{ok, true};
_ ->
{ok, false}
end;
{error, _} ->
{error, <<"Failed to read input"/utf8>>}
end
end.
-file("src/gleeam_code/auth.gleam", 65).
-spec prompt_and_save(
fun((binary()) -> nil),
fun((binary()) -> {ok, binary()} | {error, nil})
) -> {ok, nil} | {error, binary()}.
prompt_and_save(Print, Read_line) ->
case Read_line(<<"Paste your LEETCODE_SESSION cookie: "/utf8>>) of
{ok, Input} ->
Cookie = gleam@string:trim(Input),
case Cookie of
<<""/utf8>> ->
{error, <<"Empty input. Session not saved."/utf8>>};
_ ->
case gleeam_code@internal@config:save_session(Cookie) of
{ok, _} ->
Print(<<"Session saved to ~/.gleeam/session"/utf8>>),
{ok, nil};
{error, Err} ->
{error, Err}
end
end;
{error, _} ->
{error, <<"Failed to read input"/utf8>>}
end.
-file("src/gleeam_code/auth.gleam", 5).
-spec run(
binary(),
fun((binary()) -> nil),
fun((binary()) -> {ok, binary()} | {error, nil})
) -> {ok, nil} | {error, binary()}.
run(_, Print, Read_line) ->
case check_env_var(Print, Read_line) of
{error, Msg} ->
{error, Msg};
{ok, false} ->
{ok, nil};
{ok, true} ->
case check_existing_file(Print, Read_line) of
{error, Msg@1} ->
{error, Msg@1};
{ok, false} ->
{ok, nil};
{ok, true} ->
prompt_and_save(Print, Read_line)
end
end.
-file("src/gleeam_code/auth.gleam", 89).
-spec stdin_read_line(binary()) -> {ok, binary()} | {error, nil}.
stdin_read_line(Prompt) ->
gleeam_code_io_ffi:get_line(Prompt).