diff options
| author | Shulhan <ms@kilabit.info> | 2022-03-06 01:33:06 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-03-06 01:33:06 +0700 |
| commit | 6480be9802ac26c603dc55f6bb2f1a447e1a8ca9 (patch) | |
| tree | cfe6fe6a8b7b482068731b35fbab74ae6f71d9de | |
| parent | 85fb0214ebb5759d0e3c7effc4b5ad2c926d5955 (diff) | |
| download | pakakeh.go-6480be9802ac26c603dc55f6bb2f1a447e1a8ca9.tar.xz | |
all: move the DirWatcher and Watcher types from io to memfs
There are two reasons why we move them.
First, DirWatcher and Watcher code internally depends on the memfs
package, especially on Node type.
Second, we want to add new Watch method to MemFS which depends on
package io.
If we do that, there will be circular imports.
| -rw-r--r-- | lib/io/io.go | 3 | ||||
| -rw-r--r-- | lib/memfs/dirwatcher.go (renamed from lib/io/dirwatcher.go) | 29 | ||||
| -rw-r--r-- | lib/memfs/dirwatcher_test.go (renamed from lib/io/dirwatcher_test.go) | 7 | ||||
| -rw-r--r-- | lib/memfs/filestate.go (renamed from lib/io/filestate.go) | 2 | ||||
| -rw-r--r-- | lib/memfs/nodestate.go (renamed from lib/io/nodestate.go) | 8 | ||||
| -rw-r--r-- | lib/memfs/watchcallback.go (renamed from lib/io/watchcallback.go) | 2 | ||||
| -rw-r--r-- | lib/memfs/watcher.go (renamed from lib/io/watcher.go) | 11 | ||||
| -rw-r--r-- | lib/memfs/watcher_test.go (renamed from lib/io/watcher_test.go) | 2 |
8 files changed, 28 insertions, 36 deletions
diff --git a/lib/io/io.go b/lib/io/io.go index 22e30f0a..1312b023 100644 --- a/lib/io/io.go +++ b/lib/io/io.go @@ -3,8 +3,7 @@ // license that can be found in the LICENSE file. // -// Package io provide a library for reading and watching file, and reading -// from standard input. +// Package io extends the standard io library. // package io diff --git a/lib/io/dirwatcher.go b/lib/memfs/dirwatcher.go index 9ba59061..6ad31867 100644 --- a/lib/io/dirwatcher.go +++ b/lib/memfs/dirwatcher.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io +package memfs import ( "fmt" @@ -12,15 +12,14 @@ import ( "time" "github.com/shuLhan/share/lib/debug" - "github.com/shuLhan/share/lib/memfs" ) // // DirWatcher is a naive implementation of directory change notification. // type DirWatcher struct { - root *memfs.Node - fs *memfs.MemFS + root *Node + fs *MemFS ticker *time.Ticker // Callback define a function that will be called when change detected @@ -31,9 +30,9 @@ type DirWatcher struct { // being watched for changes. // The map key is relative path to directory and its value is a node // information. - dirs map[string]*memfs.Node + dirs map[string]*Node - // This struct embed memfs.Options to map the directory to be watched + // This struct embed Options to map the directory to be watched // into memory. // // The Root field define the directory that we want to watch. @@ -43,7 +42,7 @@ type DirWatcher struct { // // Excludes contains list of regex to filter file names that we did // not want to be notified. - memfs.Options + Options // Delay define a duration when the new changes will be fetched from // system. @@ -75,14 +74,14 @@ func (dw *DirWatcher) Start() (err error) { dw.Options.MaxFileSize = -1 - dw.fs, err = memfs.New(&dw.Options) + dw.fs, err = New(&dw.Options) if err != nil { return fmt.Errorf("%s: %w", logp, err) } dw.root = dw.fs.Root - dw.dirs = make(map[string]*memfs.Node) + dw.dirs = make(map[string]*Node) dw.mapSubdirs(dw.root) go dw.start() @@ -113,7 +112,7 @@ func (dw *DirWatcher) dirsKeys() (keys []string) { // the childs. // If its a regular file, start a NewWatcher. // -func (dw *DirWatcher) mapSubdirs(node *memfs.Node) { +func (dw *DirWatcher) mapSubdirs(node *Node) { var ( logp = "DirWatcher.mapSubdirs" err error @@ -136,7 +135,7 @@ func (dw *DirWatcher) mapSubdirs(node *memfs.Node) { // unmapSubdirs find sub directories in node's childrens, recursively and // remove it from map of node. // -func (dw *DirWatcher) unmapSubdirs(node *memfs.Node) { +func (dw *DirWatcher) unmapSubdirs(node *Node) { for _, child := range node.Childs { if child.IsDir() { delete(dw.dirs, child.Path) @@ -156,7 +155,7 @@ func (dw *DirWatcher) unmapSubdirs(node *memfs.Node) { // It will re-read the list of files in node directory and compare them with // old content to detect deletion and addition of files. // -func (dw *DirWatcher) onContentChange(node *memfs.Node) { +func (dw *DirWatcher) onContentChange(node *Node) { var ( logp = "onContentChange" ) @@ -261,7 +260,7 @@ func (dw *DirWatcher) onRootCreated() { err error ) - dw.fs, err = memfs.New(&dw.Options) + dw.fs, err = New(&dw.Options) if err != nil { log.Printf("%s: %s", logp, err) return @@ -273,7 +272,7 @@ func (dw *DirWatcher) onRootCreated() { return } - dw.dirs = make(map[string]*memfs.Node) + dw.dirs = make(map[string]*Node) dw.mapSubdirs(dw.root) ns := &NodeState{ @@ -314,7 +313,7 @@ func (dw *DirWatcher) onRootDeleted() { // onModified handle change when permission or attribute on node directory // changed. // -func (dw *DirWatcher) onModified(node *memfs.Node, newDirInfo os.FileInfo) { +func (dw *DirWatcher) onModified(node *Node, newDirInfo os.FileInfo) { dw.fs.Update(node, newDirInfo) ns := &NodeState{ diff --git a/lib/io/dirwatcher_test.go b/lib/memfs/dirwatcher_test.go index f6c40fc6..28767e8e 100644 --- a/lib/io/dirwatcher_test.go +++ b/lib/memfs/dirwatcher_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io +package memfs import ( "fmt" @@ -15,7 +15,6 @@ import ( "testing" "time" - "github.com/shuLhan/share/lib/memfs" "github.com/shuLhan/share/lib/test" ) @@ -73,7 +72,7 @@ func TestDirWatcher(t *testing.T) { var x int32 dw := &DirWatcher{ - Options: memfs.Options{ + Options: Options{ Root: dir, Includes: []string{ `assets/.*`, @@ -256,7 +255,7 @@ func TestDirWatcher_renameDirectory(t *testing.T) { Callback: func(ns *NodeState) { nsq <- ns }, - Options: memfs.Options{ + Options: Options{ Root: rootDir, }, Delay: 200 * time.Millisecond, diff --git a/lib/io/filestate.go b/lib/memfs/filestate.go index 3532068d..6dc1bbcc 100644 --- a/lib/io/filestate.go +++ b/lib/memfs/filestate.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io +package memfs // FileState define the state of file. // There are four states of file: created, updated on mode, updated on content diff --git a/lib/io/nodestate.go b/lib/memfs/nodestate.go index 400c66bc..ba6001c7 100644 --- a/lib/io/nodestate.go +++ b/lib/memfs/nodestate.go @@ -2,18 +2,14 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io - -import ( - "github.com/shuLhan/share/lib/memfs" -) +package memfs // // NodeState contains the information about the file and its state. // type NodeState struct { // Node represent the file information. - Node *memfs.Node + Node *Node // State of file, its either created, modified, or deleted. State FileState } diff --git a/lib/io/watchcallback.go b/lib/memfs/watchcallback.go index 475028bf..b965c988 100644 --- a/lib/io/watchcallback.go +++ b/lib/memfs/watchcallback.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io +package memfs // // WatchCallback is a function that will be called when Watcher or DirWatcher diff --git a/lib/io/watcher.go b/lib/memfs/watcher.go index f4c2e2e7..b021e0d4 100644 --- a/lib/io/watcher.go +++ b/lib/memfs/watcher.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io +package memfs import ( "fmt" @@ -12,14 +12,13 @@ import ( "time" "github.com/shuLhan/share/lib/debug" - "github.com/shuLhan/share/lib/memfs" ) // // Watcher is a naive implementation of file event change notification. // type Watcher struct { - node *memfs.Node + node *Node ticker *time.Ticker // cb define a function that will be called when file modified or @@ -58,7 +57,7 @@ func NewWatcher(path string, d time.Duration, cb WatchCallback) (w *Watcher, err return nil, fmt.Errorf("%s: path is directory", logp) } - dummyParent := &memfs.Node{ + dummyParent := &Node{ SysPath: filepath.Dir(path), } dummyParent.Path = dummyParent.SysPath @@ -68,13 +67,13 @@ func NewWatcher(path string, d time.Duration, cb WatchCallback) (w *Watcher, err // newWatcher create and initialize new Watcher like NewWatcher but using // parent node. -func newWatcher(parent *memfs.Node, fi os.FileInfo, d time.Duration, cb WatchCallback) ( +func newWatcher(parent *Node, fi os.FileInfo, d time.Duration, cb WatchCallback) ( w *Watcher, err error, ) { logp := "newWatcher" // Create new node based on FileInfo without caching the content. - node, err := memfs.NewNode(parent, fi, -1) + node, err := NewNode(parent, fi, -1) if err != nil { return nil, fmt.Errorf("%s: %w", logp, err) } diff --git a/lib/io/watcher_test.go b/lib/memfs/watcher_test.go index 55b131df..246bc56b 100644 --- a/lib/io/watcher_test.go +++ b/lib/memfs/watcher_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package io +package memfs import ( "io/ioutil" |
