api-v2/deps/tmp/README.md
2025-04-16 10:03:13 -03:00

1.4 KiB

Tmp

Actions Status

Temporary directories that are monitored and automatically removed.

Installation

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

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

Usage

Tmp.dir/2 accepts a function that will run in a new linked GenServer process. The function will be called with the path of a new temporary directory. If the function returns or the parent process exits the temporary directory is removed.

Options:

  • :base_dir defaults to System.tmp_dir()
  • :dirname defaults to a randomly generated uid
  • :timeout defaults to :infinity
Tmp.dir(fn tmp_dir_path ->
  File.touch(Path.join(tmp_dir_path, "file_one"))
  # ... other important work

  2 + 2
end, dirname: "yolo")
# => 4

To keep the temporary directory for debugging or any other reason just call the function in the optional second argument:

tmp_dir_path =
  Tmp.dir(fn tmp_dir_path, keep ->
    keep.() # calling this function will keep the temporary directory

    tmp_dir_path
  end)

# ... debugging

Config

To configure the default base dir:

config :tmp, default_base_dir: "/tmp/my_dir"

Docs

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

License

Tmp is MIT licensed.