From bd19bcd198b4348c23e56c54abbefca3c571ca8c Mon Sep 17 00:00:00 2001 From: mad Date: Wed, 9 Jun 2021 03:13:08 +0000 Subject: [PATCH 1/2] add: config sub_directory --- README.md | 1 + lib/briefly/config.ex | 2 ++ lib/briefly/entry.ex | 6 ++++-- mix.exs | 1 + 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d06533c..c344e17 100644 --- a/README.md +++ b/README.md @@ -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: "" ``` diff --git a/lib/briefly/config.ex b/lib/briefly/config.ex index 7b7d0d2..3724e37 100644 --- a/lib/briefly/config.ex +++ b/lib/briefly/config.ex @@ -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) diff --git a/lib/briefly/entry.ex b/lib/briefly/entry.ex index 211d717..c00b1f8 100644 --- a/lib/briefly/entry.ex +++ b/lib/briefly/entry.ex @@ -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 @@ -68,7 +69,8 @@ defmodule Briefly.Entry do defp ensure_tmp_dir(tmps) do {mega, _, _} = :os.timestamp() - subdir = "briefly-" <> i(mega) + [_tmp, _cwd, sub] = tmps + subdir = sub <> "-" <> i(mega) Enum.find_value(tmps, &write_tmp_dir(&1 <> subdir)) end diff --git a/mix.exs b/mix.exs index 6cfbdf7..6279ca9 100644 --- a/mix.exs +++ b/mix.exs @@ -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: "" ] From c4b8a9f35ac79cd27a29279a2d7629b42efe717b Mon Sep 17 00:00:00 2001 From: mad Date: Wed, 9 Jun 2021 14:36:22 +0000 Subject: [PATCH 2/2] remove: i/1 --- lib/briefly/entry.ex | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/briefly/entry.ex b/lib/briefly/entry.ex index c00b1f8..8b27e86 100644 --- a/lib/briefly/entry.ex +++ b/lib/briefly/entry.ex @@ -70,8 +70,8 @@ defmodule Briefly.Entry do defp ensure_tmp_dir(tmps) do {mega, _, _} = :os.timestamp() [_tmp, _cwd, sub] = tmps - subdir = sub <> "-" <> i(mega) - Enum.find_value(tmps, &write_tmp_dir(&1 <> subdir)) + subdir = "#{sub}-#{mega}" + Enum.find_value(tmps, &write_tmp_dir(Path.join(&1, subdir))) end defp write_tmp_dir(path) do @@ -112,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("-")