ConfigDB: Refactor and simplify whitelist/blacklist filter tasks
This commit is contained in:
parent
3ab8cc1eb9
commit
232cdab753
1 changed files with 41 additions and 56 deletions
|
|
@ -252,40 +252,14 @@ defmodule Mix.Tasks.Pleroma.Config do
|
||||||
if whitelisted_configs in [nil, false] do
|
if whitelisted_configs in [nil, false] do
|
||||||
shell_error("No unwanted settings in ConfigDB. No changes made.")
|
shell_error("No unwanted settings in ConfigDB. No changes made.")
|
||||||
else
|
else
|
||||||
whitelisted_groups =
|
{whitelisted_groups, whitelisted_keys} = get_filters(whitelisted_configs)
|
||||||
whitelisted_configs
|
|
||||||
|> Enum.filter(fn
|
|
||||||
{_group} -> true
|
|
||||||
_ -> false
|
|
||||||
end)
|
|
||||||
|> Enum.map(fn {group} -> group end)
|
|
||||||
|
|
||||||
whitelisted_keys =
|
|
||||||
whitelisted_configs
|
|
||||||
|> Enum.filter(fn
|
|
||||||
{_group, _key} -> true
|
|
||||||
_ -> false
|
|
||||||
end)
|
|
||||||
|
|
||||||
filtered =
|
filtered =
|
||||||
from(c in ConfigDB)
|
from(c in ConfigDB)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|> Enum.filter(¬_whitelisted?(&1, whitelisted_groups, whitelisted_keys))
|
|> Enum.filter(¬_whitelisted?(&1, whitelisted_groups, whitelisted_keys))
|
||||||
|
|
||||||
if not Enum.empty?(filtered) do
|
do_config_filter(filtered, force)
|
||||||
shell_info("The following settings will be removed from ConfigDB:\n")
|
|
||||||
Enum.each(filtered, &dump(&1))
|
|
||||||
|
|
||||||
if force or shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
|
|
||||||
filtered_ids = Enum.map(filtered, fn %{id: id} -> id end)
|
|
||||||
|
|
||||||
Repo.delete_all(from(c in ConfigDB, where: c.id in ^filtered_ids))
|
|
||||||
else
|
|
||||||
shell_error("No changes made.")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
shell_error("No unwanted settings in ConfigDB. No changes made.")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -307,39 +281,14 @@ defmodule Mix.Tasks.Pleroma.Config do
|
||||||
if blacklisted_configs in [nil, false] do
|
if blacklisted_configs in [nil, false] do
|
||||||
shell_error("No unwanted settings in ConfigDB. No changes made.")
|
shell_error("No unwanted settings in ConfigDB. No changes made.")
|
||||||
else
|
else
|
||||||
blacklisted_groups =
|
{blacklisted_groups, blacklisted_keys} = get_filters(blacklisted_configs)
|
||||||
blacklisted_configs
|
|
||||||
|> Enum.filter(fn
|
|
||||||
{_group} -> true
|
|
||||||
_ -> false
|
|
||||||
end)
|
|
||||||
|
|
||||||
blacklisted_keys =
|
|
||||||
blacklisted_configs
|
|
||||||
|> Enum.filter(fn
|
|
||||||
{_group, _key} -> true
|
|
||||||
_ -> false
|
|
||||||
end)
|
|
||||||
|
|
||||||
filtered =
|
filtered =
|
||||||
from(c in ConfigDB)
|
from(c in ConfigDB)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|> Enum.filter(&blacklisted?(&1, blacklisted_groups, blacklisted_keys))
|
|> Enum.filter(&blacklisted?(&1, blacklisted_groups, blacklisted_keys))
|
||||||
|
|
||||||
if not Enum.empty?(filtered) do
|
do_config_filter(filtered, force)
|
||||||
shell_info("The following settings will be removed from ConfigDB:\n")
|
|
||||||
Enum.each(filtered, &dump(&1))
|
|
||||||
|
|
||||||
if force or shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
|
|
||||||
filtered_ids = Enum.map(filtered, fn %{id: id} -> id end)
|
|
||||||
|
|
||||||
Repo.delete_all(from(c in ConfigDB, where: c.id in ^filtered_ids))
|
|
||||||
else
|
|
||||||
shell_error("No changes made.")
|
|
||||||
end
|
|
||||||
else
|
|
||||||
shell_error("No unwanted settings in ConfigDB. No changes made.")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -544,13 +493,49 @@ defmodule Mix.Tasks.Pleroma.Config do
|
||||||
Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
|
Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_filters(config) do
|
||||||
|
groups =
|
||||||
|
config
|
||||||
|
|> Enum.filter(fn
|
||||||
|
{_group} -> true
|
||||||
|
_ -> false
|
||||||
|
end)
|
||||||
|
|>Enum.map(fn {group} -> group end)
|
||||||
|
|
||||||
|
keys =
|
||||||
|
config
|
||||||
|
|> Enum.filter(fn
|
||||||
|
{_group, _key} -> true
|
||||||
|
_ -> false
|
||||||
|
end)
|
||||||
|
|
||||||
|
{groups, keys}
|
||||||
|
end
|
||||||
|
|
||||||
defp not_whitelisted?(%{group: group, key: key}, whitelisted_groups, whitelisted_keys) do
|
defp not_whitelisted?(%{group: group, key: key}, whitelisted_groups, whitelisted_keys) do
|
||||||
not Enum.member?(whitelisted_groups, group) and
|
not Enum.member?(whitelisted_groups, group) and
|
||||||
not Enum.member?(whitelisted_keys, {group, key})
|
not Enum.member?(whitelisted_keys, {group, key})
|
||||||
end
|
end
|
||||||
|
|
||||||
defp blacklisted?(%{group: group, key: key}, blacklisted_groups, blacklisted_keys) do
|
defp blacklisted?(%{group: group, key: key}, blacklisted_groups, blacklisted_keys) do
|
||||||
Enum.member?(blacklisted_groups, {group}) or
|
Enum.member?(blacklisted_groups, group) or
|
||||||
Enum.member?(blacklisted_keys, {group, key})
|
Enum.member?(blacklisted_keys, {group, key})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp do_config_filter(filtered, _) when length(filtered) == 0 do
|
||||||
|
shell_error("No unwanted settings in ConfigDB. No changes made.")
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_config_filter(filtered, force) do
|
||||||
|
shell_info("The following settings will be removed from ConfigDB:\n")
|
||||||
|
Enum.each(filtered, &dump(&1))
|
||||||
|
|
||||||
|
if force or shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
|
||||||
|
filtered_ids = Enum.map(filtered, fn %{id: id} -> id end)
|
||||||
|
|
||||||
|
Repo.delete_all(from(c in ConfigDB, where: c.id in ^filtered_ids))
|
||||||
|
else
|
||||||
|
shell_error("No changes made.")
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue