Skip to content

Commit 0ed6fed

Browse files
author
Kenneth Cross
committed
bumped elixir version and added formatter
1 parent 641429e commit 0ed6fed

File tree

7 files changed

+120
-90
lines changed

7 files changed

+120
-90
lines changed

.formatter.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[inputs: ["mix.exs", "{web,config,lib,test}/**/*.{ex,exs}"]]

lib/briefly.ex

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ defmodule Briefly do
99
end
1010

1111
@type create_opts :: [
12-
{:prefix, binary},
13-
{:extname, binary},
14-
{:directory, boolean}
15-
]
12+
{:prefix, binary},
13+
{:extname, binary},
14+
{:directory, boolean}
15+
]
1616

1717
@doc """
1818
Requests a temporary file to be created with the given options
1919
"""
2020
@spec create(create_opts) ::
21-
{:ok, binary} |
22-
{:too_many_attempts, binary, pos_integer} |
23-
{:no_tmp, [binary]}
21+
{:ok, binary}
22+
| {:too_many_attempts, binary, pos_integer}
23+
| {:no_tmp, [binary]}
2424
def create(opts \\ []) do
25-
GenServer.call(Briefly.Entry.server, {:create, opts})
25+
GenServer.call(Briefly.Entry.server(), {:create, opts})
2626
end
2727

2828
@doc """
@@ -34,8 +34,10 @@ defmodule Briefly do
3434
case create(opts) do
3535
{:ok, path} ->
3636
path
37+
3738
{:too_many_attempts, tmp, attempts} ->
3839
raise "tried #{attempts} times to create a temporary file at #{tmp} but failed. What gives?"
40+
3941
{:no_tmp, _tmps} ->
4042
raise "could not create a tmp directory to store temporary files. Set the :briefly :directory application setting to a directory with write permission"
4143
end
@@ -47,6 +49,6 @@ defmodule Briefly do
4749
"""
4850
@spec cleanup :: [binary]
4951
def cleanup do
50-
GenServer.call(Briefly.Entry.server, :cleanup)
52+
GenServer.call(Briefly.Entry.server(), :cleanup)
5153
end
5254
end

lib/briefly/config.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ defmodule Briefly.Config do
99

1010
defp get(key) do
1111
Application.get_env(:briefly, key, [])
12-
|> List.wrap
12+
|> List.wrap()
1313
|> Enum.find_value(&runtime_value/1)
1414
end
1515

1616
defp runtime_value({:system, env_key}) do
1717
System.get_env(env_key)
1818
end
19-
defp runtime_value(value), do: value
2019

20+
defp runtime_value(value), do: value
2121
end

lib/briefly/entry.ex

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,27 @@ defmodule Briefly.Entry do
99
use GenServer
1010

1111
def start_link() do
12-
GenServer.start_link(__MODULE__, :ok, [name: __MODULE__])
12+
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
1313
end
1414

1515
## Callbacks
1616

1717
@max_attempts 10
1818

1919
def init(:ok) do
20-
tmp = Briefly.Config.directory
21-
cwd = Path.join(File.cwd!, "tmp")
20+
tmp = Briefly.Config.directory()
21+
cwd = Path.join(File.cwd!(), "tmp")
2222
ets = :ets.new(:briefly, [:private])
2323
{:ok, {[tmp, cwd], ets}}
2424
end
2525

2626
def handle_call({:create, opts}, {pid, _ref}, {tmps, ets} = state) do
2727
options = opts |> Enum.into(%{})
28+
2829
case find_tmp_dir(pid, tmps, ets) do
2930
{:ok, tmp, paths} ->
3031
{:reply, open(options, tmp, 0, pid, ets, paths), state}
32+
3133
{:no_tmp, _} = error ->
3234
{:reply, error, state}
3335
end
@@ -53,6 +55,7 @@ defmodule Briefly.Entry do
5355
case :ets.lookup(ets, pid) do
5456
[{^pid, tmp, paths}] ->
5557
{:ok, tmp, paths}
58+
5659
[] ->
5760
if tmp = ensure_tmp_dir(tmps) do
5861
:erlang.monitor(:process, pid)
@@ -65,7 +68,7 @@ defmodule Briefly.Entry do
6568
end
6669

6770
defp ensure_tmp_dir(tmps) do
68-
{mega, _, _} = :os.timestamp
71+
{mega, _, _} = :os.timestamp()
6972
subdir = "/briefly-" <> i(mega)
7073
Enum.find_value(tmps, &write_tmp_dir(&1 <> subdir))
7174
end
@@ -77,13 +80,15 @@ defmodule Briefly.Entry do
7780
end
7881
end
7982

80-
defp open(%{directory: true} = options, tmp, attempts, pid, ets, paths) when attempts < @max_attempts do
83+
defp open(%{directory: true} = options, tmp, attempts, pid, ets, paths)
84+
when attempts < @max_attempts do
8185
path = path(options, tmp)
8286

8387
case File.mkdir_p(path) do
8488
:ok ->
85-
:ets.update_element(ets, pid, {3, [path|paths]})
89+
:ets.update_element(ets, pid, {3, [path | paths]})
8690
{:ok, path}
91+
8792
{:error, reason} when reason in [:eexist, :eacces] ->
8893
open(options, tmp, attempts + 1, pid, ets, paths)
8994
end
@@ -94,8 +99,9 @@ defmodule Briefly.Entry do
9499

95100
case :file.write_file(path, "", [:write, :raw, :exclusive, :binary]) do
96101
:ok ->
97-
:ets.update_element(ets, pid, {3, [path|paths]})
102+
:ets.update_element(ets, pid, {3, [path | paths]})
98103
{:ok, path}
104+
99105
{:error, reason} when reason in [:eexist, :eacces] ->
100106
open(options, tmp, attempts + 1, pid, ets, paths)
101107
end
@@ -109,27 +115,38 @@ defmodule Briefly.Entry do
109115
defp i(integer), do: Integer.to_string(integer)
110116

111117
defp path(options, tmp) do
112-
{_mega, sec, micro} = :os.timestamp
118+
{_mega, sec, micro} = :os.timestamp()
113119
scheduler_id = :erlang.system_info(:scheduler_id)
114-
IO.iodata_to_binary([tmp, "/",
115-
prefix(options),
116-
"-", i(sec), "-", i(micro), "-", i(scheduler_id),
117-
extname(options)])
120+
121+
IO.iodata_to_binary([
122+
tmp,
123+
"/",
124+
prefix(options),
125+
"-",
126+
i(sec),
127+
"-",
128+
i(micro),
129+
"-",
130+
i(scheduler_id),
131+
extname(options)
132+
])
118133
end
119134

120135
defp prefix(%{prefix: value}), do: value
121-
defp prefix(_), do: Briefly.Config.default_prefix
136+
defp prefix(_), do: Briefly.Config.default_prefix()
122137

123138
defp extname(%{extname: value}), do: value
124-
defp extname(_), do: Briefly.Config.default_extname
139+
defp extname(_), do: Briefly.Config.default_extname()
125140

126141
defp cleanup(ets, pid) do
127142
case :ets.lookup(ets, pid) do
128143
[{^pid, _tmp, paths}] ->
129144
:ets.delete(ets, pid)
130-
Enum.each paths, &File.rm_rf/1
145+
Enum.each(paths, &File.rm_rf/1)
131146
paths
132-
[] -> []
147+
148+
[] ->
149+
[]
133150
end
134151
end
135152
end

lib/briefly/supervisor.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ defmodule Briefly.Supervisor do
1111
children = [
1212
worker(Briefly.Entry, [])
1313
]
14+
1415
supervise(children, strategy: :one_for_one)
1516
end
1617
end

mix.exs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ defmodule Briefly.Mixfile do
22
use Mix.Project
33

44
def project do
5-
[app: :briefly,
6-
version: "0.3.1",
7-
elixir: "~> 1.3",
8-
source_url: "https://github.com/CargoSense/briefly",
9-
build_embedded: Mix.env == :prod,
10-
start_permanent: Mix.env == :prod,
11-
package: package(),
12-
deps: deps()]
5+
[
6+
app: :briefly,
7+
version: "0.3.1",
8+
elixir: "~> 1.6",
9+
source_url: "https://github.com/CargoSense/briefly",
10+
build_embedded: Mix.env() == :prod,
11+
start_permanent: Mix.env() == :prod,
12+
package: package(),
13+
deps: deps()
14+
]
1315
end
1416

1517
# Configuration for the OTP application
1618
#
1719
# Type `mix help compile.app` for more information
1820
def application do
19-
[applications: [:logger],
20-
mod: {Briefly, []},
21-
env: default_env()]
21+
[applications: [:logger], mod: {Briefly, []}, env: default_env()]
2222
end
2323

2424
# Dependencies can be Hex packages:
@@ -32,22 +32,25 @@ defmodule Briefly.Mixfile do
3232
# Type `mix help deps` for more examples and options
3333
defp deps do
3434
[
35-
{:ex_doc, "~> 0.8", only: :dev}
36-
]
35+
{:ex_doc, "~> 0.8", only: :dev}
36+
]
3737
end
3838

3939
defp package do
40-
[description: "Simple, robust temporary file support",
41-
files: ["lib", "config", "mix.exs", "README*", "LICENSE"],
42-
contributors: ["Bruce Williams"],
43-
licenses: ["Apache 2"],
44-
links: %{github: "https://github.com/CargoSense/briefly"}]
40+
[
41+
description: "Simple, robust temporary file support",
42+
files: ["lib", "config", "mix.exs", "README*", "LICENSE"],
43+
contributors: ["Bruce Williams"],
44+
licenses: ["Apache 2"],
45+
links: %{github: "https://github.com/CargoSense/briefly"}
46+
]
4547
end
4648

4749
defp default_env do
48-
[directory: [{:system, "TMPDIR"}, {:system, "TMP"}, {:system, "TEMP"}, "/tmp"],
49-
default_prefix: "briefly",
50-
default_extname: ""]
50+
[
51+
directory: [{:system, "TMPDIR"}, {:system, "TMP"}, {:system, "TEMP"}, "/tmp"],
52+
default_prefix: "briefly",
53+
default_extname: ""
54+
]
5155
end
52-
5356
end

0 commit comments

Comments
 (0)