Skip to content

Commit

Permalink
extract config validation into its own main task
Browse files Browse the repository at this point in the history
  • Loading branch information
PhillippOhlandt committed Nov 2, 2018
1 parent 0e1cb03 commit 4276b9b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
9 changes: 8 additions & 1 deletion lib/fryse.ex
Expand Up @@ -5,7 +5,7 @@ defmodule Fryse do
Full documentation will follow soon.
"""

alias Fryse.{Indexer, ScriptLoader, Builder}
alias Fryse.{Indexer, Config, ScriptLoader, Builder}

defstruct config: nil,
data: nil,
Expand All @@ -17,6 +17,13 @@ defmodule Fryse do
Indexer.index(path)
end

def validate_config(%Fryse{config: config}) do
validate_config(config)
end
def validate_config(config) do
Config.validate(config)
end

def load_scripts(%Fryse{} = fryse) do
ScriptLoader.load_for(fryse)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/fryse/cli/build.ex
Expand Up @@ -18,6 +18,7 @@ defmodule Fryse.CLI.Build do
debug = Keyword.get(switches, :debug, false)

with {:indexing, {:ok, %Fryse{} = fryse}} <- {:indexing, Fryse.index(".")},
{:config_validation, :ok} <- {:config_validation, Fryse.validate_config(fryse)},
{:script_loading, :ok} <- {:script_loading, Fryse.load_scripts(fryse)},
{:building, {:ok, results}} <- {:building, Fryse.build(fryse)} do
if debug do
Expand All @@ -42,7 +43,7 @@ defmodule Fryse.CLI.Build do
IO.puts "- #{error}"
end
end
defp show_task_errors(:indexing, %ErrorBag{context: :config_validation, errors: errors}) do
defp show_task_errors(:config_validation, %ErrorBag{context: :validate, errors: errors}) do
IO.puts(red("Config validation failed:"))
for error <- errors do
IO.puts "- #{error}"
Expand Down
9 changes: 8 additions & 1 deletion lib/fryse/config.ex
@@ -1,6 +1,7 @@
defmodule Fryse.Config do
@moduledoc false

alias Fryse.ErrorBag
alias Fryse.Errors.MissingConfigValue
alias Fryse.Errors.InvalidConfigValue

Expand All @@ -22,7 +23,13 @@ defmodule Fryse.Config do

case errors do
[] -> :ok
errors -> {:error, errors}
errors ->
error_bag = %ErrorBag{
context: :validate,
errors: errors
}

{:error, error_bag}
end
end

Expand Down
10 changes: 1 addition & 9 deletions lib/fryse/indexer.ex
Expand Up @@ -58,17 +58,9 @@ defmodule Fryse.Indexer do
config_path = Path.join(path, "config.yml")

with {:ok, config} <- FileLoader.load_file(config_path),
merged_config <- Config.merge(config, Config.default_config()),
{:validation, :ok} <- {:validation, Config.validate(merged_config)} do
merged_config <- Config.merge(config, Config.default_config()) do

{:ok, merged_config}
else
{:validation, {:error, errors}} ->
{:error, %ErrorBag{
context: :config_validation,
errors: errors
}}
value -> value
end
end

Expand Down

0 comments on commit 4276b9b

Please sign in to comment.