Centralize check that configdb is enabled which now raises an exception

This commit is contained in:
Mark Felder 2020-11-29 13:29:36 -06:00
parent 53a5ec1952
commit a7b5280b5b

View file

@ -14,11 +14,13 @@ defmodule Mix.Tasks.Pleroma.Config do
@moduledoc File.read!("docs/administration/CLI_tasks/config.md")
def run(["migrate_to_db"]) do
check_configdb()
start_pleroma()
migrate_to_db()
end
def run(["migrate_from_db" | options]) do
check_configdb()
start_pleroma()
{opts, _} =
@ -31,7 +33,7 @@ def run(["migrate_from_db" | options]) do
end
def run(["dump"]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
check_configdb()
start_pleroma()
header = config_header()
@ -48,38 +50,29 @@ def run(["dump"]) do
else
shell_error("No settings in ConfigDB.")
end
else
_ -> configdb_not_enabled()
end
end
def run(["dump", group, key]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
check_configdb()
start_pleroma()
group = maybe_atomize(group)
key = maybe_atomize(key)
dump_key(group, key)
else
_ -> configdb_not_enabled()
end
end
def run(["dump", group]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
check_configdb()
start_pleroma()
group = maybe_atomize(group)
dump_group(group)
else
_ -> configdb_not_enabled()
end
end
def run(["groups"]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
check_configdb()
start_pleroma()
groups =
@ -94,13 +87,10 @@ def run(["groups"]) do
groups |> Enum.each(fn x -> shell_info("- #{x}") end)
shell_info("\r\n")
end
else
_ -> configdb_not_enabled()
end
end
def run(["reset"]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
check_configdb()
start_pleroma()
shell_info("The following settings will be permanently removed:")
@ -120,13 +110,10 @@ def run(["reset"]) do
else
shell_error("No changes made.")
end
else
_ -> configdb_not_enabled()
end
end
def run(["delete", group]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
check_configdb()
start_pleroma()
group = maybe_atomize(group)
@ -150,13 +137,10 @@ def run(["delete", group]) do
else
shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
end
else
_ -> configdb_not_enabled()
end
end
def run(["delete", group, key]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do
check_configdb()
start_pleroma()
group = maybe_atomize(group)
@ -173,15 +157,11 @@ def run(["delete", group, key]) do
else
shell_error("No changes made.")
end
else
_ -> configdb_not_enabled()
end
end
@spec migrate_to_db(Path.t() | nil) :: any()
def migrate_to_db(file_path \\ nil) do
with true <- Pleroma.Config.get([:configurable_from_database]),
:ok <- Pleroma.Config.DeprecationWarnings.warn() do
with :ok <- Pleroma.Config.DeprecationWarnings.warn() do
config_file =
if file_path do
file_path
@ -195,8 +175,7 @@ def migrate_to_db(file_path \\ nil) do
do_migrate_to_db(config_file)
else
:error -> deprecation_error()
_ -> migration_error()
_ -> deprecation_error()
end
end
@ -232,7 +211,6 @@ defp create(group, settings) do
end
defp migrate_from_db(opts) do
if Pleroma.Config.get([:configurable_from_database]) do
env = opts[:env] || Pleroma.Config.get(:env)
config_path =
@ -259,15 +237,6 @@ defp migrate_from_db(opts) do
shell_info(
"Database configuration settings have been exported to config/#{env}.exported_from_db.secret.exs"
)
else
migration_error()
end
end
defp migration_error do
shell_error(
"Migration is not allowed in config. You can change this behavior by setting `config :pleroma, configurable_from_database: true`"
)
end
defp deprecation_error do
@ -313,7 +282,7 @@ defp dump(%Pleroma.ConfigDB{} = config) do
end
defp configdb_not_enabled do
shell_error(
raise(
"ConfigDB not enabled. Please check the value of :configurable_from_database in your configuration."
)
end
@ -366,4 +335,12 @@ defp maybe_atomize(arg) when is_binary(arg) do
String.to_atom(arg)
end
end
defp check_configdb() do
with true <- Pleroma.Config.get([:configurable_from_database]) do
:ok
else
_ -> configdb_not_enabled()
end
end
end