Allow mix tasks to always run with debug logging
[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 do
8 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
9 Application.put_env(:logger, :console, level: :debug)
10 {:ok, _} = Application.ensure_all_started(:pleroma)
11 end
12
13 def load_pleroma do
14 Application.load(:pleroma)
15 end
16
17 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
18 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
19 end
20
21 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
22 prompt_message = "#{prompt} [#{defname || defval}] "
23
24 input =
25 if mix_shell?(),
26 do: Mix.shell().prompt(prompt_message),
27 else: :io.get_line(prompt_message)
28
29 case input do
30 "\n" ->
31 case defval do
32 nil ->
33 shell_prompt(prompt, defval, defname)
34
35 defval ->
36 defval
37 end
38
39 input ->
40 String.trim(input)
41 end
42 end
43
44 def shell_yes?(message) do
45 if mix_shell?(),
46 do: Mix.shell().yes?("Continue?"),
47 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
48 end
49
50 def shell_info(message) do
51 if mix_shell?(),
52 do: Mix.shell().info(message),
53 else: IO.puts(message)
54 end
55
56 def shell_error(message) do
57 if mix_shell?(),
58 do: Mix.shell().error(message),
59 else: IO.puts(:stderr, message)
60 end
61
62 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
63 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
64
65 def escape_sh_path(path) do
66 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
67 end
68 end