+ check_configdb(fn ->
+ start_pleroma()
+
+ {opts, _} =
+ OptionParser.parse!(options,
+ strict: [env: :string, delete: :boolean, path: :string],
+ aliases: [d: :delete]
+ )
+
+ migrate_from_db(opts)
+ end)
+ end
+
+ def run(["dump"]) do
+ check_configdb(fn ->
+ start_pleroma()
+
+ header = config_header()
+
+ settings =
+ ConfigDB
+ |> Repo.all()
+ |> Enum.sort()
+
+ unless settings == [] do
+ shell_info("#{header}")
+
+ Enum.each(settings, &dump(&1))
+ else
+ shell_error("No settings in ConfigDB.")
+ end
+ end)
+ end
+
+ def run(["dump", group, key]) do
+ check_configdb(fn ->
+ start_pleroma()
+
+ group = maybe_atomize(group)
+ key = maybe_atomize(key)
+
+ group
+ |> ConfigDB.get_by_group_and_key(key)
+ |> dump()
+ end)
+ end
+
+ def run(["dump", group]) do
+ check_configdb(fn ->
+ start_pleroma()
+
+ group = maybe_atomize(group)
+
+ dump_group(group)
+ end)
+ end
+
+ def run(["dump_to_file", group, key, fname]) do
+ check_configdb(fn ->
+ start_pleroma()
+
+ group = maybe_atomize(group)
+ key = maybe_atomize(key)
+
+ config = ConfigDB.get_by_group_and_key(group, key)
+
+ json =
+ %{
+ group: ConfigDB.to_json_types(config.group),
+ key: ConfigDB.to_json_types(config.key),
+ value: ConfigDB.to_json_types(config.value)
+ }
+ |> Jason.encode!()
+ |> Jason.Formatter.pretty_print()
+
+ File.write(fname, json)
+ shell_info("Wrote #{group}_#{key}.json")
+ end)
+ end
+
+ def run(["load_from_file", fname]) do
+ check_configdb(fn ->
+ start_pleroma()
+
+ json = File.read!(fname)
+ config = Jason.decode!(json)
+ group = ConfigDB.to_elixir_types(config["group"])
+ key = ConfigDB.to_elixir_types(config["key"])
+ value = ConfigDB.to_elixir_types(config["value"])
+ params = %{group: group, key: key, value: value}
+
+ ConfigDB.update_or_create(params)
+ shell_info("Loaded #{config["group"]}, #{config["key"]}")
+ end)
+ end
+
+ def run(["groups"]) do
+ check_configdb(fn ->
+ start_pleroma()
+
+ groups =
+ ConfigDB
+ |> distinct([c], true)
+ |> select([c], c.group)
+ |> Repo.all()
+
+ if length(groups) > 0 do
+ shell_info("The following configuration groups are set in ConfigDB:\r\n")
+ groups |> Enum.each(fn x -> shell_info("- #{x}") end)
+ shell_info("\r\n")
+ end
+ end)
+ end
+
+ def run(["reset", "--force"]) do
+ check_configdb(fn ->
+ start_pleroma()
+ truncatedb()
+ shell_info("The ConfigDB settings have been removed from the database.")
+ end)
+ end
+
+ def run(["reset"]) do
+ check_configdb(fn ->
+ start_pleroma()
+
+ shell_info("The following settings will be permanently removed:")
+
+ ConfigDB
+ |> Repo.all()
+ |> Enum.sort()
+ |> Enum.each(&dump(&1))
+
+ shell_error("\nTHIS CANNOT BE UNDONE!")
+
+ if shell_prompt("Are you sure you want to continue?", "n") in ~w(Yn Y y) do
+ truncatedb()
+
+ shell_info("The ConfigDB settings have been removed from the database.")
+ else
+ shell_error("No changes made.")
+ end
+ end)
+ end
+
+ def run(["delete", "--force", group, key]) do
+ start_pleroma()
+
+ group = maybe_atomize(group)
+ key = maybe_atomize(key)
+
+ with true <- key_exists?(group, key) do
+ shell_info("The following settings will be removed from ConfigDB:\n")
+
+ group
+ |> ConfigDB.get_by_group_and_key(key)
+ |> dump()
+
+ delete_key(group, key)
+ else
+ _ ->
+ shell_error("No settings in ConfigDB for #{inspect(group)}, #{inspect(key)}. Aborting.")
+ end
+ end
+
+ def run(["delete", "--force", group]) do
+ start_pleroma()
+
+ group = maybe_atomize(group)
+
+ with true <- group_exists?(group) do
+ shell_info("The following settings will be removed from ConfigDB:\n")
+ dump_group(group)
+ delete_group(group)
+ else
+ _ -> shell_error("No settings in ConfigDB for #{inspect(group)}. Aborting.")
+ end
+ end
+
+ def run(["delete", group, key]) do