From 73953a85e873ccb6a9d87b71d23099a8dbe827df Mon Sep 17 00:00:00 2001 From: Graham Clark Date: Fri, 8 Jul 2022 22:36:18 -0400 Subject: [PATCH] Some more involved refactoring to move confwatcher to pkg --- cmd/termshark/termshark.go | 9 +++++---- .../confwatcher/confwatcher.go | 17 ++++++++++------- 2 files changed, 15 insertions(+), 11 deletions(-) rename confwatcher.go => pkg/confwatcher/confwatcher.go (86%) diff --git a/cmd/termshark/termshark.go b/cmd/termshark/termshark.go index 7f968d9..607a7cc 100644 --- a/cmd/termshark/termshark.go +++ b/cmd/termshark/termshark.go @@ -20,14 +20,15 @@ import ( "github.com/blang/semver" "github.com/gcla/gowid" "github.com/gcla/termshark/v2" - "github.com/gcla/termshark/v2/pkg/capinfo" "github.com/gcla/termshark/v2/cli" "github.com/gcla/termshark/v2/configs/profiles" + "github.com/gcla/termshark/v2/pkg/capinfo" + "github.com/gcla/termshark/v2/pkg/confwatcher" "github.com/gcla/termshark/v2/pkg/convs" "github.com/gcla/termshark/v2/pkg/pcap" - "github.com/gcla/termshark/v2/pkg/summary" "github.com/gcla/termshark/v2/pkg/shark" "github.com/gcla/termshark/v2/pkg/streams" + "github.com/gcla/termshark/v2/pkg/summary" "github.com/gcla/termshark/v2/pkg/system" "github.com/gcla/termshark/v2/pkg/tty" "github.com/gcla/termshark/v2/ui" @@ -56,7 +57,6 @@ func main() { // stopped. Any exception is a bug. var ensureGoroutinesStopWG sync.WaitGroup filter.Goroutinewg = &ensureGoroutinesStopWG - termshark.Goroutinewg = &ensureGoroutinesStopWG pcap.Goroutinewg = &ensureGoroutinesStopWG streams.Goroutinewg = &ensureGoroutinesStopWG capinfo.Goroutinewg = &ensureGoroutinesStopWG @@ -64,6 +64,7 @@ func main() { ui.Goroutinewg = &ensureGoroutinesStopWG wormhole.Goroutinewg = &ensureGoroutinesStopWG summary.Goroutinewg = &ensureGoroutinesStopWG + confwatcher.Goroutinewg = &ensureGoroutinesStopWG res := cmain() ensureGoroutinesStopWG.Wait() @@ -695,7 +696,7 @@ func cmain() int { } } - watcher, err := termshark.NewConfigWatcher() + watcher, err := confwatcher.New() if err != nil { fmt.Fprintf(os.Stderr, "Problem constructing config file watcher: %v", err) return 1 diff --git a/confwatcher.go b/pkg/confwatcher/confwatcher.go similarity index 86% rename from confwatcher.go rename to pkg/confwatcher/confwatcher.go index 5f07420..3400bd2 100644 --- a/confwatcher.go +++ b/pkg/confwatcher/confwatcher.go @@ -2,20 +2,19 @@ // code is governed by the MIT license that can be found in the LICENSE // file. -package termshark +package confwatcher import ( "os" "sync" + "github.com/gcla/termshark/v2" log "github.com/sirupsen/logrus" fsnotify "gopkg.in/fsnotify/fsnotify.v1" ) //====================================================================== -var Goroutinewg *sync.WaitGroup - type ConfigWatcher struct { watcher *fsnotify.Watcher change chan struct{} @@ -23,7 +22,7 @@ type ConfigWatcher struct { closeWait sync.WaitGroup } -func NewConfigWatcher() (*ConfigWatcher, error) { +func New() (*ConfigWatcher, error) { watcher, err := fsnotify.NewWatcher() if err != nil { panic(err) @@ -39,7 +38,7 @@ func NewConfigWatcher() (*ConfigWatcher, error) { res.closeWait.Add(1) - TrackedGo(func() { + termshark.TrackedGo(func() { defer func() { res.watcher.Close() close(change) @@ -60,7 +59,7 @@ func NewConfigWatcher() (*ConfigWatcher, error) { } }, Goroutinewg) - if err := watcher.Add(ConfFile("termshark.toml")); err != nil && !os.IsNotExist(err) { + if err := watcher.Add(termshark.ConfFile("termshark.toml")); err != nil && !os.IsNotExist(err) { return nil, err } @@ -73,7 +72,7 @@ func (c *ConfigWatcher) Close() { // drain the change channel to ensure the goroutine above can process the close. This // is safe because I know, at this point, there are no other readers because termshark // has exited its select loop. - TrackedGo(func() { + termshark.TrackedGo(func() { // This might block because the goroutine above might not be blocked sending // to c.change. But then that means the goroutine's for loop above will terminate, // c.change will be closed, and then this goroutine will end. If the above @@ -90,6 +89,10 @@ func (c *ConfigWatcher) ConfigChanged() <-chan struct{} { return c.change } +//====================================================================== + +var Goroutinewg *sync.WaitGroup + //====================================================================== // Local Variables: // mode: Go