Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt(hgctl/dashboard): avoid printing error messages cannot open browser #665

Merged
merged 8 commits into from
Dec 6, 2023
23 changes: 17 additions & 6 deletions pkg/cmd/hgctl/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ func ClosePortForwarderOnInterrupt(fw kubernetes.PortForwarder) {
}

func openBrowser(url string, writer io.Writer, browser bool) {
var err error

fmt.Fprintf(writer, "%s\n", url)

if !browser {
Expand All @@ -372,16 +370,29 @@ func openBrowser(url string, writer io.Writer, browser bool) {

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
openCommand(writer, "xdg-open", url)
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
openCommand(writer, "rundll32", "url.dll,FileProtocolHandler", url)
case "darwin":
err = exec.Command("open", url).Start()
openCommand(writer, "open", url)
default:
fmt.Fprintf(writer, "Unsupported platform %q; open %s in your browser.\n", runtime.GOOS, url)
}
}

func openCommand(writer io.Writer, command string, args ...string) {
_, err := exec.LookPath(command)
if err != nil {
if errors.Is(err, exec.ErrNotFound) {
fmt.Fprintf(writer, "Could not open your browser. Please open it maually.\n")
return
}
fmt.Fprintf(writer, "Failed to open browser; open %s in your browser.\nError: %s\n", args[0], err.Error())
return
}

err = exec.Command(command, args...).Start()
if err != nil {
fmt.Fprintf(writer, "Failed to open browser; open %s in your browser.\nError: %s\n", url, err.Error())
fmt.Fprintf(writer, "Failed to open browser; open %s in your browser.\nError: %s\n", args[0], err.Error())
}
}
Loading