about summary refs log tree commit diff stats
path: root/cmd/dev
diff options
context:
space:
mode:
authorAlan Pearce2024-05-22 18:03:22 +0200
committerAlan Pearce2024-05-22 18:03:22 +0200
commitea098ab11740fe4783694ff0122738b30e0c342f (patch)
treebcd50fdb6fc7ce9730c758f709aa6bf29a763ea9 /cmd/dev
parentaef028263229d8acda28b8e657413f7e9c187833 (diff)
downloadwebsite-ea098ab11740fe4783694ff0122738b30e0c342f.tar.lz
website-ea098ab11740fe4783694ff0122738b30e0c342f.tar.zst
website-ea098ab11740fe4783694ff0122738b30e0c342f.zip
refactor: lint with golangci-lint
Diffstat (limited to 'cmd/dev')
-rw-r--r--cmd/dev/main.go50
1 files changed, 36 insertions, 14 deletions
diff --git a/cmd/dev/main.go b/cmd/dev/main.go
index 459cfaf..e1525a2 100644
--- a/cmd/dev/main.go
+++ b/cmd/dev/main.go
@@ -48,7 +48,8 @@ func RunCommandPiped(
 		if err != nil {
 			log.Error("signal error:", "error", err)
 		}
-		return err
+
+		return errors.Wrap(err, "error signalling child process")
 	}
 	stdout, err := cmd.StdoutPipe()
 	if err != nil {
@@ -59,8 +60,18 @@ func RunCommandPiped(
 		return
 	}
 
-	go io.Copy(os.Stdout, stdout)
-	go io.Copy(os.Stderr, stderr)
+	go func() {
+		_, err := io.Copy(os.Stdout, stdout)
+		if err != nil {
+			log.Error("error copying stdout", "error", err)
+		}
+	}()
+	go func() {
+		_, err := io.Copy(os.Stderr, stderr)
+		if err != nil {
+			log.Error("error copying stderr", "error", err)
+		}
+	}()
 
 	return
 }
@@ -72,24 +83,27 @@ type FileWatcher struct {
 func NewFileWatcher(pollTime time.Duration) (*FileWatcher, error) {
 	batcher, err := watcher.New(pollTime/5, pollTime, true)
 	if err != nil {
-		return nil, err
+		return nil, errors.Wrap(err, "could not create file watcher")
 	}
+
 	return &FileWatcher{batcher}, nil
 }
 
 func (watcher FileWatcher) WatchAllFiles(from string) error {
 	log.Debug("watching files under", "from", from)
-	err := filepath.Walk(from, func(path string, info fs.FileInfo, err error) error {
+	err := filepath.Walk(from, func(path string, _ fs.FileInfo, err error) error {
 		if err != nil {
 			return err
 		}
 		// log.Debug(fmt.Sprintf("adding file %s to watcher", path))
 		if err = watcher.Add(path); err != nil {
-			return err
+			return errors.Wrapf(err, "could not add path %s to watcher", path)
 		}
+
 		return nil
 	})
-	return err
+
+	return errors.Wrapf(err, "could not walk directory tree %s", from)
 }
 
 func build(ctx context.Context, config DevConfig) error {
@@ -113,6 +127,7 @@ func build(ctx context.Context, config DevConfig) error {
 	if err != nil {
 		return errors.WithMessage(err, "error running build command")
 	}
+
 	return nil
 }
 
@@ -153,7 +168,7 @@ func server(ctx context.Context, devConfig DevConfig) error {
 			log.Debug("server context done")
 			err := cmd.Process.Signal(os.Interrupt)
 			if err != nil {
-				return err
+				return errors.Wrap(err, "could not process signal")
 			}
 			<-done
 		case err := <-cmdErr:
@@ -179,7 +194,6 @@ func main() {
 	log.Debug("running with in /tmp", "dir", devConfig.TempDir)
 
 	ctx, cancel := context.WithCancel(context.Background())
-	defer cancel()
 
 	log.Debug("setting interrupt handler")
 	c := make(chan os.Signal, 1)
@@ -195,9 +209,9 @@ func main() {
 
 	serverChan := make(chan bool, 1)
 	eventsource := eventsource.New(nil, nil)
-	defer eventsource.Close()
 	srv := http.Server{
-		Addr: devConfig.BaseURL.Host,
+		Addr:              devConfig.BaseURL.Host,
+		ReadHeaderTimeout: 1 * time.Minute,
 	}
 	devCtx, devCancel := context.WithTimeout(ctx, 1*time.Second)
 
@@ -242,15 +256,15 @@ func main() {
 
 	fw, err := NewFileWatcher(500 * time.Millisecond)
 	if err != nil {
-		log.Fatal("error creating file watcher", "error", err)
+		log.Panic("error creating file watcher", "error", err)
 	}
 	err = fw.WatchAllFiles("content")
 	if err != nil {
-		log.Fatal("could not watch files in content directory", "error", err)
+		log.Panic("could not watch files in content directory", "error", err)
 	}
 	err = fw.WatchAllFiles("templates")
 	if err != nil {
-		log.Fatal("could not watch files in templates directory", "error", err)
+		log.Panic("could not watch files in templates directory", "error", err)
 	}
 
 	var exitCode int
@@ -283,6 +297,7 @@ loop:
 				log.Debug("shutdown error", "error", err)
 			}
 			exitCode = 1
+
 			break loop
 		case event := <-fw.Events:
 			log.Debug("event received:", "event", event)
@@ -291,6 +306,7 @@ loop:
 			log.Debug("waiting for server shutdown")
 			<-serverErr
 			log.Debug("server shutdown completed")
+
 			continue
 		case err = <-serverErr:
 			if err != nil && err != context.Canceled {
@@ -301,18 +317,24 @@ loop:
 				} else {
 					log.Debug("server other error")
 				}
+
 				break
 			}
 			log.Debug("no error or server context cancelled")
+
 			continue
 		}
 
 		log.Debug("waiting on server")
 		exitCode = 0
+
 		break
 	}
 
 	log.Debug("waiting for wg before shutting down")
+	eventsource.Close()
+	cancel()
 	wg.Wait()
+
 	os.Exit(exitCode)
 }