Skip to content

Commit

Permalink
adds set dot env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
zemuldo committed Oct 29, 2023
1 parent f5097b8 commit abfcf03
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
13 changes: 13 additions & 0 deletions lib/ex_secrets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,24 @@ defmodule ExSecrets do
Supported for providers:
- :system_env
- :dot_env
- :azure_key_vault
- :azure_managed_identity
- :google_secret_manager
Calling this function requires the provider to be configured with credentials that allow create secrets like Secret Admionistrator in Azure Key Vault.
## Examples
iex> File.touch(".env.dev")
:ok
iex> Application.put_env(:ex_secrets, :providers, %{dot_env: %{path: ".env.dev"}})
:ok
iex> ExSecrets.set("TEST", "test", provider: :dot_env)
:ok
iex> Application.delete_env(:ex_secrets, :providers)
:ok
iex> File.rm(".env.dev")
"""

@spec set(String.t(), String.t(), Keyword.t()) :: :ok | :error
Expand Down
13 changes: 11 additions & 2 deletions lib/providers/dot_env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ defmodule ExSecrets.Providers.DotEnv do
end
end

def set(_name, _value) do
{:error, "Not implemented"}
def set(name, value) do
path = Config.provider_config_value(:dot_env, :path)

with existing when is_nil(existing) <- get(name),
true <- is_binary(path),
:ok <- File.write(path, "#{name}=#{value}\n", [:append]) do
Cache.save(name, value)
:ok
else
_ -> {:error, "Failed to write to #{path}"}
end
end

def init(_) do
Expand Down
1 change: 0 additions & 1 deletion lib/providers/system_env.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ defmodule ExSecrets.Providers.SystemEnv do

def set(name, value) do
System.put_env(name, value)
{:ok, value}
end

def process_name() do
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule ExSecrets.MixProject do
def project do
[
app: :ex_secrets,
version: "0.3.0",
version: "0.3.1",
elixir: "~> 1.13",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit abfcf03

Please sign in to comment.