Bash.Filesystem behaviour (Bash v0.6.0)

Copy Markdown View Source

Behaviour and dispatcher for pluggable filesystem implementations.

The filesystem is stored in session state as a {module, config} tuple. Every filesystem callback takes config as the first argument. This module provides convenience functions that unwrap the tuple and dispatch to the appropriate implementation.

Default

When no filesystem option is provided, sessions use {Bash.Filesystem.LocalDisk, nil} which passes through to Elixir's File and Erlang's :file modules.

Custom Implementations

Implement the Bash.Filesystem behaviour to provide a virtual filesystem:

defmodule MyVFS do
  @behaviour Bash.Filesystem

  @impl true
  def exists?(config, path), do: ...
  # ... implement all required callbacks
end

{:ok, session} = Bash.Session.new(filesystem: {MyVFS, my_config})

Summary

Types

t()

@type t() :: {module(), config :: term()}

Callbacks

dir?(config, path)

@callback dir?(config :: term(), path :: String.t()) :: boolean()

exists?(config, path)

@callback exists?(config :: term(), path :: String.t()) :: boolean()

handle_close(config, io_device)

@callback handle_close(config :: term(), io_device :: term()) :: :ok | {:error, term()}

handle_write(config, io_device, data)

@callback handle_write(config :: term(), io_device :: term(), data :: iodata()) ::
  :ok | {:error, term()}

ls(config, path)

@callback ls(config :: term(), path :: String.t()) ::
  {:ok, [String.t()]} | {:error, term()}

lstat(config, path)

(optional)
@callback lstat(config :: term(), path :: String.t()) ::
  {:ok, File.Stat.t()} | {:error, term()}

mkdir_p(config, path)

@callback mkdir_p(config :: term(), path :: String.t()) :: :ok | {:error, term()}

open(config, path, modes)

@callback open(config :: term(), path :: String.t(), modes :: [atom()]) ::
  {:ok, io_device :: term()} | {:error, term()}

read(config, path)

@callback read(config :: term(), path :: String.t()) :: {:ok, binary()} | {:error, term()}

read_link(config, path)

(optional)
@callback read_link(config :: term(), path :: String.t()) ::
  {:ok, String.t()} | {:error, term()}

regular?(config, path)

@callback regular?(config :: term(), path :: String.t()) :: boolean()

rm(config, path)

@callback rm(config :: term(), path :: String.t()) :: :ok | {:error, term()}

stat(config, path)

@callback stat(config :: term(), path :: String.t()) ::
  {:ok, File.Stat.t()} | {:error, term()}

wildcard(config, pattern, opts)

@callback wildcard(config :: term(), pattern :: String.t(), opts :: keyword()) :: [
  String.t()
]

write(config, path, content, opts)

@callback write(
  config :: term(),
  path :: String.t(),
  content :: iodata(),
  opts :: keyword()
) ::
  :ok | {:error, term()}

Functions

dir?(arg, path)

@spec dir?(t(), String.t()) :: boolean()

exists?(arg, path)

@spec exists?(t(), String.t()) :: boolean()

from_state(arg1)

@spec from_state(map()) :: t()

handle_close(arg, device)

@spec handle_close(t(), term()) :: :ok | {:error, term()}

handle_write(arg, device, data)

@spec handle_write(t(), term(), iodata()) :: :ok | {:error, term()}

ls(arg, path)

@spec ls(t(), String.t()) :: {:ok, [String.t()]} | {:error, term()}

lstat(arg, path)

@spec lstat(t(), String.t()) :: {:ok, File.Stat.t()} | {:error, term()}

mkdir_p(arg, path)

@spec mkdir_p(t(), String.t()) :: :ok | {:error, term()}

open(arg, path, modes)

@spec open(t(), String.t(), [atom()]) :: {:ok, term()} | {:error, term()}

read(arg, path)

@spec read(t(), String.t()) :: {:ok, binary()} | {:error, term()}

read_link(arg, path)

@spec read_link(t(), String.t()) :: {:ok, String.t()} | {:error, term()}

regular?(arg, path)

@spec regular?(t(), String.t()) :: boolean()

rm(arg, path)

@spec rm(t(), String.t()) :: :ok | {:error, term()}

stat(arg, path)

@spec stat(t(), String.t()) :: {:ok, File.Stat.t()} | {:error, term()}

wildcard(arg, pattern, opts)

@spec wildcard(t(), String.t(), keyword()) :: [String.t()]

write(arg, path, content, opts)

@spec write(t(), String.t(), iodata(), keyword()) :: :ok | {:error, term()}