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