Tmp (Tmp v0.1.3) View Source

Temporary directories that are monitored and automatically removed.

Link to this section Summary

Functions

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

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.

To keep the temporary directory for debugging call the function passed as the second argument to 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"

  • :dirname - The name of the temporary directory. Defaults to a random Base16 uid.

  • :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 ->
...>   Path.join(tmp_dir_path, "my_new_file") |> File.touch()
...>   1 + 1
...> end)
2

To keep the temporary directory for debugging:

iex> my_file = Tmp.dir(fn tmp_dir_path, keep ->
...>   file_path = Path.join(tmp_dir_path, "my_new_file")
...>   File.touch(file_path)
...>   keep.()
...>   file_path
...> end)
...> File.exists?(my_file)
true