Merge branch 'release/listener' into 'develop'
[akkoma] / lib / mix / tasks / pleroma / config.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.Tasks.Pleroma.Config do
6 use Mix.Task
7 import Mix.Pleroma
8 alias Pleroma.Repo
9 alias Pleroma.Web.AdminAPI.Config
10 @shortdoc "Manages the location of the config"
11 @moduledoc """
12 Manages the location of the config.
13
14 ## Transfers config from file to DB.
15
16 mix pleroma.config migrate_to_db
17
18 ## Transfers config from DB to file.
19
20 mix pleroma.config migrate_from_db ENV
21 """
22
23 def run(["migrate_to_db"]) do
24 start_pleroma()
25
26 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
27 Application.get_all_env(:pleroma)
28 |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
29 |> Enum.each(fn {k, v} ->
30 key = to_string(k) |> String.replace("Elixir.", "")
31 {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
32 Mix.shell().info("#{key} is migrated.")
33 end)
34
35 Mix.shell().info("Settings migrated.")
36 else
37 Mix.shell().info(
38 "Migration is not allowed by config. You can change this behavior in instance settings."
39 )
40 end
41 end
42
43 def run(["migrate_from_db", env, delete?]) do
44 start_pleroma()
45
46 delete? = if delete? == "true", do: true, else: false
47
48 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
49 config_path = "config/#{env}.exported_from_db.secret.exs"
50
51 {:ok, file} = File.open(config_path, [:write])
52 IO.write(file, "use Mix.Config\r\n")
53
54 Repo.all(Config)
55 |> Enum.each(fn config ->
56 mark =
57 if String.starts_with?(config.key, "Pleroma.") or
58 String.starts_with?(config.key, "Ueberauth"),
59 do: ",",
60 else: ":"
61
62 IO.write(
63 file,
64 "config :#{config.group}, #{config.key}#{mark} #{
65 inspect(Config.from_binary(config.value))
66 }\r\n"
67 )
68
69 if delete? do
70 {:ok, _} = Repo.delete(config)
71 Mix.shell().info("#{config.key} deleted from DB.")
72 end
73 end)
74
75 File.close(file)
76 System.cmd("mix", ["format", config_path])
77 else
78 Mix.shell().info(
79 "Migration is not allowed by config. You can change this behavior in instance settings."
80 )
81 end
82 end
83 end