-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhosts_manager.go
More file actions
59 lines (48 loc) · 1.83 KB
/
hosts_manager.go
File metadata and controls
59 lines (48 loc) · 1.83 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
package main
import (
"time"
"github.com/fsnotify/fsnotify"
)
// HostsManager defines the contract for manipulating the hosts file.
// This interface abstracts the file I/O operations, allowing for easy testing
// of components that depend on it.
type HostsManager interface {
// Apply takes a list of domains and configures the hosts file
// to block them. It must handle backing up the original file.
Apply(domains []string) error
// Restore reverts the hosts file to its original state from the backup.
Restore() error
}
// FileWatcher defines the contract for a file system event watcher.
type FileWatcher interface {
Events() <-chan fsnotify.Event
Errors() <-chan error
Add(name string) error
Close() error
}
// FsnotifyWatcher is the concrete implementation of our FileWatcher interface.
// It wraps the underlying fsnotify library to conform to the FileWatcher interface.
type FsnotifyWatcher struct {
watcher *fsnotify.Watcher
}
// NewFsnotifyWatcher creates a new instance of the concrete file watcher.
func NewFsnotifyWatcher() (FileWatcher, error) {
w, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
return &FsnotifyWatcher{watcher: w}, nil
}
func (fw *FsnotifyWatcher) Events() <-chan fsnotify.Event { return fw.watcher.Events }
func (fw *FsnotifyWatcher) Errors() <-chan error { return fw.watcher.Errors }
func (fw *FsnotifyWatcher) Add(name string) error { return fw.watcher.Add(name) }
func (fw *FsnotifyWatcher) Close() error { return fw.watcher.Close() }
// Scheduler defines the contract for a time-based scheduler.
type Scheduler interface {
AfterFunc(d time.Duration, f func())
}
// RealScheduler is the concrete implementation using Go's time package.
type RealScheduler struct{}
func (rs *RealScheduler) AfterFunc(d time.Duration, f func()) {
time.AfterFunc(d, f)
}