-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
146 lines (123 loc) · 4.02 KB
/
utils.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
package docdb
import (
"bytes"
"context"
"reflect"
"github.com/ungerik/go-fs"
"github.com/domonda/go-errs"
"github.com/domonda/go-types/uu"
)
// func HasChangedCheckedOutFiles(docID uu.ID) (changed bool, err error) {
// defer errs.WrapWithFuncParams(&err, docID)
// status, err := DocumentCheckOutStatus(docID)
// if err != nil {
// return false, err
// }
// if status == nil {
// return false, ErrDocumentNotCheckedOut(docID)
// }
// checkedOutDir := CheckedOutDocumentDir(docID)
// version, err := LatestDocumentVersion(docID)
// if err != nil {
// return false, err
// }
// checkedInFiles, err := DocumentVersionInfo(docID, version)
// if err != nil {
// return false, err
// }
// checkedInFileNames := make(map[string]struct{}, len(checkedInFiles))
// for _, checkedIn := range checkedInFiles {
// checkedOut, err := checkedOutDir.Join(checkedIn.Name).StatWithContentHash()
// if err != nil {
// return false, err
// }
// if !checkedOut.Exists || checkedOut.IsDir || checkedOut.Size != checkedIn.Size || checkedOut.ContentHash != checkedIn.Hash {
// return true, nil
// }
// checkedInFileNames[checkedIn.Name] = struct{}{}
// }
// errExtraFile := errors.New("errExtraFile")
// err = checkedOutDir.ListDir(func(file fs.File) error {
// if _, isCheckIn := checkedInFileNames[file.Name()]; !isCheckIn {
// return errExtraFile
// }
// return nil
// })
// if err == errExtraFile {
// return true, nil
// }
// if err != nil {
// return false, err
// }
// return false, nil
// }
func IdenticalDocumentVersionsOfDrivers(ctx context.Context, docID uu.ID, driverA Conn, versionA VersionTime, driverB Conn, versionB VersionTime) (identical bool, err error) {
defer errs.WrapWithFuncParams(&err, docID, driverA, versionA, driverB, versionB)
fileInfosA, err := driverA.DocumentVersionInfo(ctx, docID, versionA)
if err != nil {
return false, err
}
fileInfosB, err := driverB.DocumentVersionInfo(ctx, docID, versionB)
if err != nil {
return false, err
}
return reflect.DeepEqual(fileInfosA, fileInfosB), nil
}
func LatestDocumentVersionFileProvider(ctx context.Context, docID uu.ID) (p FileProvider, err error) {
defer errs.WrapWithFuncParams(&err, ctx, docID)
version, err := conn.LatestDocumentVersion(ctx, docID)
if err != nil {
return nil, err
}
return conn.DocumentVersionFileProvider(ctx, docID, version)
}
func FirstDocumentVersionCommitUserID(ctx context.Context, docID uu.ID) (userID uu.ID, err error) {
defer errs.WrapWithFuncParams(&err, ctx, docID)
versions, err := conn.DocumentVersions(ctx, docID)
if err != nil {
return uu.IDNil, err
}
if len(versions) == 0 {
return uu.IDNil, NewErrDocumentHasNoCommitedVersion(docID)
}
versionInfo, err := conn.DocumentVersionInfo(ctx, docID, versions[0])
if err != nil {
return uu.IDNil, err
}
return versionInfo.CommitUserID, nil
}
func CheckConnDocumentVersionFiles(ctx context.Context, conn Conn, docID uu.ID, version VersionTime, expectedFiles []fs.FileReader) (err error) {
defer errs.WrapWithFuncParams(&err, ctx, conn, docID, version, expectedFiles)
info, err := conn.DocumentVersionInfo(ctx, docID, version)
if err != nil {
return err
}
if len(info.Files) != len(expectedFiles) {
return errs.Errorf("document %s version %s has %d files, expected %d", docID, version, len(info.Files), len(expectedFiles))
}
provider, err := conn.DocumentVersionFileProvider(ctx, docID, version)
if err != nil {
return err
}
for _, expectedFile := range expectedFiles {
hasFile, err := provider.HasFile(expectedFile.Name())
if err != nil {
return err
}
if !hasFile {
return errs.Errorf("document %s version %s is missing file %s", docID, version, expectedFile.Name())
}
expectedData, err := expectedFile.ReadAll()
if err != nil {
return err
}
data, err := provider.ReadFile(ctx, expectedFile.Name())
if err != nil {
return err
}
if !bytes.Equal(data, expectedData) {
return errs.Errorf("document %s version %s file %s content not as expected", docID, version, expectedFile.Name())
}
}
return nil
}