end
def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do
- Keyword.get(options, opt) ||
- case Mix.shell().prompt("#{prompt} [#{defname || defval}]") do
- "\n" ->
- case defval do
- nil -> get_option(options, opt, prompt, defval)
- defval -> defval
- end
-
- opt ->
- opt |> String.trim()
- end
+ Keyword.get(options, opt) || shell_prompt(prompt, defval, defname)
end
+ def shell_prompt(prompt, defval \\ nil, defname \\ nil) do
+ prompt_message = "#{prompt} [#{defname || defval}]"
+
+ input =
+ if mix_shell?(),
+ do: Mix.shell().prompt(prompt_message),
+ else: :io.get_line(prompt_message)
+
+ case input do
+ "\n" ->
+ case defval do
+ nil ->
+ shell_prompt(prompt, defval, defname)
+
+ defval ->
+ defval
+ end
+
+ input ->
+ String.trim(input)
+ end
+ end
+
+ def shell_yes?(message) do
+ shell_prompt(message, "Yn") in ~w(Yn Y y)
+ end
+
+ def shell_info(message) do
+ if mix_shell?(),
+ do: Mix.shell().info(message),
+ else: IO.puts(message)
+ end
+
+ def shell_error(message) do
+ if mix_shell?(),
+ do: Mix.shell().error(message),
+ else: IO.puts(:stderr, message)
+ end
+
+ @doc "Performs a safe check whether `Mix.shell/0` is available (does not raise if Mix is not loaded)"
+ def mix_shell?, do: :erlang.function_exported(Mix, :shell, 0)
+
def escape_sh_path(path) do
~S(') <> String.replace(path, ~S('), ~S(\')) <> ~S(')
end
admin? = Keyword.get(options, :admin, false)
assume_yes? = Keyword.get(options, :assume_yes, false)
- Mix.shell().info("""
+ Common.shell_info("""
A user will be created with the following information:
- nickname: #{nickname}
- email: #{email}
- admin: #{if(admin?, do: "true", else: "false")}
""")
- proceed? = assume_yes? or Mix.shell().yes?("Continue?")
+ proceed? = assume_yes? or Common.shell_yes?("Continue?")
if proceed? do
Common.start_pleroma()
changeset = User.register_changeset(%User{}, params, need_confirmation: false)
{:ok, _user} = User.register(changeset)
- Mix.shell().info("User #{nickname} created")
+ Common.shell_info("User #{nickname} created")
if moderator? do
run(["set", nickname, "--moderator"])
run(["reset_password", nickname])
end
else
- Mix.shell().info("User will not be created.")
+ Common.shell_info("User will not be created.")
end
end
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
User.perform(:delete, user)
- Mix.shell().info("User #{nickname} deleted.")
+ Common.shell_info("User #{nickname} deleted.")
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ Common.shell_error("No local user #{nickname}")
end
end
with %User{} = user <- User.get_cached_by_nickname(nickname) do
{:ok, user} = User.deactivate(user, !user.info.deactivated)
- Mix.shell().info(
+ Common.shell_info(
"Activation status of #{nickname}: #{if(user.info.deactivated, do: "de", else: "")}activated"
)
else
_ ->
- Mix.shell().error("No user #{nickname}")
+ Common.shell_error("No user #{nickname}")
end
end
with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
{:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
- Mix.shell().info("Generated password reset token for #{user.nickname}")
+ Common.shell_info("Generated password reset token for #{user.nickname}")
IO.puts(
"URL: #{
)
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ Common.shell_error("No local user #{nickname}")
end
end
Common.start_pleroma()
with %User{} = user <- User.get_cached_by_nickname(nickname) do
- Mix.shell().info("Deactivating #{user.nickname}")
+ Common.shell_info("Deactivating #{user.nickname}")
User.deactivate(user)
{:ok, friends} = User.get_friends(user)
Enum.each(friends, fn friend ->
user = User.get_cached_by_id(user.id)
- Mix.shell().info("Unsubscribing #{friend.nickname} from #{user.nickname}")
+ Common.shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}")
User.unfollow(user, friend)
end)
user = User.get_cached_by_id(user.id)
if Enum.empty?(user.following) do
- Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}")
+ Common.shell_info("Successfully unsubscribed all followers from #{user.nickname}")
end
else
_ ->
- Mix.shell().error("No user #{nickname}")
+ Common.shell_error("No user #{nickname}")
end
end
end
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ Common.shell_error("No local user #{nickname}")
end
end
with %User{} = user <- User.get_cached_by_nickname(nickname) do
user = user |> User.tag(tags)
- Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
+ Common.shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
else
_ ->
- Mix.shell().error("Could not change user tags for #{nickname}")
+ Common.shell_error("Could not change user tags for #{nickname}")
end
end
with %User{} = user <- User.get_cached_by_nickname(nickname) do
user = user |> User.untag(tags)
- Mix.shell().info("Tags of #{user.nickname}: #{inspect(tags)}")
+ Common.shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
else
_ ->
- Mix.shell().error("Could not change user tags for #{nickname}")
+ Common.shell_error("Could not change user tags for #{nickname}")
end
end
with {:ok, val} <- options[:expires_at],
options = Map.put(options, :expires_at, val),
{:ok, invite} <- UserInviteToken.create_invite(options) do
- Mix.shell().info(
+ Common.shell_info(
"Generated user invite token " <> String.replace(invite.invite_type, "_", " ")
)
IO.puts(url)
else
error ->
- Mix.shell().error("Could not create invite token: #{inspect(error)}")
+ Common.shell_error("Could not create invite token: #{inspect(error)}")
end
end
def run(["invites"]) do
Common.start_pleroma()
- Mix.shell().info("Invites list:")
+ Common.shell_info("Invites list:")
UserInviteToken.list_invites()
|> Enum.each(fn invite ->
" | Max use: #{max_use} Left use: #{max_use - invite.uses}"
end
- Mix.shell().info(
+ Common.shell_info(
"ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
invite.used
}#{expire_info}#{using_info}"
with {:ok, invite} <- UserInviteToken.find_by_token(token),
{:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
- Mix.shell().info("Invite for token #{token} was revoked.")
+ Common.shell_info("Invite for token #{token} was revoked.")
else
- _ -> Mix.shell().error("No invite found with token #{token}")
+ _ -> Common.shell_error("No invite found with token #{token}")
end
end
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
{:ok, _} = User.delete_user_activities(user)
- Mix.shell().info("User #{nickname} statuses deleted.")
+ Common.shell_info("User #{nickname} statuses deleted.")
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ Common.shell_error("No local user #{nickname}")
end
end
message = if user.info.confirmation_pending, do: "needs", else: "doesn't need"
- Mix.shell().info("#{nickname} #{message} confirmation.")
+ Common.shell_info("#{nickname} #{message} confirmation.")
else
_ ->
- Mix.shell().error("No local user #{nickname}")
+ Common.shell_error("No local user #{nickname}")
end
end
{:ok, user} = User.update_and_set_cache(user_cng)
- Mix.shell().info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
+ Common.shell_info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
user
end
{:ok, user} = User.update_and_set_cache(user_cng)
- Mix.shell().info("Admin status of #{user.nickname}: #{user.info.is_admin}")
+ Common.shell_info("Admin status of #{user.nickname}: #{user.info.is_admin}")
user
end
{:ok, user} = User.update_and_set_cache(user_cng)
- Mix.shell().info("Locked status of #{user.nickname}: #{user.info.locked}")
+ Common.shell_info("Locked status of #{user.nickname}: #{user.info.locked}")
user
end
end