Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Watch
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Container Registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Utilities
Watch
Commits
6a0b826d
Verified
Commit
6a0b826d
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
feat: implement
#1
parent
b81a2b5f
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lighthouse.go
+67
-0
67 additions, 0 deletions
lighthouse.go
lighthouse_test.go
+79
-0
79 additions, 0 deletions
lighthouse_test.go
with
146 additions
and
0 deletions
lighthouse.go
+
67
−
0
View file @
6a0b826d
package
watch
import
(
"fmt"
"github.com/fsnotify/fsnotify"
"path/filepath"
"sync"
...
...
@@ -57,6 +58,11 @@ type Lighthouse interface {
SetOnError
(
callback
EventErrorCallback
)
SetDebounce
(
duration
time
.
Duration
)
WatchList
()
[]
string
Sync
()
error
IsInSync
()
bool
}
// NewLighthouse creates a new lighthouse instance.
...
...
@@ -102,6 +108,67 @@ func (l *lighthouse) checkAndInit() {
}
// WatchList returns a list of all watched paths.
func
(
l
*
lighthouse
)
WatchList
()
[]
string
{
l
.
mutex
.
Lock
()
defer
l
.
mutex
.
Unlock
()
var
list
[]
string
for
k
:=
range
l
.
watchers
{
list
=
append
(
list
,
k
)
}
return
list
}
// IsInSync returns true if all paths are watched by fsnotify.
func
(
l
*
lighthouse
)
IsInSync
()
bool
{
l
.
mutex
.
Lock
()
defer
l
.
mutex
.
Unlock
()
wl
:=
l
.
fsnotify
.
WatchList
()
if
len
(
wl
)
!=
len
(
l
.
watchers
)
{
return
false
}
for
_
,
w
:=
range
wl
{
if
_
,
ok
:=
l
.
watchers
[
w
];
!
ok
{
return
false
}
}
return
true
}
// Sync Lighthouse and fsnotify. Remove all watchers from fsnotify and add them again.
func
(
l
*
lighthouse
)
Sync
()
error
{
l
.
mutex
.
Lock
()
defer
l
.
mutex
.
Unlock
()
// remove all watchers from fsnotify
wl
:=
l
.
fsnotify
.
WatchList
()
for
_
,
w
:=
range
wl
{
_
=
l
.
fsnotify
.
Remove
(
w
)
}
// add all watchers to fsnotify
var
errReturn
error
for
k
:=
range
l
.
watchers
{
err
:=
l
.
fsnotify
.
Add
(
k
)
if
err
!=
nil
{
if
errReturn
==
nil
{
errReturn
=
err
}
else
{
errReturn
=
fmt
.
Errorf
(
"%w, %v"
,
errReturn
,
err
)
}
}
}
return
errReturn
}
// IsRunning returns true if the watcher is active, false otherwise.
func
(
l
*
lighthouse
)
IsRunning
()
bool
{
l
.
mutex
.
Lock
()
...
...
This diff is collapsed.
Click to expand it.
lighthouse_test.go
+
79
−
0
View file @
6a0b826d
...
...
@@ -84,3 +84,82 @@ func TestIsActiveWatched(t *testing.T) {
assert
.
True
(
t
,
l
.
IsActiveWatched
(
tempDir
))
assert
.
False
(
t
,
l
.
IsActiveWatched
(
"path/not/watched"
))
}
func
TestWatchList
(
t
*
testing
.
T
)
{
tempDir
,
err
:=
os
.
MkdirTemp
(
""
,
"watchtest"
)
assert
.
Nil
(
t
,
err
)
defer
func
()
{
_
=
os
.
RemoveAll
(
tempDir
)
// Cleanup
}()
l
:=
NewLighthouse
()
w
:=
&
Watch
{
Path
:
tempDir
}
err
=
l
.
Add
(
w
)
assert
.
Nil
(
t
,
err
)
assert
.
Equal
(
t
,
1
,
len
(
l
.
WatchList
()))
assert
.
Equal
(
t
,
tempDir
,
l
.
WatchList
()[
0
])
}
// test isinsync
func
TestIsInSync
(
t
*
testing
.
T
)
{
tempDir
,
err
:=
os
.
MkdirTemp
(
""
,
"watchtest"
)
assert
.
Nil
(
t
,
err
)
tempFile
,
err
:=
os
.
CreateTemp
(
tempDir
,
"test"
)
assert
.
Nil
(
t
,
err
)
defer
func
()
{
_
=
os
.
RemoveAll
(
tempDir
)
// Cleanup
_
=
os
.
RemoveAll
(
tempFile
.
Name
())
// Cleanup
}()
l
:=
NewLighthouse
()
w
:=
&
Watch
{
Path
:
tempDir
}
err
=
l
.
Add
(
w
)
assert
.
Nil
(
t
,
err
)
w2
:=
&
Watch
{
Path
:
tempFile
.
Name
()}
err
=
l
.
Add
(
w2
)
assert
.
Nil
(
t
,
err
)
err
=
l
.
Sync
()
assert
.
Nil
(
t
,
err
)
assert
.
True
(
t
,
l
.
IsInSync
())
internal
:=
l
.
(
*
lighthouse
)
internal
.
fsnotify
.
Remove
(
tempFile
.
Name
())
assert
.
False
(
t
,
l
.
IsInSync
())
}
// test Sync
func
TestSync
(
t
*
testing
.
T
)
{
tempDir
,
err
:=
os
.
MkdirTemp
(
""
,
"watchtest"
)
assert
.
Nil
(
t
,
err
)
tempFile
,
err
:=
os
.
CreateTemp
(
tempDir
,
"test"
)
assert
.
Nil
(
t
,
err
)
defer
func
()
{
_
=
os
.
RemoveAll
(
tempDir
)
// Cleanup
_
=
os
.
RemoveAll
(
tempFile
.
Name
())
// Cleanup
}()
l
:=
NewLighthouse
()
w
:=
&
Watch
{
Path
:
tempDir
}
err
=
l
.
Add
(
w
)
assert
.
Nil
(
t
,
err
)
w2
:=
&
Watch
{
Path
:
tempFile
.
Name
()}
err
=
l
.
Add
(
w2
)
assert
.
Nil
(
t
,
err
)
err
=
l
.
Sync
()
assert
.
Nil
(
t
,
err
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment