Add --force flag for delete and reset commands
authorMark Felder <feld@FreeBSD.org>
Wed, 2 Dec 2020 22:24:32 +0000 (16:24 -0600)
committerMark Felder <feld@FreeBSD.org>
Wed, 2 Dec 2020 22:24:32 +0000 (16:24 -0600)
Bunch of reorganization and consolidation

docs/administration/CLI_tasks/config.md
lib/mix/tasks/pleroma/config.ex
test/mix/tasks/pleroma/config_test.exs

index ea07ca293c6ef83c77fe6bbc3431e849562268a1..000ed4d98cc2963a5b613e2eb734032b3207c6fe 100644 (file)
@@ -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
     ```
index e2c4cc680692f14edb550e3d9abd851af87d1023..014782c3514a9acebce897aa933130f7dab07c09 100644 (file)
@@ -95,7 +95,7 @@ defmodule Mix.Tasks.Pleroma.Config do
     end)
   end
 
-  def run(["reset"]) do
+  def run(["reset" | options]) do
     check_configdb(fn ->
       start_pleroma()
 
@@ -108,7 +108,11 @@ defmodule Mix.Tasks.Pleroma.Config 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 @@ defmodule Mix.Tasks.Pleroma.Config 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 @@ defmodule Mix.Tasks.Pleroma.Config 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 @@ defmodule Mix.Tasks.Pleroma.Config 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
index 9d6d5ce154c13c95b8b06bcd76e42a7060688b19..3658b3179848e3d8f9c85be140ba81fa710db20d 100644 (file)
@@ -297,4 +297,99 @@ defmodule Mix.Tasks.Pleroma.ConfigTest 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