Skip to content

Commit 6a421be

Browse files
committed
handle rename cases
1 parent e0f8169 commit 6a421be

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

internal/watcher/credentials/credential_watcher_service.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package credentials
88
import (
99
"context"
1010
"log/slog"
11+
"slices"
1112
"sync"
1213
"sync/atomic"
1314
"time"
@@ -18,12 +19,6 @@ import (
1819
)
1920

2021
const (
21-
Create = fsnotify.Create
22-
Write = fsnotify.Write
23-
Remove = fsnotify.Remove
24-
Rename = fsnotify.Rename
25-
Chmod = fsnotify.Chmod
26-
2722
monitoringInterval = 5 * time.Second
2823
)
2924

@@ -150,6 +145,17 @@ func (cws *CredentialWatcherService) handleEvent(ctx context.Context, event fsno
150145
}
151146

152147
slog.DebugContext(ctx, "Processing FSNotify event", "event", event)
148+
149+
switch {
150+
case event.Has(fsnotify.Write):
151+
case event.Has(fsnotify.Create):
152+
case event.Has(fsnotify.Rename):
153+
if !slices.Contains(cws.watcher.WatchList(), event.Name) {
154+
cws.filesBeingWatched.Store(event.Name, false)
155+
}
156+
cws.addWatcher(ctx, event.Name)
157+
}
158+
153159
cws.filesChanged.Store(true)
154160
}
155161
}
@@ -183,8 +189,7 @@ func credentialPaths(agentConfig *config.Config) []string {
183189
func isEventSkippable(event fsnotify.Event) bool {
184190
return event == emptyEvent ||
185191
event.Name == "" ||
186-
event.Has(event.Op&Chmod) ||
187-
event.Has(event.Op&Create) ||
188-
event.Has(event.Op&Remove) ||
189-
event.Has(event.Op&Rename)
192+
event.Has(fsnotify.Chmod) ||
193+
event.Has(fsnotify.Create) ||
194+
event.Has(fsnotify.Remove)
190195
}

internal/watcher/credentials/credential_watcher_service_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ func TestCredentialWatcherService_handleEvent(t *testing.T) {
192192
assert.False(t, cws.filesChanged.Load())
193193
cws.handleEvent(ctx, fsnotify.Event{Name: "test-remove", Op: fsnotify.Remove})
194194
assert.False(t, cws.filesChanged.Load())
195-
cws.handleEvent(ctx, fsnotify.Event{Name: "test-rename", Op: fsnotify.Rename})
196-
assert.False(t, cws.filesChanged.Load())
197195
cws.handleEvent(ctx, fsnotify.Event{Name: "test-create", Op: fsnotify.Create})
198196
assert.False(t, cws.filesChanged.Load())
197+
cws.handleEvent(ctx, fsnotify.Event{Name: "test-rename", Op: fsnotify.Rename})
198+
assert.True(t, cws.filesChanged.Load())
199199
cws.handleEvent(ctx, fsnotify.Event{Name: "test-write", Op: fsnotify.Write})
200200
assert.True(t, cws.filesChanged.Load())
201201
}

0 commit comments

Comments
 (0)