defmodule Runbox.Utils.Path do
@moduledoc group: :utilities
@moduledoc """
Utilities for handling configured paths.
"""
@doc """
Resolves a possibly relative path to absolute path by prepending the Altworx
root dir (see `set_altworx_root/1`).
## Examples
iex> Path.set_altworx_root("/opt/altworx")
:ok
iex> Path.resolve_path("/already/absolute/path")
"/already/absolute/path"
iex> Path.resolve_path("some/../relative/path")
"/opt/altworx/relative/path"
"""
def resolve_path("/" <> _ = path) do
path
end
def resolve_path(path) when is_binary(path) do
Path.expand(Path.join(get_altworx_root(), path))
end
@doc """
Sets where the Altworx root dir is. Configuration and data dirs are supposed
to be relative to the root dir. It is typically the directory which the VM is
started in.
"""
@spec set_altworx_root(String.t()) :: :ok
def set_altworx_root(path) when is_binary(path) do
Application.put_env(:runbox, altworx_root_env_var(), path)
end
@doc """
Gets the Altworx root dir.
"""
@spec get_altworx_root :: String.t() | nil
def get_altworx_root do
Application.get_env(:runbox, altworx_root_env_var())
end
def altworx_root_env_var, do: :altworx_root_dir
end