-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.go
135 lines (122 loc) · 2.61 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package Figo
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"github.com/astaxie/beego/orm"
"github.com/figoxu/color"
"github.com/jinzhu/copier"
"github.com/quexer/utee"
"gopkg.in/mgo.v2"
"log"
"os"
"reflect"
"runtime/debug"
"strconv"
"strings"
)
func Catch(hooks ...func()) {
if err := recover(); err != nil {
log.Println(string(debug.Stack()))
log.Println(err, " (recover)")
for _, hook := range hooks {
hook()
}
}
}
func Clone(to, from interface{}) {
if reflect.TypeOf(to).Kind().String() != "ptr" {
utee.Chk(errors.New("Parameter 'to' Should Be An PTR"))
}
copier.Copy(to, from)
}
func Exist(expect interface{}, objs ...interface{}) bool {
for _, v := range objs {
if expect == v {
return true
}
}
return false
}
func RetryExe(business func() error, times int, tips string) (int, bool) {
err := business()
retry := 0
for err != nil && retry < times {
retry++
err = business()
}
success := (err == nil)
if retry > 0 && tips != "" {
log.Println(tips, " Execute With ", retry, " times . @SuccessFlag:", success)
}
return retry, success
}
func ParseUrl(s string) (string, int, error) {
a := strings.Split(s, ":")
if len(a) != 2 {
return "", 0, fmt.Errorf("bad url %s", s)
}
port, err := strconv.Atoi(a[1])
return a[0], port, err
}
func NotFound(err error) bool {
return err == mgo.ErrNotFound || err == orm.ErrNoRows
}
const (
THEME_Black = "black"
THEME_Red = "red"
THEME_Green = "green"
THEME_Yellow = "yellow"
THEME_Blue = "blue"
THEME_Magenta = "megenta"
THEME_Cyan = "cyan"
THEME_White = "white"
)
func ReadInput(tips, tipTheme, inputTheme string) string {
Print(tipTheme, tips)
Print(inputTheme, " ")
reader := bufio.NewReader(os.Stdin)
data, _, _ := reader.ReadLine()
return string(data)
}
func Print(theme string, v ...interface{}) {
s := fmt.Sprint(v...)
switch theme {
case THEME_Black:
color.Black(s)
case THEME_Red:
color.Red(s)
case THEME_Green:
color.Green(s)
case THEME_Yellow:
color.Yellow(s)
case THEME_Blue:
color.Blue(s)
case THEME_Magenta:
color.Magenta(s)
case THEME_Cyan:
color.Cyan(s)
case THEME_White:
color.White(s)
default:
log.Println(s)
}
}
func Println(theme string, v ...interface{}) {
s := fmt.Sprint(v...)
Print(theme, s, "\n")
}
func PrintJson(prefix string, v interface{}) {
if b, err := json.Marshal(v); err == nil {
Print(THEME_Magenta, prefix)
Print(THEME_Blue, string(b), "\n")
}
}
func JsonString(obj interface{}) string {
b, _ := json.Marshal(obj)
s := fmt.Sprintf("%+v", string(b))
r := strings.Replace(s, `\u003c`, "<", -1)
r = strings.Replace(r, `\u003e`, ">", -1)
return r
}