-
Notifications
You must be signed in to change notification settings - Fork 0
/
manifests.go
200 lines (173 loc) · 4.19 KB
/
manifests.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
package main
import (
"encoding/json"
"fmt"
"io"
"path"
"strings"
)
// manifestType represents an identifier for a manifest output format.
type manifestType int
const (
// No manifest output
NoManifest manifestType = iota
// Fully detailed manifest using JSON
JsonManifest
// MD5 checksum in hexadecimal and bucket/key path
FullMD5Manifest
// Configured checksum in hexadecimal and bucket/key path
FullChecksumManifest
// hash-of-hashes checksum in base64 and bucket/key path
AWSChecksumManifest
// AWS ETag and bucket/key path
ETagManifest
)
// ManifestType represents a manifestType, with helper functions to parse and
// produce human readable and produce human readable representations of the
// identifier for use via the flag module.
type ManifestType manifestType
func (p ManifestType) String() string {
switch manifestType(p) {
case JsonManifest:
return "json"
case FullMD5Manifest:
return "md5"
case FullChecksumManifest:
return "checksum"
case AWSChecksumManifest:
return "aws"
case ETagManifest:
return "etag"
default:
return "none"
}
}
func (p *ManifestType) Set(s string) error {
switch strings.ToLower(s) {
case "json":
*p = ManifestType(JsonManifest)
case "md5":
*p = ManifestType(FullMD5Manifest)
case "checksum":
*p = ManifestType(FullChecksumManifest)
case "aws":
*p = ManifestType(AWSChecksumManifest)
case "etag":
*p = ManifestType(ETagManifest)
case "none":
*p = ManifestType(NoManifest)
default:
return fmt.Errorf("valid manifest types: json, md5, checksum, aws, etag")
}
return nil
}
// Manifest returns a manifest generator for the specified manifestType,
// writing the results to the provided io.Writer.
func Manifest(t manifestType, w io.Writer) *manifestGenerator {
return &manifestGenerator{
w: w,
t: t,
nrec: 0,
}
}
type manifestGenerator struct {
w io.Writer
t manifestType
nrec int
}
// End writes trailing text to its io.Writer to indicate the end of the
// manifest, e.g., with JSON it writes the closing brace for a JSON array.
func (p *manifestGenerator) End() error {
if p.t == NoManifest {
return nil
}
if p.nrec == 0 {
return nil
}
var s string
if p.t == JsonManifest {
s = "\n]\n"
} else {
s = "\n"
}
_, err := io.WriteString(p.w, s)
return err
}
// Write writes another record for the manifest.
func (p *manifestGenerator) Write(obj *ObjectReporting) error {
// increment record counter
p.nrec += 1
// write the formatted record to p.w
switch p.t {
case NoManifest:
return nil
case JsonManifest:
buf, err := json.MarshalIndent(obj, " ", " ")
if err != nil {
return err
}
if p.nrec == 1 {
// start of JSON array
if _, err := io.WriteString(p.w, "[\n "); err != nil {
return err
}
} else {
// end of prior record in JSON array
if _, err := io.WriteString(p.w, ",\n "); err != nil {
return err
}
}
// write current record in JSON array
if _, err := p.w.Write(buf); err != nil {
return err
}
default:
var val string
switch p.t {
case FullMD5Manifest:
val = obj.FullChecksums.ChecksumMD5.Hex
case FullChecksumManifest:
for _, c := range []*ObjectChecksum{
obj.FullChecksums.ChecksumSHA256,
obj.FullChecksums.ChecksumSHA1,
obj.FullChecksums.ChecksumCRC32C,
obj.FullChecksums.ChecksumCRC32,
} {
if c != nil {
val = c.Hex
break
}
}
case AWSChecksumManifest:
for _, c := range []*ObjectChecksum{
obj.ObjectAttributes.Checksum.ChecksumSHA256,
obj.ObjectAttributes.Checksum.ChecksumSHA1,
obj.ObjectAttributes.Checksum.ChecksumCRC32C,
obj.ObjectAttributes.Checksum.ChecksumCRC32,
} {
if c != nil {
val = c.Base64
break
}
}
case ETagManifest:
val = *obj.ObjectAttributes.ETag
}
if val == "" {
return fmt.Errorf("error processing %v: unable to extract field value", p.t)
}
if p.nrec > 1 {
// end of prior record in text manifest
if _, err := io.WriteString(p.w, "\n"); err != nil {
return err
}
}
// current record in text manifest (note that there are two
// spaces between the fields)c
s := fmt.Sprintf("%s %s", val, path.Join(obj.Bucket, obj.Key))
if _, err := io.WriteString(p.w, s); err != nil {
return err
}
}
return nil
}