Metastatic.CLI (Metastatic v0.10.4)

View Source

Shared utilities for Metastatic CLI commands.

Provides common functionality for Mix tasks including:

  • Adapter lookup by language or file extension
  • File path handling
  • Error formatting
  • Success/error reporting

Summary

Functions

Detect language from file extension.

Print error and exit with status code 1.

Format error message for CLI output.

Format info message for CLI output.

Format success message for CLI output.

Get adapter for a given language.

Read file contents.

Write file contents, creating directories if needed.

Types

file_path()

@type file_path() :: String.t()

language()

@type language() :: atom()

Functions

detect_language(path)

@spec detect_language(file_path()) :: {:ok, language()} | {:error, String.t()}

Detect language from file extension.

Examples

iex> Metastatic.CLI.detect_language("foo.py")
{:ok, :python}

iex> Metastatic.CLI.detect_language("foo.ex")
{:ok, :elixir}

iex> Metastatic.CLI.detect_language("foo.erl")
{:ok, :erlang}

iex> Metastatic.CLI.detect_language("foo.txt")
{:error, "Cannot detect language from extension: .txt"}

fatal(message)

@spec fatal(String.t()) :: no_return()

Print error and exit with status code 1.

format_error(message)

@spec format_error(String.t()) :: String.t()

Format error message for CLI output.

Prefixes with red "Error: " for terminal output.

format_info(message)

@spec format_info(String.t()) :: String.t()

Format info message for CLI output.

Prefixes with blue info icon for terminal output.

format_success(message)

@spec format_success(String.t()) :: String.t()

Format success message for CLI output.

Prefixes with green checkmark for terminal output.

get_adapter(lang)

@spec get_adapter(language()) :: {:ok, module()} | {:error, String.t()}

Get adapter for a given language.

Examples

iex> Metastatic.CLI.get_adapter(:python)
{:ok, Metastatic.Adapters.Python}

iex> Metastatic.CLI.get_adapter(:unknown)
{:error, "Unsupported language: unknown"}

read_file(path)

@spec read_file(file_path()) :: {:ok, String.t()} | {:error, String.t()}

Read file contents.

Examples

iex> Metastatic.CLI.read_file("test.py")
{:ok, "print('hello')"}

iex> Metastatic.CLI.read_file("nonexistent.py")
{:error, "Cannot read file test.py: enoent"}

write_file(path, content)

@spec write_file(file_path(), String.t()) :: :ok | {:error, String.t()}

Write file contents, creating directories if needed.

Examples

iex> Metastatic.CLI.write_file("output/test.py", "print('hello')")
:ok