x/pkgsite: add -open to open browser

Add -open command line flag to cmd/pkgsite which tries to open a browser to the newly running server.

This provides a simple way to open a browser to pkgsite service without having do paste the URL into a browser manually.

It leverages the internal/browser package from go for this functionality as used by go tool cover -html.

Fixes golang/go#60002

Change-Id: I94ba4cde2aa0830630bf0e9d7710414db9801606
GitHub-Last-Rev: 4e4f3cd79b94c7301e7ef563c65b6ca2551d536d
GitHub-Pull-Request: golang/pkgsite#64
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/493075
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go
index 46ad2ea..85ce83e 100644
--- a/cmd/pkgsite/main.go
+++ b/cmd/pkgsite/main.go
@@ -55,6 +55,7 @@
 	"flag"
 	"fmt"
 	"io/fs"
+	"net"
 	"net/http"
 	"os"
 	"os/exec"
@@ -65,6 +66,7 @@
 
 	"github.com/google/safehtml/template"
 	"golang.org/x/pkgsite/internal"
+	"golang.org/x/pkgsite/internal/browser"
 	"golang.org/x/pkgsite/internal/fetch"
 	"golang.org/x/pkgsite/internal/fetchdatasource"
 	"golang.org/x/pkgsite/internal/frontend"
@@ -85,6 +87,7 @@
 	useProxy   = flag.Bool("proxy", false, "fetch from GOPROXY if not found locally")
 	devMode    = flag.Bool("dev", false, "enable developer mode (reload templates on each page load, serve non-minified JS/CSS, etc.)")
 	staticFlag = flag.String("static", "static", "path to folder containing static files served")
+	openFlag   = flag.Bool("open", false, "open a browser window to the server's address")
 	// other flags are bound to serverConfig below
 )
 
@@ -142,11 +145,32 @@
 		die(err.Error())
 	}
 
+	addr := *httpAddr
+	if addr == "" {
+		addr = ":http"
+	}
+
+	ln, err := net.Listen("tcp", addr)
+	if err != nil {
+		die(err.Error())
+	}
+
+	url := "http://" + addr
+	log.Infof(ctx, "Listening on addr %s", url)
+
+	if *openFlag {
+		go func() {
+			if !browser.Open(url) {
+				log.Infof(ctx, "Failed to open browser window. Please visit %s in your browser.", url)
+			}
+		}()
+	}
+
 	router := http.NewServeMux()
 	server.Install(router.Handle, nil, nil)
 	mw := middleware.Timeout(54 * time.Second)
-	log.Infof(ctx, "Listening on addr http://%s", *httpAddr)
-	die("%v", http.ListenAndServe(*httpAddr, mw(router)))
+	srv := &http.Server{Addr: addr, Handler: mw(router)}
+	die("%v", srv.Serve(ln))
 }
 
 func die(format string, args ...any) {
diff --git a/internal/browser/browser.go b/internal/browser/browser.go
new file mode 100644
index 0000000..f205dea
--- /dev/null
+++ b/internal/browser/browser.go
@@ -0,0 +1,68 @@
+// Copyright 2016 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Package browser provides utilities for interacting with users' browsers.
+// Copied from: https://go.googlesource.com/go/src/cmd/internal/browser/browser.go
+package browser
+
+import (
+	"os"
+	"os/exec"
+	"runtime"
+	"time"
+)
+
+// Commands returns a list of possible commands to use to open a url.
+func Commands() [][]string {
+	var cmds [][]string
+	if exe := os.Getenv("BROWSER"); exe != "" {
+		cmds = append(cmds, []string{exe})
+	}
+	switch runtime.GOOS {
+	case "darwin":
+		cmds = append(cmds, []string{"/usr/bin/open"})
+	case "windows":
+		cmds = append(cmds, []string{"cmd", "/c", "start"})
+	default:
+		if os.Getenv("DISPLAY") != "" {
+			// xdg-open is only for use in a desktop environment.
+			cmds = append(cmds, []string{"xdg-open"})
+		}
+	}
+	cmds = append(cmds,
+		[]string{"chrome"},
+		[]string{"google-chrome"},
+		[]string{"chromium"},
+		[]string{"firefox"},
+	)
+	return cmds
+}
+
+// Open tries to open url in a browser and reports whether it succeeded.
+func Open(url string) bool {
+	for _, args := range Commands() {
+		cmd := exec.Command(args[0], append(args[1:], url)...)
+		if cmd.Start() == nil && appearsSuccessful(cmd, 3*time.Second) {
+			return true
+		}
+	}
+	return false
+}
+
+// appearsSuccessful reports whether the command appears to have run successfully.
+// If the command runs longer than the timeout, it's deemed successful.
+// If the command runs within the timeout, it's deemed successful if it exited cleanly.
+func appearsSuccessful(cmd *exec.Cmd, timeout time.Duration) bool {
+	errc := make(chan error, 1)
+	go func() {
+		errc <- cmd.Wait()
+	}()
+
+	select {
+	case <-time.After(timeout):
+		return true
+	case err := <-errc:
+		return err == nil
+	}
+}