From abde5bcf1fd2ff17e3a899fba8806137675a5b29 Mon Sep 17 00:00:00 2001 From: Andrew Seigner Date: Wed, 29 Sep 2021 10:25:25 -0700 Subject: [PATCH] Modify `dashboard` cmd to handle browser failure (#33) If the `linkerd buoyant dashboard` command encountered a failure opening the default browser, it would just quit with an error. Modify `linkerd buoyant dashboard` to catch browser open errors, and instead print a message to the user instructing them to manually open a browser. This is similar to `linkerd viz dashboard` behavior. New output: ``` $ go run cli/main.go dashboard Opening Buoyant Cloud dashboard in the default browser Failed to open dashboard automatically Visit https://buoyant.cloud in your browser to view the dashboard ``` Fixes #32 Signed-off-by: Andrew Seigner --- cli/cmd/dashboard.go | 12 +++++++++++- cli/cmd/dashboard_test.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/cli/cmd/dashboard.go b/cli/cmd/dashboard.go index 9a7054f..665162c 100644 --- a/cli/cmd/dashboard.go +++ b/cli/cmd/dashboard.go @@ -1,6 +1,8 @@ package cmd import ( + "fmt" + "github.com/spf13/cobra" ) @@ -10,7 +12,15 @@ func newCmdDashboard(cfg *config, openURL openURL) *cobra.Command { Args: cobra.NoArgs, Short: "Open the Buoyant Cloud dashboard", RunE: func(cmd *cobra.Command, args []string) error { - return openURL(cfg.bcloudServer) + fmt.Fprintln(cfg.stdout, "Opening Buoyant Cloud dashboard in the default browser") + + err := openURL(cfg.bcloudServer) + if err != nil { + fmt.Fprintln(cfg.stderr, "Failed to open dashboard automatically") + fmt.Fprintf(cfg.stderr, "Visit %s in your browser to view the dashboard\n", cfg.bcloudServer) + } + + return nil }, } } diff --git a/cli/cmd/dashboard_test.go b/cli/cmd/dashboard_test.go index fe86bcd..31851e7 100644 --- a/cli/cmd/dashboard_test.go +++ b/cli/cmd/dashboard_test.go @@ -1,11 +1,20 @@ package cmd import ( + "bytes" + "errors" + "strings" "testing" ) func TestDashboard(t *testing.T) { - cfg := &config{bcloudServer: "http://example.com"} + stdout := &bytes.Buffer{} + stderr := &bytes.Buffer{} + cfg := &config{ + stdout: stdout, + stderr: stderr, + bcloudServer: "http://example.com", + } var openedUrl string mockOpen := func(url string) error { @@ -23,3 +32,27 @@ func TestDashboard(t *testing.T) { t.Fatalf("Expected to open url %s, Got %s", cfg.bcloudServer, openedUrl) } } + +func TestDashboardFailure(t *testing.T) { + stdout := &bytes.Buffer{} + stderr := &bytes.Buffer{} + cfg := &config{ + stdout: stdout, + stderr: stderr, + bcloudServer: "http://example.com", + } + + mockOpen := func(string) error { + return errors.New("browser failuer") + } + + cmd := newCmdDashboard(cfg, mockOpen) + err := cmd.RunE(cmd, nil) + if err != nil { + t.Error(err) + } + + if !strings.Contains(stderr.String(), cfg.bcloudServer) { + t.Errorf("Expected stderr to contain [%s], Got: [%s]", cfg.bcloudServer, stderr.String()) + } +}