From 8965e70463142f8951cb9011dec4f38118dc7832 Mon Sep 17 00:00:00 2001
From: Pavel Karpy <carpawell@nspcc.ru>
Date: Tue, 6 Jul 2021 13:25:31 +0300
Subject: [PATCH] [#665] cli/util: Add exit code utils

Add `errf`, `exitOnErr` and `exitOnErrCode` functions
that works with errors and exits with non-zero exit
codes on non-nil errors.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
---
 cmd/neofs-cli/modules/util.go | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/cmd/neofs-cli/modules/util.go b/cmd/neofs-cli/modules/util.go
index 64aa148d7..f560aaa7c 100644
--- a/cmd/neofs-cli/modules/util.go
+++ b/cmd/neofs-cli/modules/util.go
@@ -508,3 +508,28 @@ func keyerParseFile(filename string, d *keyer.Dashboard) error {
 
 	return d.ParseBinary(data)
 }
+
+// errf returns formatted error in errFmt format
+// if err is not nil.
+func errf(errFmt string, err error) error {
+	if err == nil {
+		return nil
+	}
+
+	return fmt.Errorf(errFmt, err)
+}
+
+// exitOnErr calls exitOnErrCode with code 1.
+func exitOnErr(cmd *cobra.Command, err error) {
+	exitOnErrCode(cmd, err, 1)
+}
+
+// exitOnErrCode prints error via cmd and calls
+// os.Exit with passed exit code. Does nothing
+// if err is nil.
+func exitOnErrCode(cmd *cobra.Command, err error, code int) {
+	if err != nil {
+		cmd.PrintErrln(err)
+		os.Exit(code)
+	}
+}