Skip to content

Commit

Permalink
Some more involved refactoring to move confwatcher to pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
gcla committed Jul 9, 2022
1 parent a09eb2a commit 73953a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
9 changes: 5 additions & 4 deletions cmd/termshark/termshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -56,14 +57,14 @@ 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
convs.Goroutinewg = &ensureGoroutinesStopWG
ui.Goroutinewg = &ensureGoroutinesStopWG
wormhole.Goroutinewg = &ensureGoroutinesStopWG
summary.Goroutinewg = &ensureGoroutinesStopWG
confwatcher.Goroutinewg = &ensureGoroutinesStopWG

res := cmain()
ensureGoroutinesStopWG.Wait()
Expand Down Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions confwatcher.go → pkg/confwatcher/confwatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,27 @@
// 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{}
closech chan struct{}
closeWait sync.WaitGroup
}

func NewConfigWatcher() (*ConfigWatcher, error) {
func New() (*ConfigWatcher, error) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
Expand All @@ -39,7 +38,7 @@ func NewConfigWatcher() (*ConfigWatcher, error) {

res.closeWait.Add(1)

TrackedGo(func() {
termshark.TrackedGo(func() {
defer func() {
res.watcher.Close()
close(change)
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -90,6 +89,10 @@ func (c *ConfigWatcher) ConfigChanged() <-chan struct{} {
return c.change
}

//======================================================================

var Goroutinewg *sync.WaitGroup

//======================================================================
// Local Variables:
// mode: Go
Expand Down

0 comments on commit 73953a8

Please sign in to comment.