Skip to content

Commit c826c9a

Browse files
authored
Merge branch 'main' into feature/m1
2 parents b9db1bb + 233fa60 commit c826c9a

File tree

5 files changed

+121
-3
lines changed

5 files changed

+121
-3
lines changed

internal/login/login.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ func UserCredentialsLogin(p LoginParameters) (LoginResponse, error) {
115115
u.RawQuery = q.Encode()
116116

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

120123
ur, err := userAuthServer()
121124
if err != nil {
@@ -227,12 +230,17 @@ func generateState() (string, error) {
227230
}
228231

229232
func openBrowser(url string) error {
233+
const rundllParameters = "url.dll,FileProtocolHandler"
230234
var err error
231235
switch runtime.GOOS {
232236
case "linux":
233-
err = exec.Command("xdg-open", url).Start()
237+
if util.IsWsl() {
238+
err = exec.Command("rundll32.exe", rundllParameters, url).Start()
239+
} else {
240+
err = exec.Command("xdg-open", url).Start()
241+
}
234242
case "windows":
235-
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
243+
err = exec.Command("rundll32", rundllParameters, url).Start()
236244
case "darwin":
237245
err = exec.Command("open", url).Start()
238246
default:

internal/util/syscall.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build linux
4+
5+
package util
6+
7+
import "syscall"
8+
9+
// Syscall wraps syscalls used in the application for unit testing purposes
10+
type Syscall struct {
11+
Uname func(buf *syscall.Utsname) (err error)
12+
}
13+
14+
var DefaultSyscall = Syscall{
15+
Uname: syscall.Uname,
16+
}

internal/util/wsl.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build linux
4+
5+
package util
6+
7+
import (
8+
"strings"
9+
"syscall"
10+
)
11+
12+
func IsWsl() bool {
13+
return isWsl(DefaultSyscall)
14+
}
15+
16+
// check for Windows Subsystem for Linux
17+
func isWsl(sc Syscall) bool {
18+
// the common factor between WSL distros is the Microsoft-specific kernel version, so we check for that
19+
// SUSE, WSLv1: 4.4.0-19041-Microsoft
20+
// Ubuntu, WSLv2: 4.19.128-microsoft-standard
21+
const wslIdentifier = "microsoft"
22+
var uname syscall.Utsname
23+
if err := sc.Uname(&uname); err == nil {
24+
var kernel []byte
25+
for _, b := range uname.Release {
26+
if b == 0 {
27+
break
28+
}
29+
kernel = append(kernel, byte(b))
30+
}
31+
return strings.Contains(strings.ToLower(string(kernel)), wslIdentifier)
32+
}
33+
return false
34+
}

internal/util/wsl_default.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build !linux
4+
5+
package util
6+
7+
// non-linux platforms cannot be WSL
8+
func IsWsl() bool {
9+
return false
10+
}

internal/util/wsl_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
// +build linux
4+
5+
package util
6+
7+
import (
8+
"errors"
9+
"syscall"
10+
"testing"
11+
12+
"github.com/stretchr/testify/assert"
13+
)
14+
15+
func TestIsWsl(t *testing.T) {
16+
a := assert.New(t)
17+
18+
var (
19+
// syscall.Utsname.Release value on various systems
20+
21+
// Ubuntu 20.04 on WSL2 on Windows 10 x64 20H2
22+
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}
23+
24+
// Arch Linux on baremetal on 2021-04-02
25+
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}
26+
)
27+
28+
result := isWsl(Syscall{
29+
Uname: func(buf *syscall.Utsname) (err error) {
30+
buf.Release = ubuntu20Wsl2
31+
return nil
32+
},
33+
})
34+
a.True(result)
35+
36+
result = isWsl(Syscall{
37+
Uname: func(buf *syscall.Utsname) (err error) {
38+
buf.Release = archReal
39+
return nil
40+
},
41+
})
42+
a.False(result)
43+
44+
result = isWsl(Syscall{
45+
Uname: func(buf *syscall.Utsname) (err error) {
46+
return errors.New("mocked error")
47+
},
48+
})
49+
a.False(result)
50+
}

0 commit comments

Comments
 (0)