improve: better prints and error handling
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful

This commit is contained in:
abs3nt 2023-03-05 02:10:06 -08:00
parent 8b0a7cb20c
commit 1767d94341
6 changed files with 78 additions and 24 deletions

View File

@ -99,7 +99,7 @@ func (r *Haunt) SetDefaults() {
}, },
Watcher: Watch{ Watcher: Watch{
Exts: []string{"go"}, Exts: []string{"go"},
Paths: []string{"/"}, Paths: []string{"."},
}, },
}) })
} else { } else {

View File

@ -7,6 +7,7 @@ package haunt
import ( import (
"errors" "errors"
"fmt" "fmt"
"log"
"os" "os"
"sync" "sync"
"time" "time"
@ -115,7 +116,10 @@ func (w *filePoller) Close() error {
w.closed = true w.closed = true
for name := range w.watches { for name := range w.watches {
w.remove(name) err := w.remove(name)
if err != nil {
log.Println(log.Prefix(), err.Error())
}
delete(w.watches, name) delete(w.watches, name)
} }
w.mu.Unlock() w.mu.Unlock()
@ -196,7 +200,12 @@ func (w *filePoller) Walk(path string, init bool) string {
if check == nil && init { if check == nil && init {
_, err := os.Stat(path) _, err := os.Stat(path)
if err == nil { if err == nil {
go w.sendEvent(fsnotify.Event{Op: fsnotify.Create, Name: path}, w.watches[path]) go func() {
err := w.sendEvent(fsnotify.Event{Op: fsnotify.Create, Name: path}, w.watches[path])
if err != nil {
log.Println(log.Prefix(), err.Error())
}
}()
} }
} }
return path return path
@ -247,7 +256,10 @@ func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}
lastFi = nil lastFi = nil
} }
// at this point, send the error // at this point, send the error
w.sendErr(err, chClose) err := w.sendErr(err, chClose)
if err != nil {
log.Println(log.Prefix(), err.Error())
}
return return
case lastFi == nil: case lastFi == nil:
if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Create, Name: f.Name()}, chClose); err != nil { if err := w.sendEvent(fsnotify.Event{Op: fsnotify.Create, Name: f.Name()}, chClose); err != nil {

View File

@ -115,7 +115,7 @@ func (p *Project) Before() {
} }
// setup go tools // setup go tools
p.Tools.Setup() p.Tools.Setup(p)
// global commands before // global commands before
p.cmd(p.stop, "before", true) p.cmd(p.stop, "before", true)
// indexing files and dirs // indexing files and dirs
@ -303,7 +303,10 @@ L:
switch event.Op { switch event.Op {
case fsnotify.Chmod: case fsnotify.Chmod:
case fsnotify.Remove: case fsnotify.Remove:
p.watcher.Remove(event.Name) err := p.watcher.Remove(event.Name)
if err != nil {
log.Println(p.parent.Prefix("Failed to remove watcher: " + err.Error()))
}
if p.Validate(event.Name, false) && ext(event.Name) != "" { if p.Validate(event.Name, false) && ext(event.Name) != "" {
// stop and restart // stop and restart
close(p.stop) close(p.stop)
@ -318,7 +321,10 @@ L:
continue continue
} }
if fi.IsDir() { if fi.IsDir() {
filepath.Walk(event.Name, p.walk) err := filepath.Walk(event.Name, p.walk)
if err != nil {
log.Println(p.parent.Prefix("Failed to walk directory: " + err.Error()))
}
} else { } else {
// stop and restart // stop and restart
close(p.stop) close(p.stop)
@ -572,8 +578,14 @@ func (p *Project) run(path string, stream chan Response, stop <-chan bool) (err
// https://github.com/golang/go/issues/5615 // https://github.com/golang/go/issues/5615
// https://github.com/golang/go/issues/6720 // https://github.com/golang/go/issues/6720
if build != nil { if build != nil {
build.Process.Signal(os.Interrupt) err := build.Process.Signal(os.Interrupt)
build.Process.Wait() if err != nil {
log.Println(p.parent.Prefix("Failed to send interrupt: " + err.Error()))
}
_, err = build.Process.Wait()
if err != nil {
log.Println(p.parent.Prefix("Error: " + err.Error()))
}
} }
}() }()
@ -675,7 +687,7 @@ func (r *Response) print(start time.Time, p *Project) {
if r.Err != nil { if r.Err != nil {
msg = fmt.Sprintln(p.pname(p.Name, 2), ":", Red.Bold(r.Name), "\n", r.Err.Error()) msg = fmt.Sprintln(p.pname(p.Name, 2), ":", Red.Bold(r.Name), "\n", r.Err.Error())
out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: r.Name, Stream: r.Out} out = BufferOut{Time: time.Now(), Text: r.Err.Error(), Type: r.Name, Stream: r.Out}
p.stamp("errororororororororor", out, msg, r.Out) p.stamp("error", out, msg, r.Out)
} else { } else {
msg = fmt.Sprintln(p.pname(p.Name, 5), ":", Green.Bold(r.Name), "completed in", Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s")) msg = fmt.Sprintln(p.pname(p.Name, 5), ":", Green.Bold(r.Name), "completed in", Magenta.Regular(big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3), " s"))
out = BufferOut{Time: time.Now(), Text: r.Name + " in " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"} out = BufferOut{Time: time.Now(), Text: r.Name + " in " + big.NewFloat(float64(time.Since(start).Seconds())).Text('f', 3) + " s"}
@ -702,13 +714,19 @@ func (c *Command) exec(base string, stop <-chan bool) (response Response) {
ex.Stdout = &stdout ex.Stdout = &stdout
ex.Stderr = &stderr ex.Stderr = &stderr
// Start command // Start command
ex.Start() err := ex.Start()
if err != nil {
log.Println(log.Prefix(), err.Error())
}
go func() { done <- ex.Wait() }() go func() { done <- ex.Wait() }()
// Wait a result // Wait a result
select { select {
case <-stop: case <-stop:
// Stop running command // Stop running command
ex.Process.Kill() err := ex.Process.Kill()
if err != nil {
log.Println(log.Prefix(), err.Error())
}
case err := <-done: case err := <-done:
// Command completed // Command completed
response.Name = c.Cmd response.Name = c.Cmd

View File

@ -57,7 +57,9 @@ func (s *Server) projects(c echo.Context) (err error) {
} else { } else {
err := json.Unmarshal([]byte(text), &s.Parent) err := json.Unmarshal([]byte(text), &s.Parent)
if err == nil { if err == nil {
s.Parent.Settings.Write(s.Parent) if err := s.Parent.Settings.Write(s.Parent); err != nil {
log.Println(s.Parent.Prefix("Failed to write: " + err.Error()))
}
break break
} }
} }
@ -88,7 +90,10 @@ func (s *Server) render(c echo.Context, path string, mime int) error {
rs.Header().Set(echo.HeaderContentType, "image/png") rs.Header().Set(echo.HeaderContentType, "image/png")
} }
rs.WriteHeader(http.StatusOK) rs.WriteHeader(http.StatusOK)
rs.Write(data) _, err = rs.Write(data)
if err != nil {
return err
}
return nil return nil
} }
@ -151,8 +156,11 @@ func (s *Server) Start() (err error) {
e.HideBanner = true e.HideBanner = true
e.Debug = false e.Debug = false
go func() { go func() {
err := e.Start(string(s.Host) + ":" + strconv.Itoa(s.Port))
if err != nil {
log.Println(s.Parent.Prefix("Failed to start on " + s.Host + ":" + strconv.Itoa(s.Port)))
}
log.Println(s.Parent.Prefix("Started on " + string(s.Host) + ":" + strconv.Itoa(s.Port))) log.Println(s.Parent.Prefix("Started on " + string(s.Host) + ":" + strconv.Itoa(s.Port)))
e.Start(string(s.Host) + ":" + strconv.Itoa(s.Port))
}() }()
} }
return nil return nil

View File

@ -1,7 +1,6 @@
package haunt package haunt
import ( import (
"io/ioutil"
"log" "log"
"os" "os"
"path/filepath" "path/filepath"
@ -89,7 +88,7 @@ func (s *Settings) Write(out interface{}) error {
if err != nil { if err != nil {
return err return err
} }
s.Fatal(ioutil.WriteFile(RFile, y, Permission)) s.Fatal(os.WriteFile(RFile, y, Permission))
return nil return nil
} }
@ -99,7 +98,7 @@ func (s Settings) Stream(file string) ([]byte, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
content, err := ioutil.ReadFile(file) content, err := os.ReadFile(file)
s.Fatal(err) s.Fatal(err)
return content, err return content, err
} }

View File

@ -39,7 +39,7 @@ type Tools struct {
} }
// Setup go tools // Setup go tools
func (t *Tools) Setup() { func (t *Tools) Setup(p *Project) {
gocmd := "go" gocmd := "go"
// go clean // go clean
@ -48,6 +48,8 @@ func (t *Tools) Setup() {
t.Clean.isTool = true t.Clean.isTool = true
t.Clean.cmd = replace([]string{gocmd, "clean"}, t.Clean.Method) t.Clean.cmd = replace([]string{gocmd, "clean"}, t.Clean.Method)
t.Clean.Args = split([]string{}, t.Clean.Args) t.Clean.Args = split([]string{}, t.Clean.Args)
msg = fmt.Sprintln(p.pname(p.Name, 1), ":", Magenta.Bold(t.Clean.name), "running. Method:", Blue.Bold(strings.Join(t.Clean.cmd, " "), " ", strings.Join(t.Clean.Args, " ")))
log.Print(msg)
} }
// go generate // go generate
if t.Generate.Status { if t.Generate.Status {
@ -56,6 +58,8 @@ func (t *Tools) Setup() {
t.Generate.name = "Generate" t.Generate.name = "Generate"
t.Generate.cmd = replace([]string{gocmd, "generate"}, t.Generate.Method) t.Generate.cmd = replace([]string{gocmd, "generate"}, t.Generate.Method)
t.Generate.Args = split([]string{}, t.Generate.Args) t.Generate.Args = split([]string{}, t.Generate.Args)
msg = fmt.Sprintln(p.pname(p.Name, 1), ":", Magenta.Bold(t.Generate.name), "running. Method:", Blue.Bold(strings.Join(t.Generate.cmd, " "), " ", strings.Join(t.Generate.Args, " ")))
log.Print(msg)
} }
// go fmt // go fmt
if t.Fmt.Status { if t.Fmt.Status {
@ -66,6 +70,8 @@ func (t *Tools) Setup() {
t.Fmt.isTool = true t.Fmt.isTool = true
t.Fmt.cmd = replace([]string{"gofmt"}, t.Fmt.Method) t.Fmt.cmd = replace([]string{"gofmt"}, t.Fmt.Method)
t.Fmt.Args = split([]string{}, t.Fmt.Args) t.Fmt.Args = split([]string{}, t.Fmt.Args)
msg = fmt.Sprintln(p.pname(p.Name, 1), ":", Magenta.Bold(t.Fmt.name), "running. Method:", Blue.Bold(strings.Join(t.Fmt.cmd, " "), " ", strings.Join(t.Fmt.Args, " ")))
log.Print(msg)
} }
// go vet // go vet
if t.Vet.Status { if t.Vet.Status {
@ -74,6 +80,8 @@ func (t *Tools) Setup() {
t.Vet.isTool = true t.Vet.isTool = true
t.Vet.cmd = replace([]string{gocmd, "vet"}, t.Vet.Method) t.Vet.cmd = replace([]string{gocmd, "vet"}, t.Vet.Method)
t.Vet.Args = split([]string{}, t.Vet.Args) t.Vet.Args = split([]string{}, t.Vet.Args)
msg = fmt.Sprintln(p.pname(p.Name, 1), ":", Magenta.Bold(t.Vet.name), "running. Method:", Blue.Bold(strings.Join(t.Vet.cmd, " "), " ", strings.Join(t.Vet.Args, " ")))
log.Print(msg)
} }
// go test // go test
if t.Test.Status { if t.Test.Status {
@ -82,6 +90,9 @@ func (t *Tools) Setup() {
t.Test.name = "Test" t.Test.name = "Test"
t.Test.cmd = replace([]string{gocmd, "test"}, t.Test.Method) t.Test.cmd = replace([]string{gocmd, "test"}, t.Test.Method)
t.Test.Args = split([]string{}, t.Test.Args) t.Test.Args = split([]string{}, t.Test.Args)
p.parent.Prefix(t.Test.name + ": Running")
msg = fmt.Sprintln(p.pname(p.Name, 1), ":", Magenta.Bold(t.Test.name), "running. Method:", Blue.Bold(strings.Join(t.Test.cmd, " "), " ", strings.Join(t.Test.Args, " ")))
log.Print(msg)
} }
// go install // go install
t.Install.name = "Install" t.Install.name = "Install"
@ -136,6 +147,7 @@ func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
} }
cmd.Stdout = &out cmd.Stdout = &out
cmd.Stderr = &stderr cmd.Stderr = &stderr
// Start command // Start command
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
@ -148,7 +160,9 @@ func (t *Tool) Exec(path string, stop <-chan bool) (response Response) {
select { select {
case <-stop: case <-stop:
// Stop running command // Stop running command
cmd.Process.Kill() if err := cmd.Process.Kill(); err != nil {
response.Err = err
}
case err := <-done: case err := <-done:
// Command completed // Command completed
response.Name = t.name response.Name = t.name
@ -181,12 +195,15 @@ func (t *Tool) Compile(path string, stop <-chan bool) (response Response) {
// Start command // Start command
err := cmd.Start() err := cmd.Start()
if err != nil { if err != nil {
log.Println(log.Prefix(), err.Error())
if cmd.Process != nil {
err := cmd.Process.Kill() err := cmd.Process.Kill()
if err != nil { if err != nil {
fmt.Println(err.Error()) log.Println(log.Prefix(), err.Error())
return
} }
} }
os.Exit(0)
}
go func() { go func() {
done <- cmd.Wait() done <- cmd.Wait()
}() }()