|
|
|
|
@ -25,7 +25,24 @@ type ErrInfo struct { |
|
|
|
|
Message string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func ParseErrInfo(line string, reg *regexp.Regexp) (ErrInfo, bool) { |
|
|
|
|
type Game struct { |
|
|
|
|
settings config.Config
|
|
|
|
|
|
|
|
|
|
BuildRunning bool |
|
|
|
|
HP int |
|
|
|
|
Errors int |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func MakeGame(settings config.Config) *Game { |
|
|
|
|
game := new(Game) |
|
|
|
|
|
|
|
|
|
game.settings = settings |
|
|
|
|
game.HP = settings.StartingHP |
|
|
|
|
|
|
|
|
|
return game |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (game *Game) ParseErrInfo(line string, reg *regexp.Regexp) (ErrInfo, bool) { |
|
|
|
|
var info ErrInfo |
|
|
|
|
|
|
|
|
|
matches := reg.FindStringSubmatch(line) |
|
|
|
|
@ -50,28 +67,33 @@ func ParseErrInfo(line string, reg *regexp.Regexp) (ErrInfo, bool) { |
|
|
|
|
return info, true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func LaunchLogger(in io.Reader, out io.Writer, err error) { |
|
|
|
|
func (game *Game) TakeHit(errinfo ErrInfo) { |
|
|
|
|
fmt.Println("!!!!!!!!!!!!!!!!!", errinfo) |
|
|
|
|
game.Errors++ |
|
|
|
|
game.HP-- |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (game *Game) LaunchLogger(in io.Reader, out io.Writer, err error) { |
|
|
|
|
if err != nil { log.Fatal(err) } |
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
scan := bufio.NewScanner(in) |
|
|
|
|
|
|
|
|
|
for scan.Scan() { |
|
|
|
|
for _, reg := range config.Settings.TriggerRegex { |
|
|
|
|
for _, reg := range game.settings.TriggerRegex { |
|
|
|
|
line := scan.Text() |
|
|
|
|
|
|
|
|
|
errinfo, ok := ParseErrInfo(line, reg) |
|
|
|
|
errinfo, ok := game.ParseErrInfo(line, reg) |
|
|
|
|
|
|
|
|
|
if ok { |
|
|
|
|
fmt.Println("!!!!!!!!!!!!!!!!!", errinfo) |
|
|
|
|
config.Settings.Errors++ |
|
|
|
|
config.Settings.HP-- |
|
|
|
|
game.TakeHit(errinfo) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func LaunchProcess(proc *config.Process) { |
|
|
|
|
func (game *Game) LaunchProcess(proc *config.Process) { |
|
|
|
|
proc.ExecCmd = exec.Command(proc.Command, proc.Args...) |
|
|
|
|
if errors.Is(proc.ExecCmd.Err, exec.ErrDot) { |
|
|
|
|
proc.ExecCmd.Err = nil |
|
|
|
|
@ -80,10 +102,10 @@ func LaunchProcess(proc *config.Process) { |
|
|
|
|
fmt.Println("STARTING", proc.Command) |
|
|
|
|
|
|
|
|
|
stderr, err := proc.ExecCmd.StderrPipe(); |
|
|
|
|
LaunchLogger(stderr, os.Stdout, err) |
|
|
|
|
game.LaunchLogger(stderr, os.Stdout, err) |
|
|
|
|
|
|
|
|
|
stdout, err := proc.ExecCmd.StdoutPipe(); |
|
|
|
|
LaunchLogger(stdout, os.Stdout, err) |
|
|
|
|
game.LaunchLogger(stdout, os.Stdout, err) |
|
|
|
|
|
|
|
|
|
err = proc.ExecCmd.Start() |
|
|
|
|
if err != nil { |
|
|
|
|
@ -96,10 +118,11 @@ func LaunchProcess(proc *config.Process) { |
|
|
|
|
fmt.Println("PROCESS", proc.Command, "EXITED") |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func MatchesPath(fp string) bool { |
|
|
|
|
|
|
|
|
|
func (game *Game) MatchesPath(fp string) bool { |
|
|
|
|
fp = filepath.ToSlash(fp) |
|
|
|
|
|
|
|
|
|
for _, reg := range config.Settings.IncludeRegex { |
|
|
|
|
for _, reg := range game.settings.IncludeRegex { |
|
|
|
|
matches := reg.MatchString(fp) |
|
|
|
|
|
|
|
|
|
if matches { |
|
|
|
|
@ -110,25 +133,37 @@ func MatchesPath(fp string) bool { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func RunBuild() { |
|
|
|
|
if !config.Settings.BuildRunning { |
|
|
|
|
config.Settings.BuildRunning = true |
|
|
|
|
defer func(){config.Settings.BuildRunning = false}() |
|
|
|
|
func (game *Game) BeginBuild() bool { |
|
|
|
|
if !game.BuildRunning { |
|
|
|
|
game.BuildRunning = true |
|
|
|
|
return true |
|
|
|
|
} else { |
|
|
|
|
return false |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fmt.Println("CONFIG:", config.Settings.ConfigPath) |
|
|
|
|
fmt.Println("COMMANDs:", config.Settings.Processes) |
|
|
|
|
func (game *Game) EndBuild() { |
|
|
|
|
game.BuildRunning = false |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (game *Game) RunBuild() { |
|
|
|
|
if game.BeginBuild() { |
|
|
|
|
defer game.EndBuild()
|
|
|
|
|
|
|
|
|
|
fmt.Println("CONFIG:", game.settings.ConfigPath) |
|
|
|
|
fmt.Println("COMMANDs:", game.settings.Processes) |
|
|
|
|
|
|
|
|
|
for name, proc := range config.Settings.Processes { |
|
|
|
|
for name, proc := range game.settings.Processes { |
|
|
|
|
fmt.Println("PROCESS:", name) |
|
|
|
|
|
|
|
|
|
LaunchProcess(&proc) |
|
|
|
|
game.LaunchProcess(&proc) |
|
|
|
|
fmt.Println("============== PROCESS EXIT") |
|
|
|
|
fmt.Printf("==== HP: %d Errors: %d =====\n", |
|
|
|
|
config.Settings.HP, config.Settings.Errors) |
|
|
|
|
game.HP, game.Errors) |
|
|
|
|
|
|
|
|
|
if config.Settings.HP <= 0 { |
|
|
|
|
if game.HP <= 0 { |
|
|
|
|
fmt.Println("!!!!!! YOU DIED !!!!!!!") |
|
|
|
|
config.Settings.HP = config.Settings.StartingHP |
|
|
|
|
game.HP = game.settings.StartingHP |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fmt.Println("===========================") |
|
|
|
|
@ -140,7 +175,7 @@ func RunBuild() { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func AddWatchDir(watcher *fsnotify.Watcher, name string) error { |
|
|
|
|
func (game *Game) AddWatchDir(watcher *fsnotify.Watcher, name string) error { |
|
|
|
|
return filepath.WalkDir(name, |
|
|
|
|
func(path string, d fs.DirEntry, err error) error { |
|
|
|
|
if err != nil { |
|
|
|
|
@ -161,7 +196,7 @@ func AddWatchDir(watcher *fsnotify.Watcher, name string) error { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func WatchDir() { |
|
|
|
|
func (game *Game) WatchDir() { |
|
|
|
|
watcher, err := fsnotify.NewWatcher() |
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal("Failed to start fsnotify", err) |
|
|
|
|
@ -178,11 +213,11 @@ func WatchDir() { |
|
|
|
|
|
|
|
|
|
if event.Has(fsnotify.Create) { |
|
|
|
|
log.Println("---> CREATE", event.Name) |
|
|
|
|
AddWatchDir(watcher, event.Name) |
|
|
|
|
game.AddWatchDir(watcher, event.Name) |
|
|
|
|
} else if event.Has(fsnotify.Write) { |
|
|
|
|
// check if match then do thing
|
|
|
|
|
if MatchesPath(event.Name) { |
|
|
|
|
go RunBuild() |
|
|
|
|
if game.MatchesPath(event.Name) { |
|
|
|
|
go game.RunBuild() |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
log.Println("event:", event) |
|
|
|
|
@ -196,7 +231,7 @@ func WatchDir() { |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
err = AddWatchDir(watcher, ".") |
|
|
|
|
err = game.AddWatchDir(watcher, ".") |
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
log.Fatal(err, "Failed to watch .") |
|
|
|
|
@ -205,10 +240,10 @@ func WatchDir() { |
|
|
|
|
<-make(chan struct{}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func main() { |
|
|
|
|
config.Load() |
|
|
|
|
settings := config.Load() |
|
|
|
|
|
|
|
|
|
game := MakeGame(settings) |
|
|
|
|
|
|
|
|
|
fmt.Println("WAITING....") |
|
|
|
|
WatchDir() |
|
|
|
|
game.WatchDir() |
|
|
|
|
} |
|
|
|
|
|