pleroma.ex 1.73 KB
Newer Older
kaniini's avatar
kaniini committed
1 2 3 4
# Pleroma: A lightweight social networking server
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
# 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 9
    Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
    {:ok, _} = Application.ensure_all_started(:pleroma)
rinpatch's avatar
rinpatch committed
10 11
  end

12 13 14 15
  def load_pleroma do
    Application.load(:pleroma)
  end

rinpatch's avatar
rinpatch committed
16
  def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
rinpatch's avatar
rinpatch committed
17
    Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
rinpatch's avatar
rinpatch committed
18 19
  end

rinpatch's avatar
rinpatch committed
20
  def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
21
    prompt_message = "#{prompt} [#{defname || defval}] "
rinpatch's avatar
rinpatch committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    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
44 45 46
    if mix_shell?(),
      do: Mix.shell().yes?("Continue?"),
      else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
rinpatch's avatar
rinpatch committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
  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
64 65 66 67
  def escape_sh_path(path) do
    ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
  end
end