Add --run-and-exit flag to qrunweb and screenshot helper script

This commit is contained in:
Ian Gulliver
2026-02-18 23:27:27 -07:00
parent 728fff185a
commit 79509607ca
2 changed files with 35 additions and 2 deletions

View File

@@ -1,11 +1,15 @@
package main package main
import ( import (
"context"
"embed" "embed"
"fmt" "fmt"
"io/fs" "io/fs"
"net"
"net/http" "net/http"
"os" "os"
"os/exec"
"strings"
) )
//go:embed static //go:embed static
@@ -13,8 +17,14 @@ var staticFS embed.FS
func main() { func main() {
addr := ":8080" addr := ":8080"
if len(os.Args) > 1 { var runAndExit []string
addr = os.Args[1]
for _, arg := range os.Args[1:] {
if v, ok := strings.CutPrefix(arg, "--run-and-exit="); ok {
runAndExit = strings.Fields(v)
} else {
addr = arg
}
} }
sub, err := fs.Sub(staticFS, "static") sub, err := fs.Sub(staticFS, "static")
@@ -25,6 +35,27 @@ func main() {
http.Handle("/", http.FileServer(http.FS(sub))) http.Handle("/", http.FileServer(http.FS(sub)))
if len(runAndExit) > 0 {
ln, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
srv := &http.Server{}
go srv.Serve(ln)
cmd := exec.Command(runAndExit[0], runAndExit[1:]...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmdErr := cmd.Run()
srv.Shutdown(context.Background())
if cmdErr != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", cmdErr)
os.Exit(1)
}
return
}
fmt.Printf("Listening on %s\n", addr) fmt.Printf("Listening on %s\n", addr)
if err := http.ListenAndServe(addr, nil); err != nil { if err := http.ListenAndServe(addr, nil); err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err) fmt.Fprintf(os.Stderr, "Error: %v\n", err)

2
screenshot.sh Executable file
View File

@@ -0,0 +1,2 @@
#!/bin/bash
exec go run ./cmd/qrunweb/ "--run-and-exit=shot-scraper http://localhost:8080/ -o /tmp/timeline.png --width 1000 --height ${1:-1200}"