-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_query.go
149 lines (125 loc) · 3.1 KB
/
log_query.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package gitpg
import (
"encoding/base64"
"errors"
"fmt"
"strings"
"time"
)
const (
version1String = "Q1"
PrefixLength = 2
AllPrefix = "AL"
AuthorPrefix = "AU"
RefPrefix = "ID"
FromTimePrefix = "FT"
ToTimePrefix = "TT"
querySeparator = "\n"
)
type LogQuery struct {
AuthorRegexp string
All bool
Ref string
FromTime *time.Time
ToTime *time.Time
}
func NormalizeLogQuery(query *LogQuery) (*LogQuery, error) {
str, err := ExportLogQuery(query)
if err != nil {
return nil, err
}
return ImportLogQuery(str)
}
func ExportLogQueryFilename(query *LogQuery) (string, error) {
exportString, err := ExportLogQuery(query)
if err != nil {
return "", err
}
return base64.URLEncoding.WithPadding(base64.NoPadding).EncodeToString([]byte(exportString)), nil
}
func ImportLogQueryFilename(filename string) (*LogQuery, error) {
data, err := base64.URLEncoding.WithPadding(base64.NoPadding).DecodeString(filename)
if err != nil {
return nil, err
}
return ImportLogQuery(string(data))
}
func ExportLogQuery(query *LogQuery) (string, error) {
params := make([]string, 0)
params = append(params, version1String)
if query.All {
params = append(params, AllPrefix)
} else if len(query.Ref) > 0 {
params = append(params, RefPrefix+query.Ref)
}
if len(query.AuthorRegexp) > 0 {
params = append(params, AuthorPrefix+query.AuthorRegexp)
}
if query.FromTime != nil {
params = append(params, FromTimePrefix+formatTime(*query.FromTime))
}
if query.ToTime != nil {
params = append(params, ToTimePrefix+formatTime(*query.ToTime))
}
return strings.Join(params, querySeparator), nil
}
func ImportLogQuery(q string) (*LogQuery, error) {
query := strings.Split(q, querySeparator)
if len(query) < 1 {
// TODO: error handling
return nil, errors.New("Invalid Log Query")
}
if query[0] == version1String {
return importLogVersion1Query(query[1:])
} else {
// TODO: error handling
return nil, errors.New("Invalid Log Query")
}
}
func formatTime(t time.Time) string {
return fmt.Sprintf("0x%x", t.UTC().Unix())
}
func parseTime(value string) (*time.Time, error) {
var unixTime int64
n, err := fmt.Sscanf(value, "0x%x", &unixTime)
if err != nil {
return nil, err
}
if n < 1 {
return nil, errors.New(fmt.Sprintf("Invalid time export format %s", value))
}
t := time.Unix(unixTime, 0)
return &t, nil
}
func importLogVersion1Query(query []string) (*LogQuery, error) {
var err error
q := LogQuery{}
for _, param := range query {
if len(param) < PrefixLength {
return nil, errors.New("Invalid Log Parameter " + param)
}
paramName := param[0:PrefixLength]
paramValue := param[PrefixLength:]
switch paramName {
case AuthorPrefix:
q.AuthorRegexp = paramValue
case RefPrefix:
q.Ref = paramValue
case AllPrefix:
q.All = true
case FromTimePrefix:
q.FromTime, err = parseTime(paramValue)
if err != nil {
return nil, err
}
case ToTimePrefix:
q.ToTime, err = parseTime(paramValue)
if err != nil {
return nil, err
}
default:
return nil, errors.New("Invalid Log Parameter value " + param)
}
}
return &q, nil
}