diff --git a/.ttarpit.json b/.ttarpit.json index c37b276..5d2c925 100644 --- a/.ttarpit.json +++ b/.ttarpit.json @@ -1,4 +1,5 @@ { + "StartingHP": 10, "Includes": [ "^.*.go$" ], @@ -9,6 +10,7 @@ } }, "Triggers": [ - "(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*" + "(?.*):(?[0-9]+):(?[0-9]+):\\s*(?.*):\\s*(?.*)", + "(?.*):(?[0-9]+):(?[0-9]+):\\s*(?.*)" ] } diff --git a/config/settings.go b/config/settings.go index 8283714..e350282 100644 --- a/config/settings.go +++ b/config/settings.go @@ -15,26 +15,30 @@ type Process struct { ExecCmd *exec.Cmd } -type config struct { +type Config struct { Triggers []string Includes []string Processes map[string]Process + StartingHP int ConfigPath string BuildRunning bool TriggerRegex []*regexp.Regexp IncludeRegex []*regexp.Regexp + + HP int + Errors int } -var Settings config +var Settings Config -func parseFlags(c *config) { +func ParseFlags(c *Config) { flag.StringVar(&c.ConfigPath, "config", ".ttarpit.json", ".ttarpit.json to load") flag.Parse() } func Load() { - parseFlags(&Settings) + ParseFlags(&Settings) data, err := os.ReadFile(Settings.ConfigPath) if err != nil { log.Fatal(err) } @@ -49,4 +53,6 @@ func Load() { for _, reg := range Settings.Includes { Settings.IncludeRegex = append(Settings.IncludeRegex, regexp.MustCompile(reg)) } + + Settings.HP = Settings.StartingHP } diff --git a/main.go b/main.go index b641b8a..136709a 100644 --- a/main.go +++ b/main.go @@ -13,8 +13,43 @@ import ( "lcthw.dev/go/ttarpit/config" "github.com/fsnotify/fsnotify" "bufio" + "regexp" + "strconv" ) +type ErrInfo struct { + File string + Line int + Col int + ErrType string + Message string +} + +func ParseErrInfo(line string, reg *regexp.Regexp) (ErrInfo, bool) { + var info ErrInfo + + matches := reg.FindStringSubmatch(line) + if matches == nil { + return info, false + } + + et_index := reg.SubexpIndex("ErrType") + if et_index != -1 { + info.ErrType = matches[et_index] + } + + // BUG: use reflect + info.File = matches[reg.SubexpIndex("File")] + + info.Line, _ = strconv.Atoi(matches[reg.SubexpIndex("Line")]) + + info.Col, _ = strconv.Atoi(matches[reg.SubexpIndex("Col")]) + + info.Message = matches[reg.SubexpIndex("Message")] + + return info, true +} + func LaunchLogger(in io.Reader, out io.Writer, err error) { if err != nil { log.Fatal(err) } @@ -25,11 +60,11 @@ func LaunchLogger(in io.Reader, out io.Writer, err error) { for _, reg := range config.Settings.TriggerRegex { line := scan.Text() - matches := reg.FindStringSubmatch(line) - if len(matches) > 0 { - file, line, col, err_type, text := matches[1], matches[2], matches[3], matches[4], matches[5] - fmt.Printf("!!!!!!!!!!!!!! file=%s, line=%s, col=%s, type=%s, text=%s\n", - file, line, col, err_type, text) + errinfo, ok := ParseErrInfo(line, reg) + if ok { + fmt.Println("!!!!!!!!!!!!!!!!!", errinfo) + config.Settings.Errors++ + config.Settings.HP-- } } } @@ -57,8 +92,6 @@ func LaunchProcess(proc *config.Process) { fmt.Println("WAITING for", proc.Command) - fmt.Println("SENDING READY on channel") - proc.ExecCmd.Wait() fmt.Println("PROCESS", proc.Command, "EXITED") } @@ -89,7 +122,16 @@ func RunBuild() { fmt.Println("PROCESS:", name) LaunchProcess(&proc) - fmt.Println("=================== PROCESS EXIT") + fmt.Println("============== PROCESS EXIT") + fmt.Printf("==== HP: %d Errors: %d =====\n", + config.Settings.HP, config.Settings.Errors) + + if config.Settings.HP <= 0 { + fmt.Println("!!!!!! YOU DIED !!!!!!!") + config.Settings.HP = config.Settings.StartingHP + } + + fmt.Println("===========================") } time.Sleep(1000 * time.Millisecond)