From b73e4173a3c1e69e02ad6b4e3b43e425e57a5be9 Mon Sep 17 00:00:00 2001 From: MHSanaei Date: Thu, 9 Feb 2023 22:48:06 +0330 Subject: 3x-ui --- util/common/err.go | 29 +++++++++++++++++++++++++++++ util/common/format.go | 21 +++++++++++++++++++++ util/common/multi_error.go | 30 ++++++++++++++++++++++++++++++ util/common/stringUtil.go | 9 +++++++++ 4 files changed, 89 insertions(+) create mode 100644 util/common/err.go create mode 100644 util/common/format.go create mode 100644 util/common/multi_error.go create mode 100644 util/common/stringUtil.go (limited to 'util/common') diff --git a/util/common/err.go b/util/common/err.go new file mode 100644 index 00000000..c0ecbbb8 --- /dev/null +++ b/util/common/err.go @@ -0,0 +1,29 @@ +package common + +import ( + "errors" + "fmt" + "x-ui/logger" +) + +var CtxDone = errors.New("context done") + +func NewErrorf(format string, a ...interface{}) error { + msg := fmt.Sprintf(format, a...) + return errors.New(msg) +} + +func NewError(a ...interface{}) error { + msg := fmt.Sprintln(a...) + return errors.New(msg) +} + +func Recover(msg string) interface{} { + panicErr := recover() + if panicErr != nil { + if msg != "" { + logger.Error(msg, "panic:", panicErr) + } + } + return panicErr +} diff --git a/util/common/format.go b/util/common/format.go new file mode 100644 index 00000000..1ea10877 --- /dev/null +++ b/util/common/format.go @@ -0,0 +1,21 @@ +package common + +import ( + "fmt" +) + +func FormatTraffic(trafficBytes int64) (size string) { + if trafficBytes < 1024 { + return fmt.Sprintf("%.2fB", float64(trafficBytes)/float64(1)) + } else if trafficBytes < (1024 * 1024) { + return fmt.Sprintf("%.2fKB", float64(trafficBytes)/float64(1024)) + } else if trafficBytes < (1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fMB", float64(trafficBytes)/float64(1024*1024)) + } else if trafficBytes < (1024 * 1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fGB", float64(trafficBytes)/float64(1024*1024*1024)) + } else if trafficBytes < (1024 * 1024 * 1024 * 1024 * 1024) { + return fmt.Sprintf("%.2fTB", float64(trafficBytes)/float64(1024*1024*1024*1024)) + } else { + return fmt.Sprintf("%.2fEB", float64(trafficBytes)/float64(1024*1024*1024*1024*1024)) + } +} diff --git a/util/common/multi_error.go b/util/common/multi_error.go new file mode 100644 index 00000000..ff9ff628 --- /dev/null +++ b/util/common/multi_error.go @@ -0,0 +1,30 @@ +package common + +import ( + "strings" +) + +type multiError []error + +func (e multiError) Error() string { + var r strings.Builder + r.WriteString("multierr: ") + for _, err := range e { + r.WriteString(err.Error()) + r.WriteString(" | ") + } + return r.String() +} + +func Combine(maybeError ...error) error { + var errs multiError + for _, err := range maybeError { + if err != nil { + errs = append(errs, err) + } + } + if len(errs) == 0 { + return nil + } + return errs +} diff --git a/util/common/stringUtil.go b/util/common/stringUtil.go new file mode 100644 index 00000000..5f1f93fd --- /dev/null +++ b/util/common/stringUtil.go @@ -0,0 +1,9 @@ +package common + +import "sort" + +func IsSubString(target string, str_array []string) bool { + sort.Strings(str_array) + index := sort.SearchStrings(str_array, target) + return index < len(str_array) && str_array[index] == target +} -- cgit v1.2.3