From cf19073ac950d42a66a26d26560ed90927a2d794 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann Date: Mon, 5 Jul 2021 23:48:57 +0200 Subject: [PATCH] cmd: Move exit status codes to separate package Signal handling by the `atexit` package needs acceess to `exitCodeUncategorizedError`. With this change all exit status values are moved to a dedicated package so that they can be reused. Signed-off-by: Michael Hanselmann --- cmd/cmd.go | 34 +++++++++++----------------------- lib/exitcode/exitcode.go | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 lib/exitcode/exitcode.go diff --git a/cmd/cmd.go b/cmd/cmd.go index c8bc202c5..8c42e0ceb 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -37,6 +37,7 @@ import ( "github.com/rclone/rclone/fs/rc/rcserver" "github.com/rclone/rclone/lib/atexit" "github.com/rclone/rclone/lib/buildinfo" + "github.com/rclone/rclone/lib/exitcode" "github.com/rclone/rclone/lib/random" "github.com/rclone/rclone/lib/terminal" "github.com/spf13/cobra" @@ -60,19 +61,6 @@ var ( errorTooManyArguments = errors.New("too many arguments") ) -const ( - exitCodeSuccess = iota - exitCodeUsageError - exitCodeUncategorizedError - exitCodeDirNotFound - exitCodeFileNotFound - exitCodeRetryError - exitCodeNoRetryError - exitCodeFatalError - exitCodeTransferExceeded - exitCodeNoFilesTransferred -) - // ShowVersion prints the version to stdout func ShowVersion() { osVersion, osKernel := buildinfo.GetOSVersion() @@ -484,31 +472,31 @@ func resolveExitCode(err error) { if err == nil { if ci.ErrorOnNoTransfer { if accounting.GlobalStats().GetTransfers() == 0 { - os.Exit(exitCodeNoFilesTransferred) + os.Exit(exitcode.NoFilesTransferred) } } - os.Exit(exitCodeSuccess) + os.Exit(exitcode.Success) } _, unwrapped := fserrors.Cause(err) switch { case unwrapped == fs.ErrorDirNotFound: - os.Exit(exitCodeDirNotFound) + os.Exit(exitcode.DirNotFound) case unwrapped == fs.ErrorObjectNotFound: - os.Exit(exitCodeFileNotFound) + os.Exit(exitcode.FileNotFound) case unwrapped == errorUncategorized: - os.Exit(exitCodeUncategorizedError) + os.Exit(exitcode.UncategorizedError) case unwrapped == accounting.ErrorMaxTransferLimitReached: - os.Exit(exitCodeTransferExceeded) + os.Exit(exitcode.TransferExceeded) case fserrors.ShouldRetry(err): - os.Exit(exitCodeRetryError) + os.Exit(exitcode.RetryError) case fserrors.IsNoRetryError(err): - os.Exit(exitCodeNoRetryError) + os.Exit(exitcode.NoRetryError) case fserrors.IsFatalError(err): - os.Exit(exitCodeFatalError) + os.Exit(exitcode.FatalError) default: - os.Exit(exitCodeUsageError) + os.Exit(exitcode.UsageError) } } diff --git a/lib/exitcode/exitcode.go b/lib/exitcode/exitcode.go new file mode 100644 index 000000000..7b850cdb8 --- /dev/null +++ b/lib/exitcode/exitcode.go @@ -0,0 +1,25 @@ +// Package exitcode exports rclone's exit status numbers. +package exitcode + +const ( + // Success is returned when rclone finished without error. + Success = iota + // UsageError is returned when there was a syntax or usage error in the arguments. + UsageError + // UncategorizedError is returned for any error not categorised otherwise. + UncategorizedError + // DirNotFound is returned when a source or destination directory is not found. + DirNotFound + // FileNotFound is returned when a source or destination file is not found. + FileNotFound + // RetryError is returned for temporary errors during operations which may be retried. + RetryError + // NoRetryError is returned for errors from operations which can't/shouldn't be retried. + NoRetryError + // FatalError is returned for errors one or more retries won't resolve. + FatalError + // TransferExceeded is returned when network I/O exceeded the quota. + TransferExceeded + // NoFilesTransferred everything succeeded, but no transfer was made. + NoFilesTransferred +)