Current section
Files
Jump to
Current section
Files
src/glm_vault@vault.erl
-module(glm_vault@vault).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/glm_vault/vault.gleam").
-export([new_vault/1, decrypt/2, encrypt/3, get_int/2, set_int/3, get_float/2, set_float/3, get_bool/2, set_bool/3, get_string/2, set_string/3]).
-export_type([vault/0, vault_error/0]).
-opaque vault() :: {vault, gleam@dict:dict(binary(), tom:toml())}.
-type vault_error() :: {unable_to_decrypt_file,
glm_encrypted_file@encfile:enc_file_error(),
glm_encrypted_file@encfile:encrypted_file(),
glm_encrypted_file@encfile:password_file()} |
{unable_to_parse_decrypted_toml,
tom:parse_error(),
glm_encrypted_file@encfile:encrypted_file(),
glm_encrypted_file@encfile:password_file()} |
{unable_to_serialize_item_get_error, nil, binary()} |
{unable_to_serialize_unsupportd_type, tom:toml()} |
{unable_to_get_int_from_vault, tom:get_error()} |
{unable_to_get_float_from_vault, tom:get_error()} |
{unable_to_get_bool_from_vault, tom:get_error()} |
{unable_to_get_string_from_vault, tom:get_error()} |
{unable_to_encrypt_file, glm_encrypted_file@encfile:encrypted_file()} |
{unable_to_write_password, simplifile:file_error()}.
-file("src/glm_vault/vault.gleam", 19).
-spec new_vault(gleam@dict:dict(binary(), tom:toml())) -> vault().
new_vault(Secrets) ->
{vault, Secrets}.
-file("src/glm_vault/vault.gleam", 44).
-spec decrypt(
glm_encrypted_file@encfile:encrypted_file(),
glm_encrypted_file@encfile:password_file()
) -> {ok, vault()} | {error, vault_error()}.
decrypt(Encrypted_file, Password_file) ->
gleam@result:'try'(
begin
_pipe = glm_encrypted_file@encfile:decrypt(
Encrypted_file,
Password_file
),
_pipe@1 = gleam@result:map(
_pipe,
fun(Plaintext) ->
logging:log(
debug,
<<<<<<"decrypted encrypted file: "/utf8,
(erlang:element(2, Encrypted_file))/binary>>/binary,
", using password file: "/utf8>>/binary,
(erlang:element(2, Password_file))/binary>>
),
Plaintext
end
),
gleam@result:map_error(
_pipe@1,
fun(_capture) ->
{unable_to_decrypt_file,
_capture,
Encrypted_file,
Password_file}
end
)
end,
fun(Plaintext@1) -> _pipe@2 = Plaintext@1,
_pipe@3 = tom:parse(_pipe@2),
_pipe@4 = gleam@result:map_error(
_pipe@3,
fun(_capture@1) ->
{unable_to_parse_decrypted_toml,
_capture@1,
Encrypted_file,
Password_file}
end
),
gleam@result:map(
_pipe@4,
fun(Secrets) ->
logging:log(
debug,
<<<<<<"parsed encrypted file: "/utf8,
(erlang:element(2, Encrypted_file))/binary>>/binary,
", using password file: "/utf8>>/binary,
(erlang:element(2, Password_file))/binary>>
),
{vault, Secrets}
end
) end
).
-file("src/glm_vault/vault.gleam", 90).
-spec serialize_item(binary(), {ok, tom:toml()} | {error, nil}) -> {ok,
binary()} |
{error, vault_error()}.
serialize_item(Key, Value) ->
case Value of
{error, E} ->
{error, {unable_to_serialize_item_get_error, E, Key}};
{ok, Found} ->
case Found of
{int, X} ->
{ok,
<<<<Key/binary, " = "/utf8>>/binary,
(erlang:integer_to_binary(X))/binary>>};
{string, X@1} ->
{ok, <<<<Key/binary, " = "/utf8>>/binary, X@1/binary>>};
{bool, X@2} ->
{ok,
<<<<Key/binary, " = "/utf8>>/binary,
(begin
_pipe = X@2,
gleam@bool:to_string(_pipe)
end)/binary>>};
{float, X@3} ->
{ok,
<<<<Key/binary, " = "/utf8>>/binary,
(begin
_pipe@1 = X@3,
gleam_stdlib:float_to_string(_pipe@1)
end)/binary>>};
T ->
{error, {unable_to_serialize_unsupportd_type, T}}
end
end.
-file("src/glm_vault/vault.gleam", 83).
-spec serialize(gleam@dict:dict(binary(), tom:toml())) -> {ok, list(binary())} |
{error, vault_error()}.
serialize(D) ->
_pipe = maps:keys(D),
_pipe@1 = gleam@list:map(
_pipe,
fun(Key) -> serialize_item(Key, gleam_stdlib:map_get(D, Key)) end
),
gleam@result:all(_pipe@1).
-file("src/glm_vault/vault.gleam", 115).
-spec encrypt(
vault(),
glm_encrypted_file@encfile:encrypted_file(),
glm_encrypted_file@encfile:password_file()
) -> {ok, nil} | {error, vault_error()}.
encrypt(Vault, Encrypted_file, Password_file) ->
Result = begin
temporary:create(
temporary:file(),
fun(Temp_file) ->
logging:log(
debug,
<<"opened temp file to write plaintext to: "/utf8,
Temp_file/binary>>
),
_ = begin
_pipe = erlang:element(2, Vault),
_pipe@1 = serialize(_pipe),
_pipe@2 = gleam@result:map(
_pipe@1,
fun(_capture) ->
gleam@string:join(_capture, <<"\n"/utf8>>)
end
),
_pipe@3 = gleam@result:map(
_pipe@2,
fun(_capture@1) ->
simplifile:write(Temp_file, _capture@1)
end
),
gleam@result:map(
_pipe@3,
fun(_) ->
_ = glm_encrypted_file@encfile:encrypt(
glm_encrypted_file@encfile:new_plaintext_file(
Temp_file
),
Encrypted_file,
Password_file
),
logging:log(
debug,
<<"encrypted plaintext and wrote to: "/utf8,
(erlang:element(2, Encrypted_file))/binary>>
)
end
)
end,
nil
end
)
end,
case Result of
{ok, Vault_result} ->
{ok, Vault_result};
{error, _} ->
{error, {unable_to_encrypt_file, Encrypted_file}}
end.
-file("src/glm_vault/vault.gleam", 154).
-spec get_int(vault(), binary()) -> {ok, integer()} | {error, vault_error()}.
get_int(V, Key) ->
_pipe = tom:get_int(erlang:element(2, V), [Key]),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {unable_to_get_int_from_vault, Field@0} end
).
-file("src/glm_vault/vault.gleam", 159).
-spec set_int(vault(), binary(), integer()) -> vault().
set_int(V, Key, Value) ->
Secrets = gleam@dict:insert(erlang:element(2, V), Key, {int, Value}),
{vault, Secrets}.
-file("src/glm_vault/vault.gleam", 164).
-spec get_float(vault(), binary()) -> {ok, float()} | {error, vault_error()}.
get_float(V, Key) ->
_pipe = tom:get_float(erlang:element(2, V), [Key]),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {unable_to_get_float_from_vault, Field@0} end
).
-file("src/glm_vault/vault.gleam", 169).
-spec set_float(vault(), binary(), float()) -> vault().
set_float(V, Key, Value) ->
Secrets = gleam@dict:insert(erlang:element(2, V), Key, {float, Value}),
{vault, Secrets}.
-file("src/glm_vault/vault.gleam", 174).
-spec get_bool(vault(), binary()) -> {ok, boolean()} | {error, vault_error()}.
get_bool(V, Key) ->
_pipe = tom:get_bool(erlang:element(2, V), [Key]),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {unable_to_get_bool_from_vault, Field@0} end
).
-file("src/glm_vault/vault.gleam", 179).
-spec set_bool(vault(), binary(), boolean()) -> vault().
set_bool(V, Key, Value) ->
Secrets = gleam@dict:insert(erlang:element(2, V), Key, {bool, Value}),
{vault, Secrets}.
-file("src/glm_vault/vault.gleam", 184).
-spec get_string(vault(), binary()) -> {ok, binary()} | {error, vault_error()}.
get_string(V, Key) ->
_pipe = tom:get_string(erlang:element(2, V), [Key]),
gleam@result:map_error(
_pipe,
fun(Field@0) -> {unable_to_get_string_from_vault, Field@0} end
).
-file("src/glm_vault/vault.gleam", 189).
-spec set_string(vault(), binary(), binary()) -> vault().
set_string(V, Key, Value) ->
Secrets = gleam@dict:insert(erlang:element(2, V), Key, {string, Value}),
{vault, Secrets}.