- Notifications
You must be signed in to change notification settings - Fork18
allow coder login for WSL#446
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -4,7 +4,10 @@ import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"net/url" | ||
"os/exec" | ||
"runtime" | ||
"strings" | ||
"github.com/pkg/browser" | ||
@@ -66,7 +69,8 @@ func login(cmd *cobra.Command, workspaceURL *url.URL) error { | ||
q.Add("show_token", "true") | ||
authURL.RawQuery = q.Encode() | ||
if err := openURL(authURL.String()); err != nil { | ||
clog.LogWarn(err.Error()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Thanks for the additional warning log 👍🏻 | ||
fmt.Printf("Open the following in your browser:\n\n\t%s\n\n", authURL.String()) | ||
} else { | ||
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 { | ||
} | ||
return nil | ||
} | ||
// isWSL determines if coder-cli is running within Windows Subsystem for Linux | ||
func isWSL() (bool, error) { | ||
if runtime.GOOS == goosDarwin || runtime.GOOS == goosWindows { | ||
return false, nil | ||
} | ||
data, err := ioutil.ReadFile("/proc/version") | ||
if err != nil { | ||
return false, xerrors.Errorf("read /proc/version: %w", err) | ||
} | ||
return strings.Contains(strings.ToLower(string(data)), "microsoft"), nil | ||
} | ||
// openURL opens the provided URL via user's default browser | ||
func openURL(url string) error { | ||
var cmd string | ||
var args []string | ||
wsl, err := isWSL() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I think this is good, but I do wonder whether we could propose this upstream to pkg/browser There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Thank you for the review. I agree, this work should ideally be part of upstream There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Yeah, I think it makes sense to make this change now for sure! No objection to it :) Thanks for your contribution! | ||
if err != nil { | ||
return xerrors.Errorf("test running Windows Subsystem for Linux: %w", err) | ||
} | ||
if wsl { | ||
cmd = "cmd.exe" | ||
args = []string{"/c", "start"} | ||
url = strings.ReplaceAll(url, "&", "^&") | ||
args = append(args, url) | ||
return exec.Command(cmd, args...).Start() | ||
} | ||
return browser.OpenURL(url) | ||
} |