Skip to content

Commit

Permalink
consolidate all config concerns in Stow.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
boonious committed Mar 11, 2024
1 parent c27d8f3 commit 0cf9a29
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
24 changes: 24 additions & 0 deletions lib/stow/config.ex
Original file line number Diff line number Diff line change
@@ -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
10 changes: 2 additions & 8 deletions lib/stow/http/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
12 changes: 0 additions & 12 deletions lib/stow/pipeline.ex
Original file line number Diff line number Diff line change
@@ -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__{
Expand All @@ -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
11 changes: 5 additions & 6 deletions lib/stow/sink/file_sink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down Expand Up @@ -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 """
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion test/stow/plug/sink_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0cf9a29

Please sign in to comment.