Skip to content

Commit

Permalink
Use default path and filename instead of raise errors (#15)
Browse files Browse the repository at this point in the history
* Use default path and filename instead of raise errors

* Get compile time app name

* Correct initialization

* Some adjusts

* Remove inspect

* Unused
  • Loading branch information
sleipnir authored Feb 6, 2024
1 parent 977056b commit 7c0a979
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions lib/do_it/commfig.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,29 @@ defmodule DoIt.Commfig do
use GenServer
require Logger

@default_app_name Mix.Project.config()[:app]
|> Atom.to_string()

defmodule State do
@moduledoc false
defstruct [:dir, :file, :data]
end

def start_link([dirname, filename]) when is_nil(dirname) or is_nil(filename) do
@spec start_link([...]) :: :ignore | {:error, any()} | {:ok, pid()}
def start_link([dirname, filename] = _args) do
case {dirname, filename} do
{nil, nil} ->
raise(DoIt.InitConfigError,
message:
"dirname and filename are required for the initialization of the persistent configuration"
)

{nil, _} ->
raise(DoIt.InitConfigError,
message: "dirname is required for the initialization of the persistent configuration"
)

{_, nil} ->
raise(DoIt.InitConfigError,
message: "filename is required for the initialization of the persistent configuration"
)
end
end
do_start(System.tmp_dir(), @default_app_name)

def start_link([dirname, filename]) do
GenServer.start_link(__MODULE__, [dirname, filename], name: __MODULE__)
{nil, filename} ->
do_start(System.tmp_dir(), filename)

{dirname, nil} ->
do_start(dirname, @default_app_name)

{dirname, filename} ->
do_start(dirname, filename)
end
end

def start_link(_),
Expand All @@ -48,6 +44,10 @@ defmodule DoIt.Commfig do
"dirname and filename are required for the initialization of the persistent configuration"
)

defp do_start(dirname, filename) do
GenServer.start_link(__MODULE__, [dirname, filename], name: __MODULE__)
end

def get_dir() do
GenServer.call(__MODULE__, :get_dir)
end
Expand Down

0 comments on commit 7c0a979

Please sign in to comment.