71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"slices"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/fsnotify/fsnotify"
|
||
|
)
|
||
|
|
||
|
var segmentList = []string{}
|
||
|
|
||
|
func remove(slice []string, s int) []string {
|
||
|
return append(slice[:s], slice[s+1:]...)
|
||
|
}
|
||
|
|
||
|
func printSegmentList() {
|
||
|
log.Printf("Segment list: %s\n", strings.Join(segmentList, ", "))
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
log.Println("Starting segment 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
|
||
|
}
|
||
|
|
||
|
if strings.HasSuffix(event.Name, ".ts") {
|
||
|
if event.Op == fsnotify.Create {
|
||
|
segmentList = append(segmentList, event.Name)
|
||
|
log.Println("Created file", event.Name)
|
||
|
printSegmentList()
|
||
|
} else if event.Op == fsnotify.Remove {
|
||
|
index := slices.Index(segmentList, event.Name)
|
||
|
if index != -1 {
|
||
|
segmentList = remove(segmentList, index)
|
||
|
}
|
||
|
|
||
|
log.Println("Removed file", event.Name)
|
||
|
printSegmentList()
|
||
|
}
|
||
|
}
|
||
|
case err, ok := <-watcher.Errors:
|
||
|
if !ok {
|
||
|
return
|
||
|
}
|
||
|
log.Println("error:", err)
|
||
|
}
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
// Add a path.
|
||
|
err = watcher.Add("hls")
|
||
|
if err != nil {
|
||
|
log.Fatal(err)
|
||
|
}
|
||
|
|
||
|
// Block main goroutine forever.
|
||
|
<-make(chan struct{})
|
||
|
}
|