2024-05-11 01:09:51 +00:00
package main
// It's not an actual reflex (https://github.com/cespare/reflex). I just wrote some code as for reflex and couldn't manage to adapt it fast before moving to creating own solution
import (
// "errors"
// // "flag"
2024-07-01 10:59:05 +00:00
"io/fs"
2024-07-01 11:05:55 +00:00
"path/filepath"
2024-05-11 01:09:51 +00:00
// "fmt"
// "io/ioutil"
// "log"
// "os"
// "os/exec"
// "path"
// "reflect"
// "regexp"
// "strconv"
// // "strings"
// "syscall"
// "time"
// "bytes"
"log"
"os/exec"
"github.com/fsnotify/fsnotify"
// tea "github.com/charmbracelet/bubbletea"
)
type fileUpdated bool
// func watchFilesystem(tui *tea.Program) {
2024-07-01 10:59:05 +00:00
func watchFilesystem ( ) { //TODO watch subfolders
2024-05-11 01:09:51 +00:00
// Create new watcher.
watcher , err := fsnotify . NewWatcher ( )
if err != nil {
errHandler ( err , "Can't setup filesystem watcher" )
}
defer watcher . Close ( )
// Start listening for events.
go func ( ) {
for {
select {
case event , ok := <- watcher . Events :
if ! ok {
return
}
// log.Println("event:", event)
if event . Has ( fsnotify . Write ) {
// log.Println("modified file:", event.Name)
var fileUpdated1 fileUpdated
TUI . Send ( fileUpdated1 )
}
case err , ok := <- watcher . Errors :
if ! ok {
return
}
log . Println ( "error:" , err )
}
}
} ( )
2024-07-01 10:59:05 +00:00
//get subdirectories
2024-07-01 11:08:01 +00:00
// err = watcher.Add("...") //TODO waiting for official recursive add https://github.com/fsnotify/fsnotify/issues/18
2024-07-01 11:05:55 +00:00
var subdirs [ ] string
filepath . WalkDir ( "." , func ( path string , file fs . DirEntry , err error ) error {
if err != nil {
return err
}
if file . IsDir ( ) {
// fmt.Println(path);
subdirs = append ( subdirs , path )
}
return nil
} )
for _ , dir := range subdirs {
2024-07-01 10:59:05 +00:00
err = watcher . Add ( dir )
2024-07-01 11:05:55 +00:00
if err != nil {
errHandler ( err , "Filesystem Watcher - Can't add current dir:" )
}
2024-07-01 10:59:05 +00:00
}
2024-05-11 01:09:51 +00:00
// Add a current path.
err = watcher . Add ( "." )
if err != nil {
errHandler ( err , "Filesystem Watcher - Can't add current dir:" )
}
// Block main goroutine forever.
<- make ( chan struct { } )
}
func watchRun ( ) string {
2024-07-01 11:08:01 +00:00
//TODO if edited .go file or go.mod/go.sum
2024-05-11 01:09:51 +00:00
return GoVet ( )
}
func GoVet ( ) string {
out1 , _ := exec . Command ( "go" , "mod" , "tidy" ) . CombinedOutput ( )
out2 , _ := exec . Command ( "go" , "vet" ) . CombinedOutput ( )
output := string ( out1 ) + string ( out2 )
if output == "" {
output = "[BUILD OK]"
}
return output
}