Current section

Files

Jump to
go_over src go_over@util@cache.erl
Raw

src/go_over@util@cache.erl

-module(go_over@util@cache).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/go_over/util/cache.gleam").
-export([pull_if_not_cached/6]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-file("src/go_over/util/cache.gleam", 12).
?DOC(false).
-spec cache_name(binary()) -> binary().
cache_name(Path) ->
filepath:join(Path, <<".go-over-cache"/utf8>>).
-file("src/go_over/util/cache.gleam", 16).
?DOC(false).
-spec version_name(binary()) -> binary().
version_name(Path) ->
filepath:join(Path, <<".go-over-version"/utf8>>).
-file("src/go_over/util/cache.gleam", 20).
?DOC(false).
-spec file_cached(binary(), integer()) -> {ok, boolean()} | {error, nil}.
file_cached(Path, Max_age_seconds) ->
_pipe = Path,
_pipe@1 = cache_name(_pipe),
_pipe@2 = simplifile:read(_pipe@1),
_pipe@3 = gleam@result:replace_error(_pipe@2, nil),
_pipe@4 = gleam@result:'try'(
_pipe@3,
fun gleam@time@timestamp:parse_rfc3339/1
),
gleam@result:map(
_pipe@4,
fun(V) ->
Cutoff = gleam@time@timestamp:add(
V,
gleam@time@duration:seconds(Max_age_seconds)
),
case gleam@time@timestamp:compare(
gleam@time@timestamp:system_time(),
Cutoff
) of
lt ->
true;
eq ->
true;
_ ->
false
end
end
).
-file("src/go_over/util/cache.gleam", 36).
?DOC(false).
-spec pull_if_not_cached(
binary(),
integer(),
boolean(),
boolean(),
fun(() -> nil),
binary()
) -> nil.
pull_if_not_cached(Path, Max_age, Force_pull, Verbose, Pull_fn, Cache_message) ->
case {Force_pull, file_cached(Path, Max_age)} of
{false, {ok, true}} ->
go_over@util@print:progress(
Verbose,
<<"Cached: "/utf8, Cache_message/binary>>
),
nil;
{_, _} ->
Pull_fn(),
Now = begin
_pipe = gleam@time@timestamp:system_time(),
gleam@time@timestamp:to_rfc3339(_pipe, {duration, 0, 0})
end,
_pipe@1 = simplifile:create_directory_all(Path),
gxyz@cli:hard_fail_with_msg(
_pipe@1,
<<"could not write cache file for "/utf8, Path/binary>>
),
_pipe@2 = Path,
_pipe@3 = cache_name(_pipe@2),
_pipe@4 = simplifile:write(_pipe@3, Now),
gxyz@cli:hard_fail_with_msg(
_pipe@4,
<<"could not write cache file for "/utf8, Path/binary>>
),
_pipe@5 = Path,
_pipe@6 = version_name(_pipe@5),
_pipe@7 = simplifile:write(_pipe@6, <<"3.1.0"/utf8>>),
gxyz@cli:hard_fail_with_msg(
_pipe@7,
<<"could not write cache file for "/utf8, Path/binary>>
),
nil
end.