Merge branch 'develop' into issue/1276
[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 File.read!("docs/administration/CLI_tasks/config.md")
12 def run(["migrate_to_db"]) do
13 start_pleroma()
14
15 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
16 Application.get_all_env(:pleroma)
17 |> Enum.reject(fn {k, _v} -> k in [Pleroma.Repo, :env] end)
18 |> Enum.each(fn {k, v} ->
19 key = to_string(k) |> String.replace("Elixir.", "")
20
21 key =
22 if String.starts_with?(key, "Pleroma.") do
23 key
24 else
25 ":" <> key
26 end
27
28 {:ok, _} = Config.update_or_create(%{group: "pleroma", key: key, value: v})
29 Mix.shell().info("#{key} is migrated.")
30 end)
31
32 Mix.shell().info("Settings migrated.")
33 else
34 Mix.shell().info(
35 "Migration is not allowed by config. You can change this behavior in instance settings."
36 )
37 end
38 end
39
40 def run(["migrate_from_db", env, delete?]) do
41 start_pleroma()
42
43 delete? = if delete? == "true", do: true, else: false
44
45 if Pleroma.Config.get([:instance, :dynamic_configuration]) do
46 config_path = "config/#{env}.exported_from_db.secret.exs"
47
48 {:ok, file} = File.open(config_path, [:write, :utf8])
49 IO.write(file, "use Mix.Config\r\n")
50
51 Repo.all(Config)
52 |> Enum.each(fn config ->
53 IO.write(
54 file,
55 "config :#{config.group}, #{config.key}, #{inspect(Config.from_binary(config.value))}\r\n\r\n"
56 )
57
58 if delete? do
59 {:ok, _} = Repo.delete(config)
60 Mix.shell().info("#{config.key} deleted from DB.")
61 end
62 end)
63
64 File.close(file)
65 System.cmd("mix", ["format", config_path])
66 else
67 Mix.shell().info(
68 "Migration is not allowed by config. You can change this behavior in instance settings."
69 )
70 end
71 end
72 end