Skip to content

Commit

Permalink
handle browser opening in WSL, show URL on error
Browse files Browse the repository at this point in the history
  • Loading branch information
3ventic committed Apr 16, 2021
1 parent df96efa commit 630ae76
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"net/url"
"os/exec"
"runtime"
"strings"
"syscall"
"time"

"github.com/spf13/viper"
Expand Down Expand Up @@ -115,7 +117,10 @@ func UserCredentialsLogin(p LoginParameters) (LoginResponse, error) {
u.RawQuery = q.Encode()

fmt.Println("Opening browser. Press Ctrl+C to cancel...")
openBrowser(u.String())
err = openBrowser(u.String())
if err != nil {
fmt.Printf("Unable to open default browser. You can manually navigate to this URL to complete the login: %s\n", u.String())
}

ur, err := userAuthServer()
if err != nil {
Expand Down Expand Up @@ -226,11 +231,34 @@ func generateState() (string, error) {
return base64.URLEncoding.EncodeToString(b), nil
}

// check for Windows Subsystem for Linux
func isWsl() bool {
// the common factor between WSL distros is the Microsoft-specific kernel version, so we check for that
// SUSE, WSLv1: 4.4.0-19041-Microsoft
// Ubuntu, WSLv2: 4.19.128-microsoft-standard
var uname syscall.Utsname
if err := syscall.Uname(&uname); err == nil {
var kernel []byte
for _, b := range uname.Release {
if b == 0 {
break
}
kernel = append(kernel, byte(b))
}
return strings.Contains(strings.ToLower(string(kernel)), "microsoft")
}
return false
}

func openBrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
if isWsl() {
err = exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", url).Start()
} else {
err = exec.Command("xdg-open", url).Start()
}
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
Expand Down

0 comments on commit 630ae76

Please sign in to comment.