defmodule Mastomation do @moduledoc """ CLI helpers to fetch and remove Mastodon statuses. """ alias Mastomation.Delete alias Mastomation.Parser alias Mastomation.Threads @doc """ Parses delete-related CLI options from raw arguments. """ @spec parse([String.t()]) :: map() def parse(args \\ []) do Delete.parse_options(args) end @doc """ Executes the CLI command router. """ @spec run([String.t()]) :: :ok | :error def run(args \\ []) do parse_args = if args == [], do: ["--help"], else: args dispatch(Parser.parse(parse_args), parse_args) end @spec dispatch(term(), [String.t()]) :: :ok | :error defp dispatch({:ok, [:thread, :see], result}, _parse_args), do: Threads.run_thread_see(result) defp dispatch({:ok, [:thread, :export], result}, _parse_args), do: Threads.run_thread_export(result) defp dispatch({:ok, [:thread], result}, _parse_args), do: Threads.run_thread_see(result) defp dispatch({:ok, [:delete, :all], result}, _parse_args), do: Delete.run_delete_all(result) defp dispatch({:ok, [:delete, :thread], result}, _parse_args), do: Delete.run_delete_thread(result) defp dispatch({:ok, [:delete], result}, _parse_args), do: Delete.run_delete(result) defp dispatch(:help, parse_args), do: print_parser_help(parse_args, :ok) defp dispatch({:help, _subcommand_path}, parse_args), do: print_parser_help(parse_args, :ok) defp dispatch(:version, parse_args), do: print_parser_help(parse_args, :ok) defp dispatch({:error, _errors}, parse_args), do: print_parser_help(parse_args, :error) defp dispatch(_other, _parse_args), do: :ok @spec print_parser_help([String.t()], :ok | :error) :: :ok | :error defp print_parser_help(parse_args, status) do _ = Parser.print_and_parse!(parse_args) status end @doc """ Decodes HTTP response bodies into maps/lists when needed. """ @spec decode!(binary() | map() | list()) :: map() | list() def decode!(result) do case result do binary when is_binary(binary) -> JSON.decode!(binary) %{} = map -> map list when is_list(list) -> list other -> raise "Unexpected body type: #{inspect(other)}" end end @doc """ Builds authorization/content headers for Mastodon API requests. """ @spec build_header(String.t()) :: keyword(String.t()) def build_header(token) do [ authorization: "Bearer #{token}", "content-type": "application/json" ] end end