From e379ab8277f552d66737963a9c908ae3fc01c1ff Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Wed, 2 Dec 2020 16:24:32 -0600 Subject: [PATCH] Add --force flag for delete and reset commands Bunch of reorganization and consolidation --- docs/administration/CLI_tasks/config.md | 12 +-- lib/mix/tasks/pleroma/config.ex | 110 +++++++++++++++--------- test/mix/tasks/pleroma/config_test.exs | 95 ++++++++++++++++++++ 3 files changed, 170 insertions(+), 47 deletions(-) diff --git a/docs/administration/CLI_tasks/config.md b/docs/administration/CLI_tasks/config.md index ea07ca293..000ed4d98 100644 --- a/docs/administration/CLI_tasks/config.md +++ b/docs/administration/CLI_tasks/config.md @@ -111,13 +111,13 @@ e.g., this deletes all the settings under `config :tesla` === "OTP" ```sh - ./bin/pleroma_ctl config delete tesla + ./bin/pleroma_ctl config delete [--force] tesla ``` === "From Source" ```sh - mix pleroma.config delete tesla + mix pleroma.config delete [--force] tesla ``` To delete values under a specific key: @@ -127,13 +127,13 @@ e.g., this deletes all the settings under `config :phoenix, :stacktrace_depth` === "OTP" ```sh - ./bin/pleroma_ctl config delete phoenix stacktrace_depth + ./bin/pleroma_ctl config delete [--force] phoenix stacktrace_depth ``` === "From Source" ```sh - mix pleroma.config delete phoenix stacktrace_depth + mix pleroma.config delete [--force] phoenix stacktrace_depth ``` ## Remove all settings from the database @@ -143,11 +143,11 @@ This forcibly removes all saved values in the database. === "OTP" ```sh - ./bin/pleroma_ctl config reset + ./bin/pleroma_ctl config [--force] reset ``` === "From Source" ```sh - mix pleroma.config reset + mix pleroma.config [--force] reset ``` diff --git a/lib/mix/tasks/pleroma/config.ex b/lib/mix/tasks/pleroma/config.ex index e2c4cc680..014782c35 100644 --- a/lib/mix/tasks/pleroma/config.ex +++ b/lib/mix/tasks/pleroma/config.ex @@ -95,7 +95,7 @@ def run(["groups"]) do end) end - def run(["reset"]) do + def run(["reset" | options]) do check_configdb(fn -> start_pleroma() @@ -108,7 +108,11 @@ def run(["reset"]) do shell_error("\nTHIS CANNOT BE UNDONE!") - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + proceed? = + "--force" in options or + shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) + + if proceed? do Ecto.Adapters.SQL.query!(Repo, "TRUNCATE config;") Ecto.Adapters.SQL.query!(Repo, "ALTER SEQUENCE config_id_seq RESTART;") @@ -119,53 +123,46 @@ def run(["reset"]) do end) end - def run(["delete", group]) do - check_configdb(fn -> - start_pleroma() + def run(["delete", "--force", group, key]) do + start_pleroma() - group = maybe_atomize(group) + group = maybe_atomize(group) + key = maybe_atomize(key) - if group_exists?(group) do - shell_info("The following settings will be removed from ConfigDB:\n") + delete_key(group, key) + end - dump_group(group) + def run(["delete", "--force", group]) do + start_pleroma() - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group do - x |> delete(true) - end - end) - else - shell_error("No changes made.") - end - else - shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") - end - end) + group = maybe_atomize(group) + + delete_group(group) end def run(["delete", group, key]) do - check_configdb(fn -> - start_pleroma() + start_pleroma() - group = maybe_atomize(group) - key = maybe_atomize(key) + group = maybe_atomize(group) + key = maybe_atomize(key) - if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do - ConfigDB - |> Repo.all() - |> Enum.filter(fn x -> - if x.group == group and x.key == key do - x |> delete(true) - end - end) - else - shell_error("No changes made.") - end - end) + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + delete_key(group, key) + else + shell_error("No changes made.") + end + end + + def run(["delete", group]) do + start_pleroma() + + group = maybe_atomize(group) + + if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do + delete_group(group) + else + shell_error("No changes made.") + end end @spec migrate_to_db(Path.t() | nil) :: any() @@ -275,7 +272,7 @@ defp delete(config, true) do {:ok, _} = Repo.delete(config) shell_info( - "config #{inspect(config.group)}, #{inspect(config.key)} deleted from the ConfigDB." + "config #{inspect(config.group)}, #{inspect(config.key)} was deleted from the ConfigDB." ) end @@ -348,4 +345,35 @@ defp check_configdb(callback) do ) end end + + defp delete_key(group, key) do + check_configdb(fn -> + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group and x.key == key do + x |> delete(true) + end + end) + end) + end + + defp delete_group(group) do + check_configdb(fn -> + with true <- group_exists?(group) do + shell_info("The following settings will be removed from ConfigDB:\n") + dump_group(group) + + ConfigDB + |> Repo.all() + |> Enum.filter(fn x -> + if x.group == group do + x |> delete(true) + end + end) + else + _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.") + end + end) + end end diff --git a/test/mix/tasks/pleroma/config_test.exs b/test/mix/tasks/pleroma/config_test.exs index 9d6d5ce15..3658b3179 100644 --- a/test/mix/tasks/pleroma/config_test.exs +++ b/test/mix/tasks/pleroma/config_test.exs @@ -297,4 +297,99 @@ test "refuses to dump" do ]} end end + + describe "destructive operations" do + setup do: clear_config(:configurable_from_database, true) + + test "deletes group of settings" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + _config_before = Repo.all(ConfigDB) + + assert config_before = [ + %Pleroma.ConfigDB{ + group: :pleroma, + key: :instance, + value: [name: "Pleroma Test"] + } + ] + + Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma"]) + + config_after = Repo.all(ConfigDB) + + refute config_after == config_before + end + + test "deletes specified key" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + insert(:config, + group: :pleroma, + key: Pleroma.Captcha, + value: [ + enabled: false + ] + ) + + _config_before = Repo.all(ConfigDB) + + assert config_before = [ + %Pleroma.ConfigDB{ + group: :pleroma, + key: :instance, + value: [name: "Pleroma Test"] + }, + %Pleroma.ConfigDB{ + group: :pleroma, + key: Pleroma.Captcha, + value: [enabled: false] + } + ] + + Mix.Tasks.Pleroma.Config.run(["delete", "--force", "pleroma", "Pleroma.Captcha"]) + + config_after = Repo.all(ConfigDB) + + refute config_after == config_before + end + + test "resets entire config" do + insert(:config, + group: :pleroma, + key: :instance, + value: [ + name: "Pleroma Test" + ] + ) + + _config_before = Repo.all(ConfigDB) + + assert config_before = [ + %Pleroma.ConfigDB{ + group: :pleroma, + key: :instance, + value: [name: "Pleroma Test"] + } + ] + + Mix.Tasks.Pleroma.Config.run(["reset", "--force"]) + + config_after = Repo.all(ConfigDB) + + assert config_after == [] + end + end end