Merge branch 'feature/add-background-image-to-mastoapi' into 'develop'
[akkoma] / lib / mix / tasks / pleroma / config.ex
1 defmodule Mix.Tasks.Pleroma.Config do
2 use Mix.Task
3 alias Mix.Tasks.Pleroma.Common
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 Common.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(%{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]) do
40 Common.start_pleroma()
41
42 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
43 config_path = "config/#{env}.migrated.secret.exs"
44
45 {:ok, file} = File.open(config_path, [:write])
46
47 Repo.all(Config)
48 |> Enum.each(fn config ->
49 mark = if String.starts_with?(config.key, "Pleroma."), do: ",", else: ":"
50
51 IO.write(
52 file,
53 "config :pleroma, #{config.key}#{mark} #{inspect(Config.from_binary(config.value))}\r\n"
54 )
55
56 {:ok, _} = Repo.delete(config)
57 Mix.shell().info("#{config.key} deleted from DB.")
58 end)
59
60 File.close(file)
61 System.cmd("mix", ["format", config_path])
62 else
63 Mix.shell().info(
64 "Migration is not allowed by config. You can change this behavior in instance settings."
65 )
66 end
67 end
68 end