added fsnotify example, to check bahavior on windows.

This commit is contained in:
Erik Brakkee 2024-07-23 22:55:03 +02:00
parent 537579a567
commit 1ed49c638e
4 changed files with 71 additions and 0 deletions

View File

@ -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{})
}

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.21
require ( require (
github.com/ActiveState/termtest/conpty v0.5.0 github.com/ActiveState/termtest/conpty v0.5.0
github.com/creack/pty v1.1.21 github.com/creack/pty v1.1.21
github.com/fsnotify/fsnotify v1.7.0
github.com/gliderlabs/ssh v0.3.7 github.com/gliderlabs/ssh v0.3.7
github.com/gorilla/websocket v1.5.3 github.com/gorilla/websocket v1.5.3
github.com/hashicorp/yamux v0.1.1 github.com/hashicorp/yamux v0.1.1

2
go.sum
View File

@ -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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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 h1:iV3Bqi942d9huXnzEF2Mt+CY9gLu8DNM4Obd+8bODRE=
github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8= github.com/gliderlabs/ssh v0.3.7/go.mod h1:zpHEXBstFnQYtGnB8k8kQLol82umzn/2/snG7alWVD8=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=

View File

@ -13,6 +13,15 @@ import (
_ "embed" _ "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 // global configuration
type AgentState struct { 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 // 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 // present. Otherwise it will exit after the epxiry time. This allows users to
// reconnect later. // reconnect later.
func check() { func check() {
now := time.Now() now := time.Now()