configure only console backend
[akkoma] / lib / mix / pleroma.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Pleroma do
6 @doc "Common functions to be reused in mix tasks"
7 def start_pleroma(change_logger_level \\ true) do
8 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
9
10 if Pleroma.Config.get(:env) != :test and change_logger_level do
11 require Logger
12 Logger.configure(:console, level: :debug)
13 end
14
15 {:ok, _} = Application.ensure_all_started(:pleroma)
16 end
17
18 def load_pleroma do
19 Application.load(:pleroma)
20 end
21
22 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
23 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
24 end
25
26 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
27 prompt_message = "#{prompt} [#{defname || defval}] "
28
29 input =
30 if mix_shell?(),
31 do: Mix.shell().prompt(prompt_message),
32 else: :io.get_line(prompt_message)
33
34 case input do
35 "\n" ->
36 case defval do
37 nil ->
38 shell_prompt(prompt, defval, defname)
39
40 defval ->
41 defval
42 end
43
44 input ->
45 String.trim(input)
46 end
47 end
48
49 def shell_yes?(message) do
50 if mix_shell?(),
51 do: Mix.shell().yes?("Continue?"),
52 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
53 end
54
55 def shell_info(message) do
56 if mix_shell?(),
57 do: Mix.shell().info(message),
58 else: IO.puts(message)
59 end
60
61 def shell_error(message) do
62 if mix_shell?(),
63 do: Mix.shell().error(message),
64 else: IO.puts(:stderr, message)
65 end
66
67 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
68 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
69
70 def escape_sh_path(path) do
71 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
72 end
73 end