commands: Add -O flag to server to open browser

Fixes #13040
This commit is contained in:
Bjørn Erik Pedersen 2024-11-14 15:18:27 +01:00
parent 23d21b0d16
commit ac6962d284

View file

@ -40,6 +40,7 @@ import (
"time" "time"
"github.com/bep/mclib" "github.com/bep/mclib"
"github.com/pkg/browser"
"github.com/bep/debounce" "github.com/bep/debounce"
"github.com/bep/simplecobra" "github.com/bep/simplecobra"
@ -448,6 +449,7 @@ type serverCommand struct {
// Flags. // Flags.
renderStaticToDisk bool renderStaticToDisk bool
navigateToChanged bool navigateToChanged bool
openBrowser bool
serverAppend bool serverAppend bool
serverInterface string serverInterface string
tlsCertFile string tlsCertFile string
@ -539,6 +541,7 @@ of a second, you will be able to save and see your changes nearly instantly.`
cmd.Flags().BoolVarP(&c.serverAppend, "appendPort", "", true, "append port to baseURL") cmd.Flags().BoolVarP(&c.serverAppend, "appendPort", "", true, "append port to baseURL")
cmd.Flags().BoolVar(&c.disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild") cmd.Flags().BoolVar(&c.disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild")
cmd.Flags().BoolVarP(&c.navigateToChanged, "navigateToChanged", "N", false, "navigate to changed content file on live browser reload") cmd.Flags().BoolVarP(&c.navigateToChanged, "navigateToChanged", "N", false, "navigate to changed content file on live browser reload")
cmd.Flags().BoolVarP(&c.openBrowser, "openBrowser", "O", false, "open the site in a browser after server startup")
cmd.Flags().BoolVar(&c.renderStaticToDisk, "renderStaticToDisk", false, "serve static files from disk and dynamic files from memory") cmd.Flags().BoolVar(&c.renderStaticToDisk, "renderStaticToDisk", false, "serve static files from disk and dynamic files from memory")
cmd.Flags().BoolVar(&c.disableFastRender, "disableFastRender", false, "enables full re-renders on changes") cmd.Flags().BoolVar(&c.disableFastRender, "disableFastRender", false, "enables full re-renders on changes")
cmd.Flags().BoolVar(&c.disableBrowserError, "disableBrowserError", false, "do not show build errors in the browser") cmd.Flags().BoolVar(&c.disableBrowserError, "disableBrowserError", false, "do not show build errors in the browser")
@ -998,6 +1001,13 @@ func (c *serverCommand) serve() error {
c.r.Println("Press Ctrl+C to stop") c.r.Println("Press Ctrl+C to stop")
if c.openBrowser {
// There may be more than one baseURL in multihost mode, open the first.
if err := browser.OpenURL(baseURLs[0].String()); err != nil {
c.r.logger.Warnf("Failed to open browser: %s", err)
}
}
err = func() error { err = func() error {
for { for {
select { select {