User and permission management command: adopt a more suitable exit code

Apparently [1], which is the most standardized set of exit codes
we could find, includes a dedicated exit code for cases where
a user does not exist.

References #425, rabbitmq/rabbitmq-server#2363, bda0cbfdc3.

1. https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+12.0-RELEASE&arch=default&format=html
This commit is contained in:
Michael Klishin 2020-06-08 05:35:33 +03:00
parent bda0cbfdc3
commit 6986086b88
9 changed files with 16 additions and 13 deletions

View File

@ -15,13 +15,14 @@
# Lists predefined error exit codes used by RabbitMQ CLI tools.
# The codes are adopted from [1], which (according to our team's research)
# is possibly the most standardized set of codes there is.
# is possibly the most standardized set of command line tool exit codes there is.
#
# 1. https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+12.0-RELEASE&arch=default&format=html
defmodule RabbitMQ.CLI.Core.ExitCodes do
@exit_ok 0
@exit_usage 64
@exit_dataerr 65
@exit_nouser 67
@exit_unavailable 69
@exit_software 70
@exit_tempfail 75
@ -32,6 +33,7 @@ defmodule RabbitMQ.CLI.Core.ExitCodes do
def exit_ok, do: @exit_ok
def exit_usage, do: @exit_usage
def exit_dataerr, do: @exit_dataerr
def exit_nouser, do: @exit_nouser
def exit_unavailable, do: @exit_unavailable
def exit_software, do: @exit_software
def exit_tempfail, do: @exit_tempfail
@ -48,6 +50,7 @@ defmodule RabbitMQ.CLI.Core.ExitCodes do
def exit_code_for({:validation_failure, _}), do: exit_usage()
# a special case of bad_argument
def exit_code_for({:no_such_vhost, _}), do: exit_dataerr()
def exit_code_for({:no_such_user, _}), do: exit_nouser()
def exit_code_for({:badrpc_multi, :timeout, _}), do: exit_tempfail()
def exit_code_for({:badrpc, :timeout}), do: exit_tempfail()
def exit_code_for({:badrpc, {:timeout, _}}), do: exit_tempfail()

View File

@ -58,7 +58,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ChangePasswordCommand do
{:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exists"}}
end
def output({:error, {:no_such_user, username}}, _) do
{:error, ExitCodes.exit_software(), "User \"#{username}\" does not exist"}
{:error, ExitCodes.exit_nouser(), "User \"#{username}\" does not exist"}
end
use RabbitMQ.CLI.DefaultOutput

View File

@ -14,7 +14,7 @@
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.Commands.ClearPermissionsCommand do
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers}
@behaviour RabbitMQ.CLI.CommandBehaviour
@ -40,7 +40,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearPermissionsCommand do
{:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}}
end
def output({:error, {:no_such_user, username}}, _) do
{:error, "User #{username} does not exist"}
{:error, ExitCodes.exit_nouser(), "User #{username} does not exist"}
end
def output({:error, {:no_such_vhost, vhost}}, _) do
{:error, "Virtual host #{vhost} does not exist"}

View File

@ -14,7 +14,7 @@
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.Commands.ClearTopicPermissionsCommand do
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers}
@behaviour RabbitMQ.CLI.CommandBehaviour
@ -59,7 +59,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.ClearTopicPermissionsCommand do
{:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}}
end
def output({:error, {:no_such_user, username}}, _) do
{:error, "User #{username} does not exist"}
{:error, ExitCodes.exit_nouser(), "User #{username} does not exist"}
end
def output({:error, {:no_such_vhost, vhost}}, _) do
{:error, "Virtual host #{vhost} does not exist"}

View File

@ -35,7 +35,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.DeleteUserCommand do
{:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exists"}}
end
def output({:error, {:no_such_user, username}}, _) do
{:error, ExitCodes.exit_software(), "User \"#{username}\" does not exist"}
{:error, ExitCodes.exit_nouser(), "User \"#{username}\" does not exist"}
end
use RabbitMQ.CLI.DefaultOutput

View File

@ -14,7 +14,7 @@
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand do
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers}
@behaviour RabbitMQ.CLI.CommandBehaviour
@ -54,7 +54,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetPermissionsCommand do
{:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}}
end
def output({:error, {:no_such_user, username}}, _) do
{:error, "User #{username} does not exist"}
{:error, ExitCodes.exit_nouser(), "User #{username} does not exist"}
end
def output({:error, {:no_such_vhost, vhost}}, _) do
{:error, "Virtual host #{vhost} does not exist"}

View File

@ -14,7 +14,7 @@
## Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
defmodule RabbitMQ.CLI.Ctl.Commands.SetTopicPermissionsCommand do
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, Helpers}
@behaviour RabbitMQ.CLI.CommandBehaviour
@ -48,7 +48,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetTopicPermissionsCommand do
{:error, %{"result" => "error", "node" => node_name, "message" => "Virtual host #{vhost} does not exist"}}
end
def output({:error, {:no_such_user, username}}, _) do
{:error, "User #{username} does not exist"}
{:error, ExitCodes.exit_nouser(), "User #{username} does not exist"}
end
def output({:error, {:no_such_vhost, vhost}}, _) do
{:error, "Virtual host #{vhost} does not exist"}

View File

@ -40,7 +40,7 @@ defmodule RabbitMQ.CLI.Ctl.Commands.SetUserTagsCommand do
{:error, %{"result" => "error", "node" => node_name, "message" => "User #{username} does not exists"}}
end
def output({:error, {:no_such_user, username}}, _) do
{:error, ExitCodes.exit_software(), "User \"#{username}\" does not exist"}
{:error, ExitCodes.exit_nouser(), "User \"#{username}\" does not exist"}
end
use RabbitMQ.CLI.DefaultOutput

View File

@ -131,7 +131,7 @@ defmodule RabbitMQCtlTest do
test "An errored command returns an error code" do
command = ["delete_user", "voldemort"]
capture_io(:stderr, fn -> error_check(command, exit_software()) end)
capture_io(:stderr, fn -> error_check(command, exit_nouser()) end)
end
test "A malformed command with an option as the first command-line arg fails gracefully" do