Current section
Files
Jump to
Current section
Files
src/yodel@internal@profiles.erl
-module(yodel@internal@profiles).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/yodel/internal/profiles.gleam").
-export([active_profiles/1, discover_configs/3]).
-export_type([config_file/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.
?MODULEDOC(false).
-type config_file() :: {config_file, binary(), gleam@option:option(binary())}.
-file("src/yodel/internal/profiles.gleam", 21).
?DOC(false).
-spec active_profiles(yodel@options:options()) -> list(binary()).
active_profiles(Options) ->
case begin
_pipe = Options,
_pipe@1 = yodel@options:get_profile_env_var(_pipe),
envoy_ffi:get(_pipe@1)
end of
{ok, Value} ->
_pipe@2 = Value,
_pipe@3 = gleam@string:trim(_pipe@2),
_pipe@4 = gleam@string:split(_pipe@3, <<","/utf8>>),
_pipe@5 = gleam@list:map(_pipe@4, fun gleam@string:trim/1),
gleam@list:filter(_pipe@5, fun(P) -> P /= <<""/utf8>> end);
{error, _} ->
_pipe@6 = Options,
yodel@options:get_profiles(_pipe@6)
end.
-file("src/yodel/internal/profiles.gleam", 96).
?DOC(false).
-spec group_by_profile(list(config_file())) -> list({gleam@option:option(binary()),
list(config_file())}).
group_by_profile(Config_files) ->
_pipe = Config_files,
_pipe@1 = gleam@list:group(_pipe, fun(Cf) -> erlang:element(3, Cf) end),
maps:to_list(_pipe@1).
-file("src/yodel/internal/profiles.gleam", 105).
?DOC(false).
-spec check_groups_for_duplicates(
list({gleam@option:option(binary()), list(config_file())})
) -> {ok, nil} | {error, yodel@errors:config_error()}.
check_groups_for_duplicates(Groups) ->
case Groups of
[] ->
{ok, nil};
[{Profile, Configs} | Rest] ->
case Configs of
[] ->
check_groups_for_duplicates(Rest);
[_] ->
check_groups_for_duplicates(Rest);
[First, Second | _] ->
Error_msg = case Profile of
none ->
<<<<<<"Multiple base config files found: "/utf8,
(erlang:element(2, First))/binary>>/binary,
" and "/utf8>>/binary,
(erlang:element(2, Second))/binary>>;
{some, Name} ->
<<<<<<<<<<"Multiple config files found for profile '"/utf8,
Name/binary>>/binary,
"': "/utf8>>/binary,
(erlang:element(2, First))/binary>>/binary,
" and "/utf8>>/binary,
(erlang:element(2, Second))/binary>>
end,
{error, {validation_error, {invalid_config, Error_msg}}}
end
end.
-file("src/yodel/internal/profiles.gleam", 86).
?DOC(false).
-spec check_for_duplicates(list(config_file())) -> {ok, nil} |
{error, yodel@errors:config_error()}.
check_for_duplicates(Config_files) ->
_pipe = Config_files,
_pipe@1 = group_by_profile(_pipe),
check_groups_for_duplicates(_pipe@1).
-file("src/yodel/internal/profiles.gleam", 161).
?DOC(false).
-spec has_config_extension(binary()) -> boolean().
has_config_extension(Filename) ->
Ext = yodel@internal@input:get_extension_from_path(Filename),
_pipe = yodel@internal@parser:supported_extensions(),
gleam@list:contains(_pipe, Ext).
-file("src/yodel/internal/profiles.gleam", 156).
?DOC(false).
-spec matches_base_pattern(binary(), binary()) -> boolean().
matches_base_pattern(Filename, Base_name) ->
gleam_stdlib:string_starts_with(Filename, Base_name) andalso has_config_extension(
Filename
).
-file("src/yodel/internal/profiles.gleam", 190).
?DOC(false).
-spec extract_filename(binary()) -> binary().
extract_filename(Path) ->
_pipe = Path,
_pipe@1 = gleam@string:split(_pipe, <<"/"/utf8>>),
_pipe@2 = gleam@list:last(_pipe@1),
gleam@result:unwrap(_pipe@2, <<""/utf8>>).
-file("src/yodel/internal/profiles.gleam", 195).
?DOC(false).
-spec remove_extension(binary()) -> binary().
remove_extension(Filename) ->
Parts = gleam@string:split(Filename, <<"."/utf8>>),
_pipe = Parts,
_pipe@1 = gleam@list:take(_pipe, erlang:length(Parts) - 1),
gleam@string:join(_pipe@1, <<"."/utf8>>).
-file("src/yodel/internal/profiles.gleam", 172).
?DOC(false).
-spec extract_profile(binary(), binary()) -> gleam@option:option(binary()).
extract_profile(Filename, Base_name) ->
Without_ext = remove_extension(Filename),
case Without_ext =:= Base_name of
true ->
none;
false ->
Prefix = <<Base_name/binary, "-"/utf8>>,
case gleam_stdlib:string_starts_with(Without_ext, Prefix) of
true ->
_pipe = gleam@string:drop_start(
Without_ext,
string:length(Prefix)
),
{some, _pipe};
false ->
none
end
end.
-file("src/yodel/internal/profiles.gleam", 139).
?DOC(false).
-spec parse_config_file(binary(), binary()) -> {ok, config_file()} |
{error, nil}.
parse_config_file(Path, Base_name) ->
Filename = extract_filename(Path),
case matches_base_pattern(Filename, Base_name) of
true ->
case extract_profile(Filename, Base_name) of
{some, Profile} ->
{ok, {config_file, Path, {some, Profile}}};
none ->
{ok, {config_file, Path, none}}
end;
false ->
{error, nil}
end.
-file("src/yodel/internal/profiles.gleam", 39).
?DOC(false).
-spec discover_configs(binary(), binary(), yodel@options:options()) -> {ok,
list(config_file())} |
{error, yodel@errors:config_error()}.
discover_configs(Directory, Base_name, Options) ->
gleam@result:'try'(
yodel@internal@input:list_files(Directory),
fun(Files) ->
Active_profiles = active_profiles(Options),
Config_files = begin
_pipe = Files,
gleam@list:filter_map(
_pipe,
fun(Path) -> parse_config_file(Path, Base_name) end
)
end,
gleam@result:'try'(
begin
_pipe@1 = Config_files,
check_for_duplicates(_pipe@1)
end,
fun(_) ->
Base_configs = begin
_pipe@2 = Config_files,
gleam@list:filter(
_pipe@2,
fun(Cf) -> erlang:element(3, Cf) =:= none end
)
end,
Active_configs = begin
_pipe@3 = Active_profiles,
gleam@list:filter_map(
_pipe@3,
fun(Profile_name) -> _pipe@4 = Config_files,
_pipe@5 = gleam@list:find(
_pipe@4,
fun(Cf@1) -> case erlang:element(3, Cf@1) of
{some, P} ->
P =:= Profile_name;
none ->
false
end end
),
gleam@result:replace_error(_pipe@5, nil) end
)
end,
case Base_configs of
[] ->
{ok, Active_configs};
[Base] ->
{ok, [Base | Active_configs]};
[First, Second | _] ->
Error_msg = <<<<<<"Multiple base config files found: "/utf8,
(erlang:element(2, First))/binary>>/binary,
" and "/utf8>>/binary,
(erlang:element(2, Second))/binary>>,
{error,
{validation_error, {invalid_config, Error_msg}}}
end
end
)
end
).