-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlog.go
235 lines (199 loc) · 4.97 KB
/
log.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: Apache-2.0
package types
import (
"bytes"
"fmt"
"github.com/go-vela/server/constants"
)
// Log is the API representation of a log for a step in a build.
//
// swagger:model Log
type Log struct {
ID *int64 `json:"id,omitempty"`
BuildID *int64 `json:"build_id,omitempty"`
RepoID *int64 `json:"repo_id,omitempty"`
ServiceID *int64 `json:"service_id,omitempty"`
StepID *int64 `json:"step_id,omitempty"`
// swagger:strfmt base64
Data *[]byte `json:"data,omitempty"`
}
// AppendData adds the provided data to the end of
// the Data field for the Log type. If the Data
// field is empty, then the function overwrites
// the entire Data field.
func (l *Log) AppendData(data []byte) {
// check if Data field is empty
if len(l.GetData()) == 0 {
// overwrite the Data field
l.SetData(data)
return
}
// add the data to the Data field
l.SetData(append(l.GetData(), data...))
}
// MaskData reads through the log data and masks
// all values provided in the string slice. If the
// log is empty, we do nothing.
func (l *Log) MaskData(secrets []string) {
data := l.GetData()
// early exit on empty log or secret list
if len(data) == 0 || len(secrets) == 0 {
return
}
// byte replace data with masked logs
for _, secret := range secrets {
data = bytes.ReplaceAll(data, []byte(secret), []byte(constants.SecretLogMask))
}
// update data field to masked logs
l.SetData(data)
}
// GetID returns the ID field.
//
// When the provided Log type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Log) GetID() int64 {
// return zero value if Log type or ID field is nil
if l == nil || l.ID == nil {
return 0
}
return *l.ID
}
// GetBuildID returns the BuildID field.
//
// When the provided Log type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Log) GetBuildID() int64 {
// return zero value if Log type or BuildID field is nil
if l == nil || l.BuildID == nil {
return 0
}
return *l.BuildID
}
// GetRepoID returns the RepoID field.
//
// When the provided Log type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Log) GetRepoID() int64 {
// return zero value if Log type or RepoID field is nil
if l == nil || l.RepoID == nil {
return 0
}
return *l.RepoID
}
// GetServiceID returns the ServiceID field.
//
// When the provided Log type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Log) GetServiceID() int64 {
// return zero value if Log type or ServiceID field is nil
if l == nil || l.ServiceID == nil {
return 0
}
return *l.ServiceID
}
// GetStepID returns the StepID field.
//
// When the provided Log type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Log) GetStepID() int64 {
// return zero value if Log type or StepID field is nil
if l == nil || l.StepID == nil {
return 0
}
return *l.StepID
}
// GetData returns the Data field.
//
// When the provided Log type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (l *Log) GetData() []byte {
// return zero value if Log type or Data field is nil
if l == nil || l.Data == nil {
return []byte{}
}
return *l.Data
}
// SetID sets the ID field.
//
// When the provided Log type is nil, it
// will set nothing and immediately return.
func (l *Log) SetID(v int64) {
// return if Log type is nil
if l == nil {
return
}
l.ID = &v
}
// SetBuildID sets the BuildID field.
//
// When the provided Log type is nil, it
// will set nothing and immediately return.
func (l *Log) SetBuildID(v int64) {
// return if Log type is nil
if l == nil {
return
}
l.BuildID = &v
}
// SetRepoID sets the RepoID field.
//
// When the provided Log type is nil, it
// will set nothing and immediately return.
func (l *Log) SetRepoID(v int64) {
// return if Log type is nil
if l == nil {
return
}
l.RepoID = &v
}
// SetServiceID sets the ServiceID field.
//
// When the provided Log type is nil, it
// will set nothing and immediately return.
func (l *Log) SetServiceID(v int64) {
// return if Log type is nil
if l == nil {
return
}
l.ServiceID = &v
}
// SetStepID sets the StepID field.
//
// When the provided Log type is nil, it
// will set nothing and immediately return.
func (l *Log) SetStepID(v int64) {
// return if Log type is nil
if l == nil {
return
}
l.StepID = &v
}
// SetData sets the Data field.
//
// When the provided Log type is nil, it
// will set nothing and immediately return.
func (l *Log) SetData(v []byte) {
// return if Log type is nil
if l == nil {
return
}
l.Data = &v
}
// String implements the Stringer interface for the Log type.
func (l *Log) String() string {
return fmt.Sprintf(`{
BuildID: %d,
Data: %s,
ID: %d,
RepoID: %d,
ServiceID: %d,
StepID: %d,
}`,
l.GetBuildID(),
l.GetData(),
l.GetID(),
l.GetRepoID(),
l.GetServiceID(),
l.GetStepID(),
)
}