pleroma.ex 2.08 KB
Newer Older
kaniini's avatar
kaniini committed
1
# Pleroma: A lightweight social networking server
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
kaniini's avatar
kaniini committed
3 4
# SPDX-License-Identifier: AGPL-3.0-only

5
defmodule Mix.Pleroma do
Maksim's avatar
Maksim committed
6
  @doc "Common functions to be reused in mix tasks"
rinpatch's avatar
rinpatch committed
7
  def start_pleroma do
8
    Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
9 10 11 12 13

    if Pleroma.Config.get(:env) != :test do
      Application.put_env(:logger, :console, level: :debug)
    end

14
    {:ok, _} = Application.ensure_all_started(:pleroma)
15 16 17 18 19 20 21 22 23 24 25 26 27

    if Pleroma.Config.get(:env) not in [:test, :benchmark] do
      pleroma_rebooted?()
    end
  end

  defp pleroma_rebooted? do
    if Restarter.Pleroma.rebooted?() do
      :ok
    else
      Process.sleep(10)
      pleroma_rebooted?()
    end
rinpatch's avatar
rinpatch committed
28 29
  end

30 31 32 33
  def load_pleroma do
    Application.load(:pleroma)
  end

rinpatch's avatar
rinpatch committed
34
  def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
rinpatch's avatar
rinpatch committed
35
    Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
rinpatch's avatar
rinpatch committed
36 37
  end

rinpatch's avatar
rinpatch committed
38
  def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
39
    prompt_message = "#{prompt} [#{defname || defval}] "
rinpatch's avatar
rinpatch committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    input =
      if mix_shell?(),
        do: Mix.shell().prompt(prompt_message),
        else: :io.get_line(prompt_message)

    case input do
      "\n" ->
        case defval do
          nil ->
            shell_prompt(prompt, defval, defname)

          defval ->
            defval
        end

      input ->
        String.trim(input)
    end
  end

  def shell_yes?(message) do
rinpatch's avatar
rinpatch committed
62 63 64
    if mix_shell?(),
      do: Mix.shell().yes?("Continue?"),
      else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
rinpatch's avatar
rinpatch committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  end

  def shell_info(message) do
    if mix_shell?(),
      do: Mix.shell().info(message),
      else: IO.puts(message)
  end

  def shell_error(message) do
    if mix_shell?(),
      do: Mix.shell().error(message),
      else: IO.puts(:stderr, message)
  end

  @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
  def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)

rinpatch's avatar
rinpatch committed
82 83 84 85
  def escape_sh_path(path) do
    ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
  end
end