Skip to content

Commit

Permalink
Modify dashboard cmd to handle browser failure (#33)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
siggy authored Sep 29, 2021
1 parent d86ee69 commit abde5bc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
12 changes: 11 additions & 1 deletion cli/cmd/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

Expand All @@ -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
},
}
}
35 changes: 34 additions & 1 deletion cli/cmd/dashboard_test.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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())
}
}

0 comments on commit abde5bc

Please sign in to comment.