-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.go
More file actions
113 lines (95 loc) · 2.04 KB
/
utils.go
File metadata and controls
113 lines (95 loc) · 2.04 KB
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 hutils
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/google/uuid"
"github.com/pkg/errors"
)
// JSONMarshal 类似json.Marshal(), 但不转义特殊符号
func JSONMarshal(v interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(v)
return buffer.Bytes(), err
}
// NewUUID 生成string类型的uuid
func NewUUID() string {
uid, _ := uuid.New().MarshalBinary()
return hex.EncodeToString(uid)
}
// GetEnv 获取环境变量,不存在则使用默认值
func GetEnv(key, fallback string) string {
value := os.Getenv(key)
if len(value) == 0 {
value = fallback
}
return value
}
// CaptureStdout 获取func执行的标准输出
func CaptureStdout(f func()) ([]string, error) {
r, w, _ := os.Pipe()
// 替换原有os.Stdout
stdout := os.Stdout
os.Stdout = w
f()
var buf bytes.Buffer
output := make(chan string, 1)
errs := make(chan error, 1)
go func() {
_, err := io.Copy(&buf, r)
output <- buf.String()
errs <- err
r.Close()
}()
os.Stdout = stdout
w.Close()
return strings.Split(<-output, "\n"), <-errs
}
type CodeError interface {
Error() string
ErrCode() string
ErrMessage() string
}
// ZError
// nolint: govet // may be we need err stack
type ZError struct {
Err error
Code string
Message string
TraceID string
SpanID string
}
type ZErrorOption func(*ZError)
func WithError(err error) ZErrorOption {
return func(z *ZError) {
z.Err = errors.WithStack(err)
}
}
func NewZError(ctx context.Context, code interface{}, message string, options ...ZErrorOption) *ZError {
z := &ZError{
Code: fmt.Sprintf("%v", code),
Message: message,
TraceID: TraceIDFromContext(ctx),
SpanID: SpanIDFromContext(ctx),
}
for _, option := range options {
option(z)
}
return z
}
func (z ZError) Error() string {
return fmt.Sprintf("%s: %s", z.Code, z.Message)
}
func (z ZError) ErrCode() string {
return z.Code
}
func (z ZError) ErrMessage() string {
return z.Message
}