Repository: gohugoio/hugo (language: go)

Issue:
Command line flag to open localhost:1313
As a not-very-technical hugo user
I would like to generate my site locally to test (heavily using `hugo server -D`) and see it immediately upon build in a local browser.
So that I can review it works as I would have expected.

`hugo server -D && open http://localhost:1313` doesn't function, as it requires the server command to complete before opening the browser
`open http://localhost:1313 && hugo server -D` does function as needed, but doesn't match the order one would think about these things.

The best result would be `hugo server -D -o` -- so that the browser opens to the localhost immediately on the first build of the site, leaving the server running.


Discussion / hints:
I think we have had this discussion before, and I think the argument against it was that it wasn't a good/simple multi platform way to do this. But I see now that this exists, which looks solid enough:

https://github.com/pkg/browser

As to flags, I would suggest we go for -O (uppercase). I have tried to make it lowercase=configuration, uppercase=actions. Some of the uppercase are a little bit borderline, but I think it makes sense.

So:

```
hugo server -O
```



Fix patch:
diff --git a/commands/server.go b/commands/server.go
index 6b801b158c8..c2fee68b239 100644
--- a/commands/server.go
+++ b/commands/server.go
@@ -40,6 +40,7 @@ import (
 	"time"
 
 	"github.com/bep/mclib"
+	"github.com/pkg/browser"
 
 	"github.com/bep/debounce"
 	"github.com/bep/simplecobra"
@@ -448,6 +449,7 @@ type serverCommand struct {
 	// Flags.
 	renderStaticToDisk  bool
 	navigateToChanged   bool
+	openBrowser         bool
 	serverAppend        bool
 	serverInterface     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().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.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.disableFastRender, "disableFastRender", false, "enables full re-renders on changes")
 	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")
 
+	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 {
 		for {
 			select {


Test patch: