Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QoL fixes #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions lib/envar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ defmodule Envar do

"""

require Logger

@doc """
`get/2` gets an environment variable by name
with an _optional_ second argument `default_value`
Expand All @@ -26,13 +24,7 @@ defmodule Envar do
"""
@spec get(binary, binary | nil) :: binary | nil
def get(varname, default \\ nil) do
val = System.get_env(varname, default)

if is_nil(val) do
Logger.error("ERROR: #{varname} Environment Variable is not set")
end

val
System.get_env(varname, default)
end

@doc """
Expand All @@ -54,7 +46,6 @@ defmodule Envar do
def is_set?(varname) do
case System.get_env(varname) do
nil ->
Logger.debug("#{varname} Environment Variable is not set")
false

_ ->
Expand Down Expand Up @@ -165,7 +156,6 @@ defmodule Envar do
if File.exists?(path) do
load(filename)
else
Logger.error("Required .env file does not exist at path: #{path}")
:error
end
end
Expand Down Expand Up @@ -196,22 +186,18 @@ defmodule Envar do

"""
@spec read(binary) :: map
def read(filename) do
path = Path.join(File.cwd!(), filename)

Logger.debug(".env file path: #{path}")

def read(path) do
data = File.read!(path)

data
|> String.trim()
|> String.split("\n")
|> Enum.reject(& &1 == "")
|> Enum.reduce(%{}, fn line, acc ->
line = String.trim(line)

with line <- String.replace(line, ["export ", "'"], ""),
[key | rest] <- String.split(line, "="),
value <- Enum.join(rest, "=") do
[key, value] <- :binary.split(line, "=") do
if String.length(value) > 0 do
Map.put(acc, key, value)
else
Expand Down