-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
113 lines (100 loc) · 1.95 KB
/
util.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
package rice
import (
"bytes"
"crypto/rand"
"encoding/json"
"errors"
"math/big"
"net"
"os"
"runtime/debug"
"strings"
"unicode"
)
func LocalHostname() (string, error) {
return os.Hostname()
}
func LocalAddr() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "", err
}
defer conn.Close()
s := conn.LocalAddr().String()
i := strings.LastIndex(s, ":")
if i == -1 {
return "", errors.New("can't get local addr")
}
return s[:i], nil
}
func RandNumber(max int64) int64 {
if max <= 0 {
return 0
}
result, _ := rand.Int(rand.Reader, big.NewInt(max))
return result.Int64()
}
func LowerTitle(s string) string {
if s == "" {
return s
}
a := []rune(s)
a[0] = unicode.ToLower(a[0])
return string(a)
}
func RemoveInvisibleChars(s string) string {
return strings.Map(func(r rune) rune {
if unicode.IsGraphic(r) {
return r
}
return -1
}, s)
}
func VersionInfo() (string, string) {
var gitRevision string
buildInfo, ok := debug.ReadBuildInfo()
if ok {
for _, v := range buildInfo.Settings {
if v.Key == "vcs.revision" {
gitRevision = v.Value
break
}
}
}
if len(gitRevision) < 7 {
return "", buildInfo.GoVersion
}
return gitRevision[0:7], buildInfo.GoVersion
}
func JsonEncode(t any) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}
func JsonEncodeIndent(t any, prefix, indent string) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.SetIndent(prefix, indent)
err := encoder.Encode(t)
return buffer.Bytes(), err
}
func Exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
}
return true
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func IsFile(path string) bool {
return !IsDir(path)
}