Airat Arifullin
1ed7ab75fb
All checks were successful
DCO action / DCO (pull_request) Successful in 3m18s
Tests and linters / Staticcheck (pull_request) Successful in 4m30s
Tests and linters / gopls check (pull_request) Successful in 4m40s
Pre-commit hooks / Pre-commit (pull_request) Successful in 4m55s
Build / Build Components (pull_request) Successful in 5m28s
Vulncheck / Vulncheck (pull_request) Successful in 5m54s
Tests and linters / Lint (pull_request) Successful in 6m17s
Tests and linters / Run gofumpt (pull_request) Successful in 6m17s
Tests and linters / Tests (pull_request) Successful in 7m56s
Tests and linters / Tests with -race (pull_request) Successful in 7m59s
Vulncheck / Vulncheck (push) Successful in 2m22s
Build / Build Components (push) Successful in 2m58s
Tests and linters / Run gofumpt (push) Successful in 2m55s
Tests and linters / Staticcheck (push) Successful in 3m2s
Pre-commit hooks / Pre-commit (push) Successful in 3m9s
Tests and linters / Lint (push) Successful in 3m47s
Tests and linters / gopls check (push) Successful in 3m43s
Tests and linters / Tests (push) Successful in 4m25s
Tests and linters / Tests with -race (push) Successful in 4m55s
Signed-off-by: Airat Arifullin <a.arifullin@yadro.com>
58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
package common
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
sdkstatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// ExitOnErr prints error and exits with a code that matches
|
|
// one of the common errors from sdk library. If no errors
|
|
// found, exits with 1 code.
|
|
// Does nothing if passed error in nil.
|
|
func ExitOnErr(cmd *cobra.Command, errFmt string, err error) {
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
if errFmt != "" {
|
|
err = fmt.Errorf(errFmt, err)
|
|
}
|
|
|
|
const (
|
|
_ = iota
|
|
internal
|
|
aclDenied
|
|
apemanagerDenied
|
|
)
|
|
|
|
var (
|
|
code int
|
|
|
|
internalErr = new(sdkstatus.ServerInternal)
|
|
accessErr = new(sdkstatus.ObjectAccessDenied)
|
|
apemanagerErr = new(sdkstatus.APEManagerAccessDenied)
|
|
)
|
|
|
|
switch {
|
|
case errors.As(err, &internalErr):
|
|
code = internal
|
|
case errors.As(err, &accessErr):
|
|
code = aclDenied
|
|
err = fmt.Errorf("%w: %s", err, accessErr.Reason())
|
|
case errors.As(err, &apemanagerErr):
|
|
code = apemanagerDenied
|
|
err = fmt.Errorf("%w: %s", err, apemanagerErr.Reason())
|
|
default:
|
|
code = internal
|
|
}
|
|
|
|
cmd.PrintErrln(err)
|
|
if cmd.PersistentPostRun != nil {
|
|
cmd.PersistentPostRun(cmd, nil)
|
|
}
|
|
os.Exit(code)
|
|
}
|