arcast/internal/command/player/run.go

88 lines
2.0 KiB
Go
Raw Normal View History

2023-12-13 20:07:22 +01:00
package player
import (
"fmt"
"os"
"forge.cadoles.com/arcad/arcast/pkg/browser/lorca"
"forge.cadoles.com/arcad/arcast/pkg/server"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"gitlab.com/wpetit/goweb/logger"
)
func Run() *cli.Command {
defaults := lorca.NewOptions()
return &cli.Command{
Name: "run",
Flags: []cli.Flag{
&cli.StringSliceFlag{
Name: "additional-chrome-arg",
EnvVars: []string{"ARCAST_DESKTOP_ADDITIONAL_CHROME_ARGS"},
Value: cli.NewStringSlice("incognito"),
},
&cli.IntFlag{
Name: "window-height",
Value: defaults.Height,
},
&cli.IntFlag{
Name: "window-width",
Value: defaults.Width,
},
},
Action: func(ctx *cli.Context) error {
windowHeight := ctx.Int("window-height")
windowWidth := ctx.Int("window-width")
chromeArgs := addFlagsPrefix(ctx.StringSlice("additional-chrome-arg")...)
browser := lorca.NewBrowser(
lorca.WithAdditionalChromeArgs(chromeArgs...),
lorca.WithWindowSize(windowWidth, windowHeight),
)
if err := browser.Start(); err != nil {
return errors.Wrap(err, "could not start browser")
}
go func() {
browser.Wait()
logger.Warn(ctx.Context, "browser was closed")
os.Exit(1)
}()
defer func() {
logger.Info(ctx.Context, "stopping browser")
if err := browser.Stop(); err != nil {
logger.Error(ctx.Context, "could not stop browser", logger.CapturedE(errors.WithStack(err)))
}
}()
server := server.New(browser)
if err := server.Start(); err != nil {
return errors.Wrap(err, "could not start server")
}
defer func() {
if err := server.Stop(); err != nil {
logger.Error(ctx.Context, "could not stop server", logger.CapturedE(errors.WithStack(err)))
}
}()
if err := server.Wait(); err != nil {
return errors.Wrap(err, "could not wait for server")
}
return nil
},
}
}
func addFlagsPrefix(stripped ...string) []string {
flags := make([]string, len(stripped))
for i, s := range stripped {
flags[i] = fmt.Sprintf("--%s", s)
}
return flags
}