mobilizon.chapril.org-mobil.../lib/mix/tasks/mobilizon/users/delete.ex
Thomas Citharel 1a3a3602d1
Change CLI delete user task to completly delete user by default
And document the new option

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2020-10-13 14:36:55 +02:00

51 lines
1.2 KiB
Elixir

defmodule Mix.Tasks.Mobilizon.Users.Delete do
@moduledoc """
Task to delete a user
"""
use Mix.Task
alias Mobilizon.Users
alias Mobilizon.Users.User
@shortdoc "Deletes a Mobilizon user"
@impl Mix.Task
def run([email | rest]) do
{options, [], []} =
OptionParser.parse(
rest,
strict: [
assume_yes: :boolean,
keep_email: :boolean
],
aliases: [
y: :assume_yes,
f: :keep_email
]
)
assume_yes? = Keyword.get(options, :assume_yes, false)
keep_email? = Keyword.get(options, :keep_email, false)
Mix.Task.run("app.start")
with {:ok, %User{} = user} <- Users.get_user_by_email(email),
true <- assume_yes? or Mix.shell().yes?("Continue with deleting user #{user.email}?"),
{:ok, %User{} = user} <-
Users.delete_user(user, reserve_email: keep_email?) do
Mix.shell().info("""
The user #{user.email} has been deleted
""")
else
{:error, :user_not_found} ->
Mix.raise("Error: No such user")
_ ->
Mix.raise("User has not been deleted.")
end
end
def run(_) do
Mix.raise("mobilizon.users.delete requires an email as argument")
end
end