Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 6640696

Browse files
authored
allow coder login for WSL (#446)
* allow coder login for WSL (#1) Co-authored-by: Armin <[email protected]> * use constants for runtime.GOOS * update lint errors Co-authored-by: Armin <[email protected]>
1 parent 34d96a1 commit 6640696

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

internal/cmd/login.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import (
44
"bufio"
55
"context"
66
"fmt"
7+
"io/ioutil"
78
"net/url"
9+
"os/exec"
10+
"runtime"
811
"strings"
912

1013
"github.com/pkg/browser"
@@ -66,7 +69,8 @@ func login(cmd *cobra.Command, workspaceURL *url.URL) error {
6669
q.Add("show_token", "true")
6770
authURL.RawQuery = q.Encode()
6871

69-
if err := browser.OpenURL(authURL.String()); err != nil {
72+
if err := openURL(authURL.String()); err != nil {
73+
clog.LogWarn(err.Error())
7074
fmt.Printf("Open the following in your browser:\n\n\t%s\n\n", authURL.String())
7175
} else {
7276
fmt.Printf("Your browser has been opened to visit:\n\n\t%s\n\n", authURL.String())
@@ -113,3 +117,36 @@ func pingAPI(ctx context.Context, workspaceURL *url.URL, token string) error {
113117
}
114118
return nil
115119
}
120+
121+
// isWSL determines if coder-cli is running within Windows Subsystem for Linux
122+
func isWSL() (bool, error) {
123+
if runtime.GOOS == goosDarwin || runtime.GOOS == goosWindows {
124+
return false, nil
125+
}
126+
data, err := ioutil.ReadFile("/proc/version")
127+
if err != nil {
128+
return false, xerrors.Errorf("read /proc/version: %w", err)
129+
}
130+
return strings.Contains(strings.ToLower(string(data)), "microsoft"), nil
131+
}
132+
133+
// openURL opens the provided URL via user's default browser
134+
func openURL(url string) error {
135+
var cmd string
136+
var args []string
137+
138+
wsl, err := isWSL()
139+
if err != nil {
140+
return xerrors.Errorf("test running Windows Subsystem for Linux: %w", err)
141+
}
142+
143+
if wsl {
144+
cmd = "cmd.exe"
145+
args = []string{"/c", "start"}
146+
url = strings.ReplaceAll(url, "&", "^&")
147+
args = append(args, url)
148+
return exec.Command(cmd, args...).Start()
149+
}
150+
151+
return browser.OpenURL(url)
152+
}

0 commit comments

Comments
 (0)