diff --git a/cmd/cloudflared/shell/launch_browser_darwin.go b/cmd/cloudflared/shell/launch_browser_darwin.go new file mode 100644 index 00000000..064d892d --- /dev/null +++ b/cmd/cloudflared/shell/launch_browser_darwin.go @@ -0,0 +1,11 @@ +//+build darwin + +package shell + +import ( + "os/exec" +) + +func getBrowserCmd(url string) *exec.Cmd { + return exec.Command("open", url) +} diff --git a/cmd/cloudflared/shell/launch_browser_other.go b/cmd/cloudflared/shell/launch_browser_other.go new file mode 100644 index 00000000..9c78b959 --- /dev/null +++ b/cmd/cloudflared/shell/launch_browser_other.go @@ -0,0 +1,11 @@ +//+build !windows,!darwin,!linux,!netbsd,!freebsd,!openbsd + +package shell + +import ( + "os/exec" +) + +func getBrowserCmd(url string) *exec.Cmd { + return nil +} diff --git a/cmd/cloudflared/shell/launch_browser_unix.go b/cmd/cloudflared/shell/launch_browser_unix.go new file mode 100644 index 00000000..d9824214 --- /dev/null +++ b/cmd/cloudflared/shell/launch_browser_unix.go @@ -0,0 +1,11 @@ +//+build linux freebsd openbsd netbsd + +package shell + +import ( + "os/exec" +) + +func getBrowserCmd(url string) *exec.Cmd { + return exec.Command("xdg-open", url) +} diff --git a/cmd/cloudflared/shell/launch_browser_windows.go b/cmd/cloudflared/shell/launch_browser_windows.go new file mode 100644 index 00000000..ab300403 --- /dev/null +++ b/cmd/cloudflared/shell/launch_browser_windows.go @@ -0,0 +1,18 @@ +//+build windows + +package shell + +import ( + "fmt" + "os/exec" + "syscall" +) + +func getBrowserCmd(url string) *exec.Cmd { + cmd := exec.Command("cmd") + // CmdLine is only defined when compiling for windows. + // Empty string is the cmd proc "Title". Needs to be included because the start command will interpret the first + // quoted string as that field and we want to quote the URL. + cmd.SysProcAttr = &syscall.SysProcAttr{CmdLine: fmt.Sprintf(`/c start "" "%s"`, url)} + return cmd +} diff --git a/cmd/cloudflared/shell/shell.go b/cmd/cloudflared/shell/shell.go index c3787a68..84779b40 100644 --- a/cmd/cloudflared/shell/shell.go +++ b/cmd/cloudflared/shell/shell.go @@ -4,25 +4,11 @@ import ( "io" "os" "os/exec" - "runtime" ) // OpenBrowser opens the specified URL in the default browser of the user func OpenBrowser(url string) error { - var cmd string - var args []string - - switch runtime.GOOS { - case "windows": - cmd = "cmd" - args = []string{"/c", "start"} - case "darwin": - cmd = "open" - default: // "linux", "freebsd", "openbsd", "netbsd" - cmd = "xdg-open" - } - args = append(args, url) - return exec.Command(cmd, args...).Start() + return getBrowserCmd(url).Start() } // Run will kick off a shell task and pipe the results to the respective std pipes