mirror of
https://github.com/priyanshujain/sanderling.git
synced 2026-10-03 11:37:09 +00:00
* refactor: rename inspect to replay across the codebase Renames inspect-ui/ to replay-ui/, internal/inspect/ to internal/replay/, the CLI subcommand from `sanderling inspect` to `sanderling replay`, and updates all references in docs, Makefile, README, and Go comments. * feat(replay-ui): show spec filename with full path on hover RunList and RunDetail now render the basename of spec_path (e.g. login.spec.ts) with the full path available as a title tooltip.
120 lines
2.9 KiB
Go
120 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os/exec"
|
|
"runtime"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/priyanshujain/sanderling/internal/replay"
|
|
)
|
|
|
|
type replayOptions struct {
|
|
port int
|
|
noOpen bool
|
|
dev bool
|
|
directory string
|
|
}
|
|
|
|
func parseReplayArgs(args []string, stderr io.Writer) (replayOptions, error) {
|
|
flagSet := flag.NewFlagSet("replay", flag.ContinueOnError)
|
|
flagSet.SetOutput(stderr)
|
|
var options replayOptions
|
|
flagSet.IntVar(&options.port, "port", 0, "TCP port to listen on (0 = ephemeral)")
|
|
flagSet.BoolVar(&options.noOpen, "no-open", false, "do not open the default browser on startup")
|
|
flagSet.BoolVar(&options.dev, "dev", false, "reverse-proxy non-API requests to "+replay.DevTarget)
|
|
if err := flagSet.Parse(args); err != nil {
|
|
return replayOptions{}, err
|
|
}
|
|
rest := flagSet.Args()
|
|
if len(rest) > 1 {
|
|
return replayOptions{}, errors.New("replay takes at most one positional argument (run or runs directory)")
|
|
}
|
|
if len(rest) == 1 {
|
|
options.directory = rest[0]
|
|
}
|
|
return options, nil
|
|
}
|
|
|
|
func runReplay(options replayOptions, stdout io.Writer) error {
|
|
runsDirectory, deepLinkID, err := replay.ResolveRunsDirectory(options.directory)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
devTarget := ""
|
|
if options.dev {
|
|
devTarget = replay.DevTarget
|
|
}
|
|
server, err := replay.NewServer(replay.ServerOptions{
|
|
RunsDirectory: runsDirectory,
|
|
DevTarget: devTarget,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:"+strconv.Itoa(options.port))
|
|
if err != nil {
|
|
return fmt.Errorf("listen: %w", err)
|
|
}
|
|
address := listener.Addr().(*net.TCPAddr)
|
|
browseURL := buildBrowseURL(address, deepLinkID)
|
|
fmt.Fprintf(stdout, "sanderling replay listening on %s (runs=%s)\n", browseURL, runsDirectory)
|
|
|
|
context, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
watcherDone := make(chan struct{})
|
|
go func() {
|
|
_ = server.Watcher().Run(context)
|
|
close(watcherDone)
|
|
}()
|
|
|
|
httpServer := &http.Server{Handler: server.Handler(), ReadHeaderTimeout: 10 * time.Second}
|
|
serverDone := make(chan error, 1)
|
|
go func() { serverDone <- httpServer.Serve(listener) }()
|
|
|
|
if !options.noOpen {
|
|
if err := openBrowser(browseURL); err != nil {
|
|
fmt.Fprintf(stdout, "warning: could not open browser: %v\n", err)
|
|
}
|
|
}
|
|
|
|
err = <-serverDone
|
|
cancel()
|
|
<-watcherDone
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func buildBrowseURL(address *net.TCPAddr, deepLinkID string) string {
|
|
target := url.URL{Scheme: "http", Host: address.String(), Path: "/"}
|
|
if deepLinkID != "" {
|
|
target.Path = "/runs/" + deepLinkID
|
|
}
|
|
return target.String()
|
|
}
|
|
|
|
func openBrowser(target string) error {
|
|
var command *exec.Cmd
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
command = exec.Command("open", target)
|
|
case "windows":
|
|
command = exec.Command("cmd", "/c", "start", target)
|
|
default:
|
|
command = exec.Command("xdg-open", target)
|
|
}
|
|
return command.Start()
|
|
}
|