From 9b2e8709fb77f680e91eca422ddce0e46c61dbe4 Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Tue, 23 Jul 2024 22:55:03 +0200 Subject: [PATCH] added fsnotify example, to check bahavior on windows. --- cmd/fsnotifytest/monitor.go | 58 +++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ pkg/agent/session.go | 10 +++++++ 4 files changed, 71 insertions(+) create mode 100644 cmd/fsnotifytest/monitor.go diff --git a/cmd/fsnotifytest/monitor.go b/cmd/fsnotifytest/monitor.go new file mode 100644 index 0000000..81248e1 --- /dev/null +++ b/cmd/fsnotifytest/monitor.go @@ -0,0 +1,58 @@ +package main + +import ( + "fmt" + "log" + + "github.com/fsnotify/fsnotify" +) + +// create .hold +// CREATE + CHMOD +// remove +// REMOVE +// touch existing: +// CHMOD +// echo > .hold +// +// file name: ./.hold on linux + +func main() { + // Create a new watcher + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + + // Start listening for events + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + return + } + fmt.Printf("Event: %s File: %s\n", event.Op, event.Name) + + if event.Op&fsnotify.Write == fsnotify.Write { + fmt.Println("Modified file:", event.Name) + } + case err, ok := <-watcher.Errors: + if !ok { + return + } + fmt.Println("Error:", err) + } + } + }() + + // Add a directory to watch + err = watcher.Add(".") + if err != nil { + log.Fatal(err) + } + + // Block main goroutine forever + <-make(chan struct{}) +} diff --git a/go.mod b/go.mod index 33d0529..d114c5b 100755 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/ActiveState/termtest/conpty v0.5.0 github.com/creack/pty v1.1.21 + github.com/fsnotify/fsnotify v1.7.0 github.com/gliderlabs/ssh v0.3.7 github.com/gorilla/websocket v1.5.3 github.com/hashicorp/yamux v0.1.1 diff --git a/go.sum b/go.sum index 1dcbe17..c99068c 100755 --- a/go.sum +++ b/go.sum @@ -9,6 +9,8 @@ github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= +github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/gliderlabs/ssh v0.3.7 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE= github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= diff --git a/pkg/agent/session.go b/pkg/agent/session.go index e9819b8..3dfa823 100644 --- a/pkg/agent/session.go +++ b/pkg/agent/session.go @@ -13,6 +13,15 @@ import ( _ "embed" ) +// TDDO fix concurrency +// All methods put a message on a channel +// +// Using a channel of functions will work. +// When default is used, channel will block always and thereby +// effectively serializing everything. +// +// make(chan func()) + // global configuration type AgentState struct { @@ -168,6 +177,7 @@ func (state *AgentState) expiryTime(filename string) time.Time { // 4. If the last user logs out, the aagent will exit immediately if no .hold file is // present. Otherwise it will exit after the epxiry time. This allows users to // reconnect later. + func check() { now := time.Now()