load default config in mix tasks
[akkoma] / lib / mix / pleroma.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Pleroma do
6 @apps [:restarter, :ecto, :ecto_sql, :postgrex, :db_connection, :cachex]
7 @cachex_childs ["object", "user"]
8 @doc "Common functions to be reused in mix tasks"
9 def start_pleroma do
10 Pleroma.Config.Holder.save_default()
11 Application.put_env(:phoenix, :serve_endpoints, false, persistent: true)
12
13 if Pleroma.Config.get(:env) != :test do
14 Application.put_env(:logger, :console, level: :debug)
15 end
16
17 apps =
18 if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do
19 [:gun | @apps]
20 else
21 [:hackney | @apps]
22 end
23
24 Enum.each(apps, &Application.ensure_all_started/1)
25
26 childs = [
27 Pleroma.Repo,
28 {Pleroma.Config.TransferTask, false},
29 Pleroma.Web.Endpoint
30 ]
31
32 cachex_childs = Enum.map(@cachex_childs, &Pleroma.Application.build_cachex(&1, []))
33
34 Supervisor.start_link(childs ++ cachex_childs,
35 strategy: :one_for_one,
36 name: Pleroma.Supervisor
37 )
38
39 if Pleroma.Config.get(:env) not in [:test, :benchmark] do
40 pleroma_rebooted?()
41 end
42 end
43
44 defp pleroma_rebooted? do
45 if Restarter.Pleroma.rebooted?() do
46 :ok
47 else
48 Process.sleep(10)
49 pleroma_rebooted?()
50 end
51 end
52
53 def load_pleroma do
54 Application.load(:pleroma)
55 end
56
57 def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
58 Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
59 end
60
61 def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
62 prompt_message = "#{prompt} [#{defname || defval}] "
63
64 input =
65 if mix_shell?(),
66 do: Mix.shell().prompt(prompt_message),
67 else: :io.get_line(prompt_message)
68
69 case input do
70 "\n" ->
71 case defval do
72 nil ->
73 shell_prompt(prompt, defval, defname)
74
75 defval ->
76 defval
77 end
78
79 input ->
80 String.trim(input)
81 end
82 end
83
84 def shell_yes?(message) do
85 if mix_shell?(),
86 do: Mix.shell().yes?("Continue?"),
87 else: shell_prompt(message, "Continue?") in ~w(Yn Y y)
88 end
89
90 def shell_info(message) do
91 if mix_shell?(),
92 do: Mix.shell().info(message),
93 else: IO.puts(message)
94 end
95
96 def shell_error(message) do
97 if mix_shell?(),
98 do: Mix.shell().error(message),
99 else: IO.puts(:stderr, message)
100 end
101
102 @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
103 def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
104
105 def escape_sh_path(path) do
106 ~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
107 end
108 end