Merge remote-tracking branch 'origin/develop' into pleroma-conversations
[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 `config/env.exported_from_db.secret.exs`
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
32 key =
33 if String.starts_with?(key, "Pleroma.") do
34 key
35 else
36 ":" <> key
37 end
38
39 {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
40 Mix.shell().info("#{key} is migrated.")
41 end)
42
43 Mix.shell().info("Settings migrated.")
44 else
45 Mix.shell().info(
46 "Migration is not allowed by config. You can change this behavior in instance settings."
47 )
48 end
49 end
50
51 def run(["migrate_from_db", env, delete?]) do
52 start_pleroma()
53
54 delete? = if delete? == "true", do: true, else: false
55
56 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
57 config_path = "config/#{env}.exported_from_db.secret.exs"
58
59 {:ok, file} = File.open(config_path, [:write])
60 IO.write(file, "use Mix.Config\r\n")
61
62 Repo.all(Config)
63 |> Enum.each(fn config ->
64 IO.write(
65 file,
66 "config :#{config.group}, #{config.key}, #{inspect(Config.from_binary(config.value))}\r\n\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