Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MHSanaei/3x-ui.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMHSanaei <mc.sanaei@gmail.com>2023-02-09 22:18:06 +0300
committerMHSanaei <mc.sanaei@gmail.com>2023-02-09 22:18:06 +0300
commitb73e4173a3c1e69e02ad6b4e3b43e425e57a5be9 (patch)
treed95d2f5e903d97082e11eb9f9023c165b1bde388 /util/common
3x-ui
Diffstat (limited to 'util/common')
-rw-r--r--util/common/err.go29
-rw-r--r--util/common/format.go21
-rw-r--r--util/common/multi_error.go30
-rw-r--r--util/common/stringUtil.go9
4 files changed, 89 insertions, 0 deletions
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
+}