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{
Exts: []string{"go"},
Paths: []string{"/"},
Paths: []string{"."},
},
})
} else {

View File

@ -7,6 +7,7 @@ package haunt
import (
"errors"
"fmt"
"log"
"os"
"sync"
"time"
@ -115,7 +116,10 @@ func (w *filePoller) Close() error {
w.closed = true
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)
}
w.mu.Unlock()
@ -196,7 +200,12 @@ func (w *filePoller) Walk(path string, init bool) string {
if check == nil && init {
_, err := os.Stat(path)
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
@ -247,7 +256,10 @@ func (w *filePoller) watch(f *os.File, lastFi os.FileInfo, chClose chan struct{}
lastFi = nil
}
// 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
case lastFi == 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
p.Tools.Setup()
p.Tools.Setup(p)
// global commands before
p.cmd(p.stop, "before", true)
// indexing files and dirs
@ -303,7 +303,10 @@ L:
switch event.Op {
case fsnotify.Chmod:
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) != "" {
// stop and restart
close(p.stop)
@ -318,7 +321,10 @@ L:
continue
}
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 {
// stop and restart
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/6720
if build != nil {
build.Process.Signal(os.Interrupt)
build.Process.Wait()
err := build.Process.Signal(os.Interrupt)
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 {
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}
p.stamp("errororororororororor", out, msg, r.Out)
p.stamp("error", out, msg, r.Out)
} 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"))
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.Stderr = &stderr
// Start command
ex.Start()
err := ex.Start()
if err != nil {
log.Println(log.Prefix(), err.Error())
}
go func() { done <- ex.Wait() }()
// Wait a result
select {
case <-stop:
// Stop running command
ex.Process.Kill()
err := ex.Process.Kill()
if err != nil {
log.Println(log.Prefix(), err.Error())
}
case err := <-done:
// Command completed
response.Name = c.Cmd

View File

@ -57,7 +57,9 @@ func (s *Server) projects(c echo.Context) (err error) {
} else {
err := json.Unmarshal([]byte(text), &s.Parent)
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
}
}
@ -88,7 +90,10 @@ func (s *Server) render(c echo.Context, path string, mime int) error {
rs.Header().Set(echo.HeaderContentType, "image/png")
}
rs.WriteHeader(http.StatusOK)
rs.Write(data)
_, err = rs.Write(data)
if err != nil {
return err
}
return nil
}
@ -151,8 +156,11 @@ func (s *Server) Start() (err error) {
e.HideBanner = true
e.Debug = false
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)))
e.Start(string(s.Host) + ":" + strconv.Itoa(s.Port))
}()
}
return nil

View File

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

View File

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