Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/utility-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lleadbet committed Apr 16, 2021
2 parents 8769235 + 233fa60 commit 121dfcc
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 3 deletions.
14 changes: 11 additions & 3 deletions internal/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,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 @@ -227,12 +230,17 @@ func generateState() (string, error) {
}

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 util.IsWsl() {
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
16 changes: 16 additions & 0 deletions internal/util/syscall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// +build linux

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,
}
34 changes: 34 additions & 0 deletions internal/util/wsl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// +build linux

package util

import (
"strings"
"syscall"
)

func IsWsl() bool {
return isWsl(DefaultSyscall)
}

// check for Windows Subsystem for Linux
func isWsl(sc 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
}
10 changes: 10 additions & 0 deletions internal/util/wsl_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// +build !linux

package util

// non-linux platforms cannot be WSL
func IsWsl() bool {
return false
}
50 changes: 50 additions & 0 deletions internal/util/wsl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// +build linux

package util

import (
"errors"
"syscall"
"testing"

"github.com/stretchr/testify/assert"
)

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(Syscall{
Uname: func(buf *syscall.Utsname) (err error) {
buf.Release = ubuntu20Wsl2
return nil
},
})
a.True(result)

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

result = isWsl(Syscall{
Uname: func(buf *syscall.Utsname) (err error) {
return errors.New("mocked error")
},
})
a.False(result)
}

0 comments on commit 121dfcc

Please sign in to comment.