feat: add browserless mode
Add 3 new "edit" command parameters: - `--http-port` - Specify the HTTP port used by the web server (instead of 127.0.0.1) - `--http-host` - Specify the HTTP host used by the web server (instead of a random one) - `--no-browser` - Disable the automatic start of the web browser
This commit is contained in:
parent
29026a6295
commit
ddbe4c0f26
|
@ -17,11 +17,28 @@ import (
|
|||
func Edit() *cli.Command {
|
||||
flags := commonFlags()
|
||||
|
||||
flags = append(flags, &cli.StringFlag{
|
||||
flags = append(flags,
|
||||
&cli.StringFlag{
|
||||
Name: "browser",
|
||||
EnvVars: []string{"FORMIDABLE_BROWSER"},
|
||||
Value: "w3m",
|
||||
})
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "http-host",
|
||||
EnvVars: []string{"FORMIDABLE_HTTP_HOST"},
|
||||
Value: "127.0.0.1",
|
||||
},
|
||||
&cli.UintFlag{
|
||||
Name: "http-port",
|
||||
EnvVars: []string{"FORMIDABLE_HTTP_PORT"},
|
||||
Value: 0,
|
||||
},
|
||||
&cli.BoolFlag{
|
||||
Name: "no-browser",
|
||||
EnvVars: []string{"FORMIDABLE_NO_BROWSER"},
|
||||
Value: false,
|
||||
},
|
||||
)
|
||||
|
||||
return &cli.Command{
|
||||
Name: "edit",
|
||||
|
@ -29,6 +46,9 @@ func Edit() *cli.Command {
|
|||
Flags: flags,
|
||||
Action: func(ctx *cli.Context) error {
|
||||
browser := ctx.String("browser")
|
||||
noBrowser := ctx.Bool("no-browser")
|
||||
httpPort := ctx.Uint("http-port")
|
||||
httpHost := ctx.String("http-host")
|
||||
|
||||
schema, err := loadSchema(ctx)
|
||||
if err != nil {
|
||||
|
@ -49,6 +69,7 @@ func Edit() *cli.Command {
|
|||
defer srvCancel()
|
||||
|
||||
srv := server.New(
|
||||
server.WithAddress(httpHost, httpPort),
|
||||
server.WithSchema(schema),
|
||||
server.WithValues(values),
|
||||
server.WithDefaults(defaults),
|
||||
|
@ -68,16 +89,38 @@ func Edit() *cli.Command {
|
|||
|
||||
log.Printf("listening on %s", url)
|
||||
|
||||
browserErrs := make(chan error)
|
||||
browserCtx, browserCancel := context.WithCancel(ctx.Context)
|
||||
defer browserCancel()
|
||||
|
||||
if !noBrowser {
|
||||
browserErrs = startBrowser(browserCtx, browser, url)
|
||||
}
|
||||
|
||||
select {
|
||||
case err := <-browserErrs:
|
||||
srvCancel()
|
||||
|
||||
return errors.WithStack(err)
|
||||
|
||||
case err := <-srvErrs:
|
||||
browserCancel()
|
||||
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func startBrowser(ctx context.Context, browser, url string) chan error {
|
||||
cmdErrs := make(chan error)
|
||||
cmdCtx, cmdCancel := context.WithCancel(ctx.Context)
|
||||
defer cmdCancel()
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
close(cmdErrs)
|
||||
}()
|
||||
|
||||
cmd := exec.CommandContext(cmdCtx, browser, url)
|
||||
cmd := exec.CommandContext(ctx, browser, url)
|
||||
|
||||
cmd.Stdin = os.Stdin
|
||||
cmd.Stderr = os.Stderr
|
||||
|
@ -89,17 +132,5 @@ func Edit() *cli.Command {
|
|||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-cmdErrs:
|
||||
srvCancel()
|
||||
|
||||
return errors.WithStack(err)
|
||||
|
||||
case err := <-srvErrs:
|
||||
cmdCancel()
|
||||
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
},
|
||||
}
|
||||
return cmdErrs
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue