Current section
Files
Jump to
Current section
Files
lib/jacob/files.ex
defmodule Jacob.Files do
@moduledoc """
Manages the dot directory.
i.e. ~/.jacob
Every jacob application may need to store application related
files. Following other cli's conventions we will put them in
a dot directory in the user's home directory.
The dot directory name is infered by the cli app name therefore a
jacob app called `rabbi` will see it's dot directory stored at
`~/.rabbi`.
"""
@doc """
Write some file in the dot directory.
If the file already exists it will be overwritten.
If the dot directory does not exits it will be created.
Returns what `File.write/2` would return.
"""
def save(file_name, content) do
unless dot_directory_exists?(), do: create_dot_directory()
file_name
|> dot_directory_path()
|> File.write(content)
end
@doc """
Read from the dot directory.
Returns what `File.read/1` would return.
"""
def read(file_name) do
file_name
|> dot_directory_path()
|> File.read()
end
@doc """
Return the dot directory's full path.
e.g. /home/username/.jacob/
"""
def dot_directory_path do
Path.expand("~/.#{Jacob.escript_name()}/")
end
@doc """
Join the given path to the dot directory path.
"""
def dot_directory_path(path_to_join) do
dot_directory_path()
|> Path.join(path_to_join)
end
defp dot_directory_exists? do
dot_directory_path()
|> File.dir?()
end
defp create_dot_directory do
dot_directory_path()
|> File.mkdir!()
end
end