-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
130 lines (114 loc) · 2.4 KB
/
Copy pathutils.go
File metadata and controls
130 lines (114 loc) · 2.4 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"time"
)
/**
* PathExists return true if the path exists.
*/
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
/**
* Throw InvalidLogLine when try to parse a line cannot be parsed.
* It happends sometimes as logs may contain many unformatted lines.
*/
type InvalidLogLine struct{}
func (e *InvalidLogLine) Error() string{
return "Invalid log line"
}
/**
* Throw ParseFailed when when parse some string failed.
* It always implies wrong regulation expression
*/
type ParseFailed struct{
expr string
str string
}
func (e *ParseFailed) Error() string {
return fmt.Sprintf("Failed to parse %s use %s", e.str, e.expr)
}
/**
*
*/
type UnknownReg struct{
reg string
}
func (e *UnknownReg) Error() string{
return fmt.Sprintf("Unknown regular expression %s.", e.reg)
}
type WrongEventType struct{
eventType string
}
func (e *WrongEventType) Error() string{
return fmt.Sprintf("Get wrong event type %s.", e.eventType)
}
type UnMatchedSelfPeer struct{
selfpeer string
peer string
}
func (e *UnMatchedSelfPeer) Error() string {
return fmt.Sprintf("Unmatched self peer %s. %s", e.selfpeer, e.peer)
}
func Map2json(info map[string]interface{}) string {
jsonString, err := json.MarshalIndent(info, "", "\t")
if err != nil{
fmt.Println(err.Error())
return ""
}
return string(jsonString)
}
func Stringmap2json(info map[string]string) string{
jsonString, err := json.MarshalIndent(info, "", "\t")
if err != nil{
fmt.Println(err.Error())
return ""
}
return string(jsonString)
}
func parseTimestamp(str string) (time.Time, error){
t, err := time.Parse(timeFotmat, str)
if err != nil {
fmt.Println(err.Error())
return t, err
}
return t, nil
}
func Contains(sl []string, v string) bool {
for _, vv := range sl {
if vv == v {
return true
}
}
return false
}
/**
* WriteBytes write data into a file
*/
func WriteBytes(filePath string, b []byte) (int, error) {
//os.MkdirAll(path.Dir(filePath), os.ModePerm)
fw, err := os.Create(filePath)
if err != nil {
return 0, err
}
defer fw.Close()
return fw.Write(b)
}
/**
* ReadBytes read data from a file
*/
func ReadBytes(filePath string) ([]byte, error) {
fw, err := os.Open(filePath); if err !=nil {return nil, err}
defer fw.Close()
return ioutil.ReadAll(fw)
}