Current section
Files
Jump to
Current section
Files
src/dotenv_conf.erl
-module(dotenv_conf).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([read_string/2, read_string_or/3, read_int_or/3, read_float_or/3, read_file/2]).
-export_type([env_file_error/0]).
-type env_file_error() :: missing_file |
invalid_file |
file_error |
bad_config |
missing_var.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 48).
-spec read_string(
binary(),
{ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}
) -> {ok, binary()} | {error, env_file_error()}.
read_string(Name, From_file) ->
case {envoy_ffi:get(Name), From_file} of
{{ok, Value}, _} ->
{ok, Value};
{_, {error, Err}} ->
{error, Err};
{_, {ok, Values}} ->
case gleam_stdlib:map_get(Values, Name) of
{ok, Value@1} ->
{ok, Value@1};
_ ->
{error, missing_var}
end
end.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 67).
-spec read_string_or(
binary(),
{ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()},
binary()
) -> binary().
read_string_or(Name, From_file, Default) ->
case read_string(Name, From_file) of
{ok, Value} ->
Value;
_ ->
Default
end.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 82).
-spec read_int_or(
binary(),
{ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()},
integer()
) -> integer().
read_int_or(Name, From_file, Default) ->
case read_string(Name, From_file) of
{error, _} ->
Default;
{ok, Value_str} ->
case gleam_stdlib:parse_int(Value_str) of
{ok, Value} ->
Value;
_ ->
Default
end
end.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 97).
-spec read_float_or(
binary(),
{ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()},
float()
) -> float().
read_float_or(Name, From_file, Default) ->
case read_string(Name, From_file) of
{error, _} ->
Default;
{ok, Value_str} ->
case gleam_stdlib:parse_float(Value_str) of
{ok, Value} ->
Value;
_ ->
Default
end
end.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 142).
-spec remove_comments(binary()) -> binary().
remove_comments(Line) ->
case gleam@string:split_once(Line, <<"#"/utf8>>) of
{error, nil} ->
Line;
{ok, {Cleaned, _}} ->
gleam@string:trim(Cleaned)
end.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 149).
-spec remove_empty(list(binary()), list(binary())) -> list(binary()).
remove_empty(Lines, Acc) ->
case Lines of
[] ->
Acc;
[Line | Rest] ->
case Line of
<<""/utf8>> ->
remove_empty(Rest, Acc);
_ ->
remove_empty(Rest, [Line | Acc])
end
end.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 160).
-spec parse_config_lines(list(binary()), list({binary(), binary()})) -> {ok,
gleam@dict:dict(binary(), binary())} |
{error, env_file_error()}.
parse_config_lines(Lines, Acc) ->
case Lines of
[] ->
{ok, maps:from_list(Acc)};
[Line | Rest] ->
case gleam@string:split_once(Line, <<"="/utf8>>) of
{error, nil} ->
{error, bad_config};
{ok, L} ->
parse_config_lines(Rest, [L | Acc])
end
end.
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 132).
-spec parse_dotenv_file(binary()) -> {ok, gleam@dict:dict(binary(), binary())} |
{error, env_file_error()}.
parse_dotenv_file(Content) ->
_pipe = Content,
_pipe@1 = gleam@string:trim(_pipe),
_pipe@2 = gleam@string:replace(_pipe@1, <<"\r\n"/utf8>>, <<"\n"/utf8>>),
_pipe@3 = gleam@string:split(_pipe@2, <<"\n"/utf8>>),
_pipe@4 = gleam@list:map(_pipe@3, fun remove_comments/1),
_pipe@5 = remove_empty(_pipe@4, []),
parse_config_lines(_pipe@5, []).
-file("/home/pierre/Programming/gleam/dotenv_conf/src/dotenv_conf.gleam", 118).
-spec read_file(
binary(),
fun(({ok, gleam@dict:dict(binary(), binary())} | {error, env_file_error()}) -> FQZ)
) -> FQZ.
read_file(Path, Then) ->
File = case simplifile_erl:is_file(Path) of
{error, _} ->
{error, missing_file};
{ok, false} ->
{error, invalid_file};
_ ->
case simplifile:read(Path) of
{ok, Content} ->
parse_dotenv_file(Content);
{error, _} ->
{error, file_error}
end
end,
Then(File).