Support atoms and strings as args to the mix task

Improve output. Show the user what will be deleted before the prompt.
This commit is contained in:
Mark Felder 2020-11-27 14:44:05 -06:00
parent ae7d37de06
commit 3df115b2b0

View file

@ -36,12 +36,18 @@ defmodule Mix.Tasks.Pleroma.Config do
header = config_header() header = config_header()
shell_info("#{header}") settings =
ConfigDB
|> Repo.all()
|> Enum.sort()
ConfigDB unless settings == [] do
|> Repo.all() shell_info("#{header}")
|> Enum.sort()
|> Enum.each(&dump(&1)) settings |> Enum.each(&dump(&1))
else
shell_error("No settings in ConfigDB.")
end
else else
_ -> configdb_not_enabled() _ -> configdb_not_enabled()
end end
@ -51,6 +57,9 @@ defmodule Mix.Tasks.Pleroma.Config do
with true <- Pleroma.Config.get([:configurable_from_database]) do with true <- Pleroma.Config.get([:configurable_from_database]) do
start_pleroma() start_pleroma()
group = atomize(group)
key = atomize(key)
dump_key(group, key) dump_key(group, key)
else else
_ -> configdb_not_enabled() _ -> configdb_not_enabled()
@ -61,6 +70,8 @@ defmodule Mix.Tasks.Pleroma.Config do
with true <- Pleroma.Config.get([:configurable_from_database]) do with true <- Pleroma.Config.get([:configurable_from_database]) do
start_pleroma() start_pleroma()
group = atomize(group)
dump_group(group) dump_group(group)
else else
_ -> configdb_not_enabled() _ -> configdb_not_enabled()
@ -88,10 +99,12 @@ defmodule Mix.Tasks.Pleroma.Config do
end end
end end
def run(["keys" | group]) do def run(["keys", group]) do
with true <- Pleroma.Config.get([:configurable_from_database]) do with true <- Pleroma.Config.get([:configurable_from_database]) do
start_pleroma() start_pleroma()
group = atomize(group)
keys = keys =
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
@ -124,7 +137,7 @@ defmodule Mix.Tasks.Pleroma.Config do
shell_info("The ConfigDB settings have been removed from the database.") shell_info("The ConfigDB settings have been removed from the database.")
else else
shell_info("No changes made.") shell_error("No changes made.")
end end
else else
_ -> configdb_not_enabled() _ -> configdb_not_enabled()
@ -135,18 +148,26 @@ defmodule Mix.Tasks.Pleroma.Config do
with true <- Pleroma.Config.get([:configurable_from_database]) do with true <- Pleroma.Config.get([:configurable_from_database]) do
start_pleroma() start_pleroma()
group = group |> String.to_atom() group = atomize(group)
if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do if group_exists?(group) do
ConfigDB shell_info("The following settings will be removed from ConfigDB:\n")
|> Repo.all()
|> Enum.filter(fn x -> dump_group(group)
if x.group == group do
x |> delete(true) if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
end ConfigDB
end) |> Repo.all()
|> Enum.filter(fn x ->
if x.group == group do
x |> delete(true)
end
end)
else
shell_error("No changes made.")
end
else else
shell_info("No changes made.") shell_error("No settings in ConfigDB for :#{group}. Aborting.")
end end
else else
_ -> configdb_not_enabled() _ -> configdb_not_enabled()
@ -157,8 +178,8 @@ defmodule Mix.Tasks.Pleroma.Config do
with true <- Pleroma.Config.get([:configurable_from_database]) do with true <- Pleroma.Config.get([:configurable_from_database]) do
start_pleroma() start_pleroma()
group = group |> String.to_atom() group = atomize(group)
key = key |> String.to_atom() key = atomize(key)
if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
ConfigDB ConfigDB
@ -169,7 +190,7 @@ defmodule Mix.Tasks.Pleroma.Config do
end end
end) end)
else else
shell_info("No changes made.") shell_error("No changes made.")
end end
else else
_ -> configdb_not_enabled() _ -> configdb_not_enabled()
@ -296,7 +317,10 @@ defmodule Mix.Tasks.Pleroma.Config do
defp delete(config, true) do defp delete(config, true) do
{:ok, _} = Repo.delete(config) {:ok, _} = Repo.delete(config)
shell_info(":#{config.group}, :#{config.key} deleted from the ConfigDB.")
shell_info(
"config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB."
)
end end
defp delete(_config, _), do: :ok defp delete(_config, _), do: :ok
@ -313,10 +337,7 @@ defmodule Mix.Tasks.Pleroma.Config do
) )
end end
defp dump_key(group, key) do defp dump_key(group, key) when is_atom(group) and is_atom(key) do
group = group |> String.to_atom()
key = key |> String.to_atom()
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
|> Enum.filter(fn x -> |> Enum.filter(fn x ->
@ -326,9 +347,7 @@ defmodule Mix.Tasks.Pleroma.Config do
end) end)
end end
defp dump_group(group) do defp dump_group(group) when is_atom(group) do
group = group |> String.to_atom()
ConfigDB ConfigDB
|> Repo.all() |> Repo.all()
|> Enum.filter(fn x -> |> Enum.filter(fn x ->
@ -337,4 +356,24 @@ defmodule Mix.Tasks.Pleroma.Config do
end end
end) end)
end end
defp group_exists?(group) when is_atom(group) do
result =
ConfigDB
|> Repo.all()
|> Enum.filter(fn x ->
if x.group == group do
x
end
end)
unless result == [] do
true
else
false
end
end
defp atomize(x) when is_atom(x), do: x
defp atomize(x) when is_binary(x), do: String.to_atom(x)
end end