-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfileinfo.go
69 lines (58 loc) · 1.66 KB
/
fileinfo.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
package artifact_diff
import (
"fmt"
"log"
"sort"
"strings"
)
type FileInfo struct {
Path string `json:"path" yaml:"path"`
Filesize int64 `json:"filesize" yaml:"filesize"`
Checksum string `json:"checksum" yaml:"checksum"`
}
func (fi *FileInfo) String() string {
return fmt.Sprintf("path=%s, filesize=%v, checksum=%s", fi.Path, fi.Filesize, fi.Checksum)
}
type FileInfos map[string]*FileInfo
func (i FileInfos) String() string {
values := make([]string, 0, len(i))
for _, info := range i {
values = append(values, info.String())
}
return fmt.Sprintf(strings.Join(values, ""))
}
type ArtifactInfo struct {
Path string `json:"path" yaml:"path"`
Count int `json:"count" yaml:"count"`
FileInfos FileInfos `json:"fileInfos" yaml:"fileInfos"`
}
type FlatArtifactInfo struct {
Path string `json:"path" yaml:"path"`
Count int `json:"count" yaml:"count"`
FileInfos []*FileInfo `json:"fileInfos" yaml:"fileInfos"`
}
func (d *ArtifactInfo) WithFlattenedAndSortedFileInfos() *FlatArtifactInfo {
flat := &FlatArtifactInfo{
Path: d.Path,
Count: d.Count,
}
infos := make([]*FileInfo, 0, len(d.FileInfos))
for _, f := range d.FileInfos {
infos = append(infos, f)
}
sort.Slice(infos, func(i, j int) bool {
return infos[i].Path < infos[j].Path
})
flat.FileInfos = infos
return flat
}
func (d *ArtifactInfo) AddFileInfo(path string, info *FileInfo) {
pathMd5 := Md5hash(path)
if _, ok := d.FileInfos[pathMd5]; ok {
log.Println(fmt.Sprintf("duplicate path for %s?", path))
}
d.FileInfos[pathMd5] = info
}
func (d *ArtifactInfo) String() string {
return fmt.Sprintf("Count=%v, FileInfos=%v", d.Count, d.FileInfos)
}