-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
changes.go
129 lines (111 loc) · 3.07 KB
/
changes.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
/*
Copyright 2019 The Skaffold Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package filemon
import (
"context"
"fmt"
"os"
"sort"
"strings"
"time"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
)
// FileMap is a map of filename to modification times.
type FileMap map[string]time.Time
// Stat returns the modification times for a list of files.
func Stat(deps func() ([]string, error)) (FileMap, error) {
state := FileMap{}
paths, err := deps()
if err != nil {
return state, fmt.Errorf("listing files: %w", err)
}
for _, path := range paths {
stat, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
log.Entry(context.TODO()).Debugf("could not stat dependency: %s", err)
continue // Ignore files that don't exist
}
return nil, fmt.Errorf("unable to stat file %q: %w", path, err)
}
state[path] = stat.ModTime()
}
return state, nil
}
type Events struct {
Added []string
Modified []string
Deleted []string
}
func (e Events) HasChanged() bool {
return len(e.Added) != 0 || len(e.Deleted) != 0 || len(e.Modified) != 0
}
func (e *Events) String() string {
added, deleted, modified := len(e.Added), len(e.Deleted), len(e.Modified)
var sb strings.Builder
if added > 0 {
sb.WriteString(fmt.Sprintf("[watch event] added: %s\n", e.Added))
}
if deleted > 0 {
sb.WriteString(fmt.Sprintf("[watch event] deleted: %s\n", e.Deleted))
}
if modified > 0 {
sb.WriteString(fmt.Sprintf("[watch event] modified: %s\n", e.Modified))
}
return sb.String()
}
func events(prev, curr FileMap) Events {
e := Events{}
for f, t := range prev {
modtime, ok := curr[f]
if !ok {
// file in prev but not in curr -> file deleted
e.Deleted = append(e.Deleted, f)
continue
}
if !modtime.Equal(t) {
// file in both prev and curr
// time not equal -> file modified
e.Modified = append(e.Modified, f)
continue
}
}
for f := range curr {
// don't need to check case where file is in both curr and prev
// covered above
_, ok := prev[f]
if !ok {
// file in curr but not in prev -> file added
e.Added = append(e.Added, f)
}
}
sortEvents(e)
logEvents(e)
return e
}
func sortEvents(e Events) {
sort.Strings(e.Added)
sort.Strings(e.Modified)
sort.Strings(e.Deleted)
}
func logEvents(e Events) {
if e.Added != nil && len(e.Added) > 0 {
log.Entry(context.TODO()).Infof("files added: %v", e.Added)
}
if e.Modified != nil && len(e.Modified) > 0 {
log.Entry(context.TODO()).Infof("files modified: %v", e.Modified)
}
if e.Deleted != nil && len(e.Deleted) > 0 {
log.Entry(context.TODO()).Infof("files deleted: %v", e.Deleted)
}
}