diff --git a/lib/stow/config.ex b/lib/stow/config.ex new file mode 100644 index 0000000..60620b8 --- /dev/null +++ b/lib/stow/config.ex @@ -0,0 +1,24 @@ +defmodule Stow.Config do + @moduledoc false + alias Stow.Http.Client + + @default_base_dir "./stow_data" + @base_dir Application.compile_env(:stow, :base_dir) + @file_io Application.compile_env(:stow, :file_io, Elixir.File) + + def base_dir(opts \\ []) do + Keyword.get(opts, :base_dir, @base_dir) || + System.get_env("LB_STOW_BASE_DIR") || + default_base_dir() + end + + def default_base_dir, do: @default_base_dir + + def file_io, do: @file_io + + def http_client(client \\ "httpc") do + Application.get_env(:stow, :http_client, runtime_client(client)) + end + + defp runtime_client(client), do: client |> Macro.camelize() |> then(&Module.concat(Client, &1)) +end diff --git a/lib/stow/http/client.ex b/lib/stow/http/client.ex index 286fd51..2d1c987 100644 --- a/lib/stow/http/client.ex +++ b/lib/stow/http/client.ex @@ -3,8 +3,6 @@ defmodule Stow.Http.Client do HTTP client behaviour based on `t:Plug.Conn.t/0`. """ - alias Stow.Http.Client - @type client_options :: keyword() @type conn :: Plug.Conn.t() @@ -34,10 +32,6 @@ defmodule Stow.Http.Client do end @doc false - @spec impl() :: module() - def impl(client \\ "httpc") do - Application.get_env(:stow, :http_client, runtime_client(client)) - end - - defp runtime_client(client), do: client |> Macro.camelize() |> then(&Module.concat(Client, &1)) + defdelegate impl, to: Stow.Config, as: :http_client + defdelegate impl(client), to: Stow.Config, as: :http_client end diff --git a/lib/stow/pipeline.ex b/lib/stow/pipeline.ex index e1668e2..ce3a1fb 100644 --- a/lib/stow/pipeline.ex +++ b/lib/stow/pipeline.ex @@ -1,9 +1,6 @@ defmodule Stow.Pipeline do @moduledoc false - @default_base_dir "./stow_data" - @base_dir Application.compile_env(:stow, :base_dir) - defstruct [:source, :sink] @type t :: %__MODULE__{ @@ -28,13 +25,4 @@ defmodule Stow.Pipeline do @spec conn() :: Plug.Conn.t() def conn(), do: %Plug.Conn{owner: self(), remote_ip: {127, 0, 0, 1}} - - @doc false - def base_dir(opts \\ []) do - Keyword.get(opts, :base_dir, @base_dir) || - System.get_env("LB_STOW_BASE_DIR") || - default_base_dir() - end - - def default_base_dir, do: @default_base_dir end diff --git a/lib/stow/sink/file_sink.ex b/lib/stow/sink/file_sink.ex index 28b4d25..a6b547e 100644 --- a/lib/stow/sink/file_sink.ex +++ b/lib/stow/sink/file_sink.ex @@ -11,9 +11,8 @@ defmodule Stow.Sink.FileSink do @behaviour Stow.Sink - import Stow.Pipeline, only: [base_dir: 1] + import Stow.Config, only: [base_dir: 1, file_io: 0] - @file_io Application.compile_env(:stow, :file_io, Elixir.File) @options [:base_dir, :modes, :file_io] @doc """ @@ -42,15 +41,15 @@ defmodule Stow.Sink.FileSink do defp maybe_create_dir(path) do dir = path |> Path.dirname() - case dir |> @file_io.exists?() do + case dir |> file_io().exists?() do true -> :ok - false -> @file_io.mkdir_p(dir) + false -> file_io().mkdir_p(dir) end end defp write_file(path, data, opts) do file_modes = Keyword.get(opts, :modes, []) - Keyword.get(opts, :file_io, @file_io).write(path, data, file_modes) + Keyword.get(opts, :file_io, file_io()).write(path, data, file_modes) end @doc """ @@ -65,7 +64,7 @@ defmodule Stow.Sink.FileSink do """ @impl true def delete(%URI{scheme: "file", host: nil, path: path}, opts) when not is_nil(path) do - case [validate_opts(opts) |> base_dir(), path] |> @file_io.rm() do + case [validate_opts(opts) |> base_dir(), path] |> file_io().rm() do :ok -> :ok {:error, reason} -> {:error, reason} end diff --git a/mix.lock b/mix.lock index 07b5a5b..cf4abe8 100644 --- a/mix.lock +++ b/mix.lock @@ -5,7 +5,7 @@ "cowboy": {:hex, :cowboy, "2.10.0", "ff9ffeff91dae4ae270dd975642997afe2a1179d94b1887863e43f681a203e26", [:make, :rebar3], [{:cowlib, "2.12.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "3afdccb7183cc6f143cb14d3cf51fa00e53db9ec80cdcd525482f5e99bc41d6b"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, "cowlib": {:hex, :cowlib, "2.12.1", "a9fa9a625f1d2025fe6b462cb865881329b5caff8f1854d1cbc9f9533f00e1e1", [:make, :rebar3], [], "hexpm", "163b73f6367a7341b33c794c4e88e7dbfe6498ac42dcd69ef44c5bc5507c8db0"}, - "credo": {:hex, :credo, "1.7.4", "68ca5cf89071511c12fd9919eb84e388d231121988f6932756596195ccf7fd35", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "9cf776d062c78bbe0f0de1ecaee183f18f2c3ec591326107989b054b7dddefc2"}, + "credo": {:hex, :credo, "1.7.5", "643213503b1c766ec0496d828c90c424471ea54da77c8a168c725686377b9545", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f799e9b5cd1891577d8c773d245668aa74a2fcd15eb277f51a0131690ebfb3fd"}, "dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, "file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"}, diff --git a/test/stow/plug/sink_test.exs b/test/stow/plug/sink_test.exs index 7e17da9..ecb0b93 100644 --- a/test/stow/plug/sink_test.exs +++ b/test/stow/plug/sink_test.exs @@ -3,7 +3,7 @@ defmodule Stow.Plug.SinkTest do use Plug.Test import Hammox - import Stow.Pipeline, only: [base_dir: 0] + import Stow.Config, only: [base_dir: 0] import Stow.Plug.Utils, only: [update_private: 3] alias Plug.Conn