Tmp

test

Temporary directories that are monitored and automatically removed in Elixir.

Installation

The package can be installed by adding tmp to your list of dependencies in mix.exs:

def deps do
  [
    {:tmp, "~> 0.3.0"}
  ]
end

Usage

Define your Tmp module:

defmodule MyApp.Tmp do
  use Tmp
end

# Or with a custom base directory
defmodule MyApp.CustomTmp do
  use Tmp, base_dir: "/tmp/my_dir"
end

Add it to your supervision tree:

children = [
  {MyApp.Tmp, name: MyApp.Tmp}
]

Use it in your code:

MyApp.Tmp.dir(fn tmp_dir_path ->
  file_path = Path.join(tmp_dir_path, "my_file")
  # do work with file_path...
  # then return a value
  Enum.sum([1,2,3])
end)
# => 6

Options

When calling MyApp.Tmp.dir/2, you can pass the following options:

More Examples

MyApp.Tmp.dir(fn tmp_dir_path ->
  File.touch(Path.join(tmp_dir_path, "file_one"))
  # ... other important work

  2 + 2
end, prefix: "my_app", base_dir: "/tmp/custom_base")
# => 4

Docs

Documentation can be found at https://hexdocs.pm/tmp.

License

Tmp is MIT licensed.