View Source Gitly.Utils.FS (gitly v0.1.0)

A module for file system operations.

This module provides utility functions for common file system operations such as ensuring directories exist, moving files, and removing directories.

Summary

Functions

Ensures that the directory for the given path exists.

Conditionally moves a file or directory.

Conditionally removes a directory and all its contents.

Moves a file or directory from source to destination.

Checks if a path is safe to remove.

Returns the root path for Gitly.

Functions

@spec ensure_dir_exists(Path.t()) :: :ok | {:error, File.posix()}

Ensures that the directory for the given path exists.

Creates all necessary parent directories if they don't exist.

Parameters

  • path - The path for which to ensure the directory exists.

Returns

  • :ok if successful.
  • {:error, reason} if an error occurs.

Examples

iex> Gitly.Utils.FS.ensure_dir_exists("/tmp/new_dir/file.txt")
:ok
Link to this function

maybe_move(source, dest, condition)

View Source
@spec maybe_move(Path.t(), Path.t(), boolean()) ::
  {:ok, Path.t()} | {:error, String.t()}

Conditionally moves a file or directory.

Parameters

  • source - The source path.
  • dest - The destination path.
  • condition - Boolean condition determining whether to move.

Returns

  • {:ok, dest} if moved or if condition is false.
  • {:error, reason} if an error occurs during move.

Examples

iex> Gitly.Utils.FS.maybe_move("/tmp/source", "/tmp/dest", true)
{:ok, "/tmp/dest"}

iex> Gitly.Utils.FS.maybe_move("/tmp/source", "/tmp/dest", false)
{:ok, "/tmp/dest"}
Link to this function

maybe_rm_rf(path, condition)

View Source
@spec maybe_rm_rf(Path.t(), boolean()) :: {:ok, Path.t()} | {:error, String.t()}

Conditionally removes a directory and all its contents.

Parameters

  • path - The path to remove.
  • condition - Boolean condition determining whether to remove.

Returns

  • {:ok, path} if removed or if condition is false.
  • {:error, reason} if an error occurs during removal.

Examples

iex> Gitly.Utils.FS.maybe_rm_rf("/tmp/to_remove", true)
{:ok, "/tmp/to_remove"}

iex> Gitly.Utils.FS.maybe_rm_rf("/tmp/to_keep", false)
{:ok, "/tmp/to_keep"}
@spec move(Path.t(), Path.t()) :: {:ok, Path.t()} | {:error, String.t()}

Moves a file or directory from source to destination.

Parameters

  • source - The source path.
  • dest - The destination path.

Returns

  • {:ok, dest} if successful.
  • {:error, reason} if an error occurs.

Examples

iex> Gitly.Utils.FS.move("/tmp/source", "/tmp/dest")
{:ok, "/tmp/dest"}
@spec rm?(Path.t()) :: boolean()

Checks if a path is safe to remove.

Prevents removal of important system directories and non-existent paths.

Parameters

  • dest - The path to check.

Returns

  • true if the path is safe to remove.
  • false otherwise.

Examples

iex> Gitly.Utils.FS.rm?("/tmp/safe_to_remove")
true

iex> Gitly.Utils.FS.rm?("/")
false
@spec root_path() :: Path.t()

Returns the root path for Gitly.

Returns

  • The path to the Gitly root directory.

Examples

iex> Gitly.Utils.FS.root_path()
"/home/user/.gitly"