Skip to content

Allow twitch-cli to open default browser in Windows when run inside WSL #58

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

Merged
merged 4 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 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,13 +231,38 @@ func generateState() (string, error) {
return base64.URLEncoding.EncodeToString(b), nil
}

// check for Windows Subsystem for Linux
func isWsl(sc util.Syscall) 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
const wslIdentifier = "microsoft"
var uname syscall.Utsname
if err := sc.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)), wslIdentifier)
}
return false
}

func openBrowser(url string) error {
const rundllParameters = "url.dll,FileProtocolHandler"
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
if isWsl(util.DefaultSyscall) {
err = exec.Command("rundll32.exe", rundllParameters, url).Start()
} else {
err = exec.Command("xdg-open", url).Start()
}
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
err = exec.Command("rundll32", rundllParameters, url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
Expand Down
40 changes: 40 additions & 0 deletions internal/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ package login

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"syscall"
"testing"
"time"

"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/twitchdev/twitch-cli/internal/util"
)

Expand Down Expand Up @@ -157,3 +160,40 @@ func TestUserAuthServer(t *testing.T) {
a.Equal(state, ur.State, "State mismatch")
a.Equal(code, ur.Code, "Code mismatch")
}

func TestIsWsl(t *testing.T) {
a := assert.New(t)

var (
// syscall.Utsname.Release value on various systems

// Ubuntu 20.04 on WSL2 on Windows 10 x64 20H2
ubuntu20Wsl2 = [65]int8{52, 46, 49, 57, 46, 49, 50, 56, 45, 109, 105, 99, 114, 111, 115, 111, 102, 116, 45, 115, 116, 97, 110, 100, 97, 114, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

// Arch Linux on baremetal on 2021-04-02
archReal = [65]int8{53, 46, 49, 49, 46, 49, 49, 45, 97, 114, 99, 104, 49, 45, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
)

result := isWsl(util.Syscall{
Uname: func(buf *syscall.Utsname) (err error) {
buf.Release = ubuntu20Wsl2
return nil
},
})
a.True(result)

result = isWsl(util.Syscall{
Uname: func(buf *syscall.Utsname) (err error) {
buf.Release = archReal
return nil
},
})
a.False(result)

result = isWsl(util.Syscall{
Uname: func(buf *syscall.Utsname) (err error) {
return errors.New("mocked error")
},
})
a.False(result)
}
12 changes: 12 additions & 0 deletions internal/util/syscall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package util

import "syscall"

// Syscall wraps syscalls used in the application for unit testing purposes
type Syscall struct {
Uname func(buf *syscall.Utsname) (err error)
}

var DefaultSyscall = Syscall{
Uname: syscall.Uname,
}