Current section

Files

Jump to
go_over src go_over.erl
Raw

src/go_over.erl

-module(go_over).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([main/0]).
-export_type([flags/0]).
-type flags() :: {flags,
boolean(),
boolean(),
gleam@option:option(go_over@config:format())}.
-spec merge_flags_and_config(flags(), go_over@config:config()) -> go_over@config:config().
merge_flags_and_config(Flgs, Cfg) ->
{config,
erlang:element(2, Cfg),
erlang:element(2, Flgs),
erlang:element(3, Flgs),
gleam@option:unwrap(erlang:element(4, Flgs), erlang:element(5, Cfg)),
erlang:element(6, Cfg),
erlang:element(7, Cfg),
erlang:element(8, Cfg)}.
-spec spin_up(go_over@config:config()) -> go_over@config:config().
spin_up(Cfg) ->
Args = shellout_ffi:start_arguments(),
Format = begin
_pipe = gleam@list:find(
Args,
fun(Arg) -> gleam@string:starts_with(Arg, <<"--format"/utf8>>) end
),
_pipe@1 = gleam@result:'try'(
_pipe,
fun(Arg@1) -> gleam@string:split_once(Arg@1, <<"="/utf8>>) end
),
_pipe@2 = gleam@result:map(_pipe@1, fun(Arg@2) -> case Arg@2 of
{_, Val} ->
Val
end end),
_pipe@3 = gleam@result:map(
_pipe@2,
fun go_over@config:parse_config_format/1
),
gleam@option:from_result(_pipe@3)
end,
Flags = {flags,
gleam@list:any(Args, fun(Arg@3) -> Arg@3 =:= <<"--force"/utf8>> end),
gleam@list:any(Args, fun(Arg@4) -> Arg@4 =:= <<"--fake"/utf8>> end),
Format},
merge_flags_and_config(Flags, Cfg).
-spec get_vulnerable_packages(
list(go_over@packages:package()),
go_over@config:config()
) -> list(go_over@warning:warning()).
get_vulnerable_packages(Pkgs, Conf) ->
_pipe = go_over@advisories@advisories:check_for_advisories(
Pkgs,
erlang:element(3, Conf) orelse not erlang:element(2, Conf)
),
_pipe@1 = gleam@list:map(
_pipe,
fun(P) ->
{Pkg, Adv} = P,
{Pkg, go_over@config:filter_advisory_ids(Conf, Adv)}
end
),
_pipe@2 = gleam@list:filter(_pipe@1, fun(P@1) -> case P@1 of
{_, []} ->
false;
_ ->
true
end end),
gleam@list:flat_map(
_pipe@2,
fun(P@2) ->
{Pkg@1, Adv@1} = P@2,
go_over@warning:adv_to_warning(Pkg@1, Adv@1)
end
).
-spec get_retired_packges(
list(go_over@packages:package()),
go_over@config:config()
) -> list(go_over@warning:warning()).
get_retired_packges(Pkgs, Conf) ->
_pipe = Pkgs,
_pipe@1 = gleam@list:map(
_pipe,
fun(Pkg) ->
case go_over@retired:check_retired(
Pkg,
erlang:element(3, Conf) orelse not erlang:element(2, Conf)
) of
{some, Ret} ->
{some, {Pkg, Ret}};
none ->
none
end
end
),
_pipe@2 = gleam@option:values(_pipe@1),
gleam@list:map(
_pipe@2,
fun(P) ->
{Pkg@1, Ret@1} = P,
go_over@warning:retired_to_warning(Pkg@1, Ret@1)
end
).
-spec print_warnings_count(list(go_over@warning:warning())) -> nil.
print_warnings_count(Vulns) ->
_pipe = (<<<<<<"⛔ "/utf8,
(gleam@int:to_string(erlang:length(Vulns)))/binary>>/binary,
" WARNING(s) FOUND!"/utf8>>/binary,
(<<"\n-----------------------------------------------\n"/utf8>>)/binary>>),
gleam@io:print_error(_pipe).
-spec print_warnings(list(go_over@warning:warning()), go_over@config:config()) -> nil.
print_warnings(Vulns, Conf) ->
case erlang:element(5, Conf) of
minimal ->
print_warnings_count(Vulns),
_pipe = Vulns,
_pipe@1 = gleam@list:map(
_pipe,
fun go_over@warning:format_as_string_minimal/1
),
_pipe@2 = gleam@string:join(_pipe@1, <<""/utf8>>),
gleam@io:print_error(_pipe@2);
json ->
_pipe@3 = Vulns,
_pipe@4 = gleam@list:map(
_pipe@3,
fun go_over@warning:format_as_json/1
),
_pipe@5 = gleam@json:preprocessed_array(_pipe@4),
_pipe@6 = gleam@json:to_string(_pipe@5),
gleam@io:print_error(_pipe@6);
_ ->
print_warnings_count(Vulns),
_pipe@7 = Vulns,
_pipe@8 = gleam@list:map(
_pipe@7,
fun go_over@warning:format_as_string/1
),
_pipe@9 = gleam@string:join(
_pipe@8,
<<"\n-----------------------------------------------\n"/utf8>>
),
gleam@io:print_error(_pipe@9)
end,
shellout_ffi:os_exit(1).
-spec main() -> nil.
main() ->
Conf = spin_up(go_over@config:read_config(<<"./gleam.toml"/utf8>>)),
go_over@util@util:throwaway(
not erlang:element(2, Conf),
fun() -> simplifile:delete(go_over@util@constants:go_over_path()) end
),
Pkgs = begin
_pipe = go_over@packages:read_manifest(<<"./manifest.toml"/utf8>>),
go_over@config:filter_packages(Conf, _pipe)
end,
Vulnerable_packages = get_vulnerable_packages(Pkgs, Conf),
Retired_packages = get_retired_packges(Pkgs, Conf),
go_over@util@util:iff_nil(
erlang:element(4, Conf),
fun() ->
print_warnings(
[{warning,
none,
<<"fake"/utf8>>,
<<"x.y.z"/utf8>>,
<<"Retired"/utf8>>,
vulnerable,
<<"Critical"/utf8>>,
direct},
{warning,
none,
<<"another_fake"/utf8>>,
<<"1.2.3"/utf8>>,
<<"Vulnerable"/utf8>>,
vulnerable,
<<"High"/utf8>>,
direct},
{warning,
none,
<<"and_another"/utf8>>,
<<"4.5.6"/utf8>>,
<<"Vulnerable"/utf8>>,
vulnerable,
<<"Moderate"/utf8>>,
direct},
{warning,
none,
<<"one_more"/utf8>>,
<<"7.8.9"/utf8>>,
<<"Vulnerable"/utf8>>,
vulnerable,
<<"LOW"/utf8>>,
indirect},
{warning,
none,
<<"this_one_was_retired"/utf8>>,
<<"10.11.12"/utf8>>,
<<"Retired"/utf8>>,
retired,
<<"Package Retired"/utf8>>,
indirect}],
Conf
)
end
),
Warnings = begin
_pipe@1 = lists:append(Retired_packages, Vulnerable_packages),
go_over@config:filter_severity(Conf, _pipe@1)
end,
case Warnings of
[] ->
go_over@util@print:success(<<"✅ All good! ✨"/utf8>>);
Vulns ->
print_warnings(Vulns, Conf)
end.