-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
192 lines (164 loc) · 4.52 KB
/
record.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
package recmd
import (
"fmt"
"io"
"time"
"github.com/samber/lo"
)
type RecordFormat string
const (
FormatString RecordFormat = "string"
FormatBase64 RecordFormat = "base64"
)
type Record interface {
Format() RecordFormat
Command() string
StdOut() map[time.Duration][]byte
StdIn() map[time.Duration][]byte
StdErr() map[time.Duration][]byte
Reader() io.Reader
ConvertTo(format RecordFormat) (Record, error)
ExitCode() int
}
type ByteRecord struct {
JsonFormat RecordFormat `json:"format"`
Cmd string `json:"command"`
Out map[time.Duration][]byte `json:"out"`
In map[time.Duration][]byte `json:"in"`
Err map[time.Duration][]byte `json:"err"`
ExitC int `json:"exitcode"`
}
type StringRecord struct {
JsonFormat RecordFormat `json:"format"`
Cmd string `json:"command"`
Out map[time.Duration]string `json:"out"`
In map[time.Duration]string `json:"in"`
Err map[time.Duration]string `json:"err"`
ExitC int `json:"exitcode"`
}
// Reader returns a RecordReader object.
//
// It creates a map of time durations to byte slices from the Out, In, and Err fields of the Record object.
// It then sorts the time durations in ascending order and returns a new RecordReader object with the created map and the sorted time durations.
func (r *ByteRecord) Reader() io.Reader {
return NewReader(r)
}
func (br *ByteRecord) StdOut() map[time.Duration][]byte {
return br.Out
}
func (br *ByteRecord) StdIn() map[time.Duration][]byte {
return br.In
}
func (br *ByteRecord) StdErr() map[time.Duration][]byte {
return br.Err
}
func (br *ByteRecord) Command() string {
return br.Cmd
}
func (br *ByteRecord) ExitCode() int {
return br.ExitC
}
func (br *ByteRecord) Format() RecordFormat {
if br.JsonFormat == "" {
br.JsonFormat = FormatBase64
}
return br.JsonFormat
}
func (br *ByteRecord) ConvertTo(format RecordFormat) (Record, error) {
switch format {
case FormatString:
convert := func(byteMap map[time.Duration][]byte) map[time.Duration]string {
convertedMap := make(map[time.Duration]string)
for k, v := range byteMap {
convertedMap[k] = string(v)
}
return convertedMap
}
return &StringRecord{
Cmd: br.Cmd,
Out: convert(br.Out),
In: convert(br.In),
Err: convert(br.Err),
JsonFormat: FormatString,
}, nil
case FormatBase64:
return &ByteRecord{
Cmd: br.Cmd,
Out: cloneMap(br.Out),
In: cloneMap(br.In),
Err: cloneMap(br.Err),
JsonFormat: br.JsonFormat,
ExitC: br.ExitC,
}, nil
default:
return nil, fmt.Errorf("unknown format: %s", format)
}
}
func (sr *StringRecord) Reader() io.Reader {
return NewReader(sr)
}
func (sr *StringRecord) StdOut() map[time.Duration][]byte {
return lo.MapValues[time.Duration, string, []byte](sr.Out, func(value string, _ time.Duration) []byte {
return []byte(value)
})
}
func (sr *StringRecord) StdIn() map[time.Duration][]byte {
return lo.MapValues[time.Duration, string, []byte](sr.In, func(value string, _ time.Duration) []byte {
return []byte(value)
})
}
func (sr *StringRecord) StdErr() map[time.Duration][]byte {
return lo.MapValues[time.Duration, string, []byte](sr.Err, func(value string, _ time.Duration) []byte {
return []byte(value)
})
}
func (sr *StringRecord) Command() string {
return sr.Cmd
}
func (sr *StringRecord) ExitCode() int {
return sr.ExitC
}
func (sr *StringRecord) Format() RecordFormat {
if sr.JsonFormat == "" {
sr.JsonFormat = FormatString
}
return sr.JsonFormat
}
func (sr *StringRecord) ConvertTo(format RecordFormat) (Record, error) {
switch format {
case FormatString:
return &StringRecord{
Cmd: sr.Cmd,
Out: cloneMap(sr.Out),
In: cloneMap(sr.In),
Err: cloneMap(sr.Err),
JsonFormat: sr.JsonFormat,
ExitC: sr.ExitC,
}, nil
case FormatBase64:
convert := func(stringMap map[time.Duration]string) map[time.Duration][]byte {
convertedMap := make(map[time.Duration][]byte)
for k, v := range stringMap {
convertedMap[k] = []byte(v)
}
return convertedMap
}
return &ByteRecord{
Cmd: sr.Cmd,
Out: convert(sr.Out),
In: convert(sr.In),
Err: convert(sr.Err),
JsonFormat: FormatBase64,
ExitC: sr.ExitC,
}, nil
default:
return nil, fmt.Errorf("unknown format: %s", format)
}
}
func cloneMap[K comparable, V any](originalMap map[K]V) map[K]V {
clonedMap := make(map[K]V)
for key, value := range originalMap {
clonedMap[key] = value
}
return clonedMap
}