Tmp (Tmp v0.2.0) View Source

Temporary directories that are monitored and automatically removed.

Examples

iex> Tmp.dir(fn tmp_dir_path ->
...>   _my_file_path = Path.join(tmp_dir_path, "my_file")
...>   # do work with my_file_path...
...>   # then return a value
...>   {:ok, :foobar}
...> end)
{:ok, :foobar}

Link to this section Summary

Functions

Creates a temporary directory and passes the path to the given function.

Keeps Tmp.Monitor from removing the temporary directory.

Link to this section Functions

Link to this function

dir(function, options \\ [])

View Source

Specs

dir(function(), list()) :: term()

Creates a temporary directory and passes the path to the given function.

function runs in a new linked GenServer process. The directory is automatically removed when the function returns or the process terminates. If you decide you want to keep the directory call Tmp.keep() in the function.

Options

  • :base_dir - The directory where :dirname is going to be created. Defaults to System.tmp_dir(). To customize the default :base_dir use config: config :tmp, :default_base_dir, "my_dir"

  • :prefix - Prefix the directory name

  • :timeout - How long the function is allowed to run before the GenServer call terminates, defaults to :infinity

Examples

iex> Tmp.dir(fn tmp_dir_path ->
...>   :ok = Path.join(tmp_dir_path, "my_new_file") |> File.touch()
...>   1 + 1
...> end)
2

Specs

keep(pid()) :: :ok

Keeps Tmp.Monitor from removing the temporary directory.

iex> path =
...>   Tmp.dir(fn tmp_dir_path ->
...>     Tmp.keep()
...>     tmp_dir_path
...>   end)
...> File.exists?(path)
true