-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathifchanged.go
More file actions
195 lines (180 loc) · 6.95 KB
/
ifchanged.go
File metadata and controls
195 lines (180 loc) · 6.95 KB
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
package ifchanged
import (
"fmt"
"os"
)
// If is designed for a human-readable chaining of "ifs" followed by specification of If.Changed and/or If.Missing cases
// in which the If.Execute should be called. NewIf() initializes If struct for immediate chaining.
type If struct {
pairs []FileSHA256Pair
missing []string
}
// NewIf initializes a new *If struct, although it can be used as a zero-value.
func NewIf() *If {
return &If{}
}
// Changed appends a fileName / sha256 file rule to If structure to check against for modified files,
// in which case a function provided with Execute will be called. Several can be chained if needed.
func (i *If) Changed(fileName, sha256file string) *If {
i.pairs = append(i.pairs, FileSHA256Pair{
FileName: fileName,
Sha256file: sha256file,
})
return i
}
// Missing appends fileName(s) that serve as checks for missing files,
// in which case a function provided with Execute will be called.
func (i *If) Missing(fileName ...string) *If {
i.missing = append(i.missing, fileName...)
return i
}
// Execute is expected to be a final call in the chain with Changed / Missing as it doesn't return If back.
// Error returned never comes from calling f. If f does return error, then none of the sha256 files, provided using Changed,
// will be updated.
// Execute doesn't invalidate the If struct, so additional Changed / Missing / Execute are possible on top of existing data.
func (i *If) Execute(f func() error) error {
return usingFilesOrMissing(i.pairs, i.missing, f)
}
// UsingDB based on a key/value containing sha256 checksum in DB database.
// Runs execute if checksum has changed.
// When execute returns error, we don't update sha256.
// The function does not return any error out of provided execute function.
func UsingDB(fileName string, db DB, sha256key []byte, execute func() error) error {
fileInfo, err := os.Stat(fileName)
if os.IsNotExist(err) {
return fmt.Errorf("file not found: %s due to: %w", fileName, err)
}
if fileInfo.IsDir() {
return fmt.Errorf("file is a folder: %s due to: %w", fileName, err)
}
savedSha256 := ""
if db.Has(sha256key) {
val, err := db.Get(sha256key)
if err != nil {
return fmt.Errorf("DB error: %w", err)
}
if val == nil {
} else {
savedSha256 = string(val)
}
}
newSha256, err := GetFileSHA256(fileName)
if err != nil {
return fmt.Errorf("sha256 not found: %w", err)
}
if savedSha256 == "" || savedSha256 != newSha256 {
if err := execute(); err == nil { // We only want to update the key if execution happened without error
err = db.Put(sha256key, []byte(newSha256))
if err != nil {
return fmt.Errorf("error saving sha256 db value: %w", err)
}
err = db.Sync()
if err != nil {
return fmt.Errorf("error syncing sha256 db value: %w", err)
}
}
}
return nil
}
// UsingFile checks for a sha256 checksum in a sha256file.
// Runs executeIfChanged if new checksum doesn't match previous sha256.
// executeIfChanged can return an error, in which case the sha256file generation / update will be skipped.
// The function does not return any error from executeIfChanged. It has to be handled by the caller.
func UsingFile(fileName string, sha256file string, executeIfChanged func() error) error {
return usingFilesOrMissing([]FileSHA256Pair{{
FileName: fileName,
Sha256file: sha256file,
}}, nil, executeIfChanged)
}
// Deprecated: Use NewIf followed by If.Changed and If.Missing, that works for both a list of modified and a list of possibly missing files.
//
// UsingFileOrMissing acts as UsingFile, but additionally executes executeIfChanged
// when a file checkMissingFileName is missing. This assumes that executeIfChanged generates the
// file, so it serves as a sign that operation is necessary to perform.
func UsingFileOrMissing(fileName string, sha256file string, checkMissingFileName string, execute func() error) error {
return usingFilesOrMissing([]FileSHA256Pair{{
FileName: fileName,
Sha256file: sha256file,
}}, []string{checkMissingFileName}, execute)
}
// UsingFiles executes executeIfChanged
// when any of the files listed with checkMissingFiles is missing. executeIfChanged generates the
// file(s), so it serves as a sign that operation is necessary to perform.
// UsingFileOrMissing does that for a just one file (kept for backward compatibility).
func UsingFiles(fileSHA256Pairs []FileSHA256Pair, checkMissingFiles []string, execute func() error) error {
return usingFilesOrMissing(fileSHA256Pairs, checkMissingFiles, execute)
}
type FileSHA256Pair struct {
FileName string
Sha256file string
currentSha256 string
newSha256 string
}
// usingFilesOrMissing based on a file that contains sha256 checksum.
// Runs `executeIfChanged` if checksum has changed.
// If `executeIfChanged` returns error, we don't update sha256.
// The function is not retuning error of executeIfChanged() if happened.
func usingFilesOrMissing(fileSHA256Pairs []FileSHA256Pair, missingFiles []string, executeIfChanged func() error) error {
missingDetected := false
for _, f := range missingFiles {
checkMissingFileInfo, err := os.Stat(f)
if err != nil {
if os.IsNotExist(err) {
missingDetected = true
break // One file missing is enough to force regeneration
} else {
return fmt.Errorf("file opening error: %s due to: %w", checkMissingFileInfo, err)
}
} else if checkMissingFileInfo.IsDir() {
return fmt.Errorf("checkMissingFileName is a folder: %s due to: %w", f, err)
}
}
changeDetected := false
for i, filePair := range fileSHA256Pairs {
fileName := filePair.FileName
sha256file := filePair.Sha256file
fileInfo, err := os.Stat(fileName)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("file not found: %s due to: %w", fileName, err)
} else {
return fmt.Errorf("file opening error: %s due to: %w", fileName, err)
}
} else if fileInfo.IsDir() {
return fmt.Errorf("file is a folder: %s due to: %w", fileName, err)
}
newSha256, err := GetFileSHA256(fileName)
if err != nil {
return fmt.Errorf("sha256 not found: %w", err)
}
fileSHA256Pairs[i].newSha256 = newSha256
currentSha256 := ""
if !missingDetected { // No need to read old sha256 if we have to overwrite it anyway
sha256FileInfo, err := os.Stat(sha256file)
if err == nil && sha256FileInfo.IsDir() {
return fmt.Errorf("sha256 is a folder: %w", err)
}
if !os.IsNotExist(err) && !sha256FileInfo.IsDir() {
currentSha256, err = ReadFileAsString(sha256file)
if err != nil {
return fmt.Errorf("error reading existing sha256 file: %w", err)
}
fileSHA256Pairs[i].currentSha256 = currentSha256
}
}
if currentSha256 == "" || currentSha256 != newSha256 {
changeDetected = true
}
}
if missingDetected || changeDetected {
if err := executeIfChanged(); err == nil { // We only want to update the key if execution happened without error
for _, f := range fileSHA256Pairs {
err = SaveSHA256(f.newSha256, f.Sha256file)
if err != nil {
return fmt.Errorf("error saving sha256 file: %w", err)
}
}
}
}
return nil
}