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

add: config sub_directory_prefix #29

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ following Mix config:
```elixir
config :briefly,
directory: [{:system, "TMPDIR"}, {:system, "TMP"}, {:system, "TEMP"}, "/tmp"],
sub_directory_prefix: "briefly",
default_prefix: "briefly",
default_extname: ""
```
Expand Down
2 changes: 2 additions & 0 deletions lib/briefly/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule Briefly.Config do

def directory, do: get(:directory)

def sub_directory_prefix, do: get(:sub_directory_prefix)

def default_prefix, do: get(:default_prefix)

def default_extname, do: get(:default_extname)
Expand Down
11 changes: 5 additions & 6 deletions lib/briefly/entry.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ defmodule Briefly.Entry do

def init(_init_arg) do
tmp = Briefly.Config.directory()
sub = Briefly.Config.sub_directory_prefix()
cwd = Path.join(File.cwd!(), "tmp")
ets = :ets.new(:briefly, [:private])
{:ok, {[tmp, cwd], ets}}
{:ok, {[tmp, cwd, sub], ets}}
end

def handle_call({:create, opts}, {caller_pid, _ref}, {tmps, ets} = state) do
Expand Down Expand Up @@ -68,8 +69,9 @@ defmodule Briefly.Entry do

defp ensure_tmp_dir(tmps) do
{mega, _, _} = :os.timestamp()
subdir = "briefly-" <> i(mega)
Enum.find_value(tmps, &write_tmp_dir(&1 <> subdir))
[_tmp, _cwd, sub] = tmps
subdir = "#{sub}-#{mega}"
Enum.find_value(tmps, &write_tmp_dir(Path.join(&1, subdir)))
end

defp write_tmp_dir(path) do
Expand Down Expand Up @@ -110,9 +112,6 @@ defmodule Briefly.Entry do
{:too_many_attempts, tmp, attempts}
end

@compile {:inline, i: 1}
defp i(integer), do: Integer.to_string(integer)

defp path(options, tmp) do
time = :erlang.monotonic_time() |> to_string |> String.trim("-")

Expand Down
1 change: 1 addition & 0 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ defmodule Briefly.Mixfile do
defp default_env do
[
directory: [{:system, "TMPDIR"}, {:system, "TMP"}, {:system, "TEMP"}, "/tmp"],
sub_directory_prefix: "briefly",
default_prefix: "briefly",
default_extname: ""
]
Expand Down