From 2666ce01ff5ec1c87d18b1b6069dcd4c400eb8fb Mon Sep 17 00:00:00 2001 From: Armin <4032150+arminaaki@users.noreply.github.com> Date: Tue, 21 Sep 2021 23:32:43 -0400 Subject: [PATCH 1/3] allow coder login for WSL (#1) Co-authored-by: Armin --- internal/cmd/login.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/internal/cmd/login.go b/internal/cmd/login.go index a706259c..2931a3c5 100644 --- a/internal/cmd/login.go +++ b/internal/cmd/login.go @@ -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 := browser.OpenURL(authURL.String()); err != nil { + if err := openURL(authURL.String()); err != nil { + clog.LogWarn(err.Error()) 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 == "darwin" || runtime.GOOS == "windows" { + 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() + 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.Replace(url, "&", "^&", -1) + args = append(args, url) + return exec.Command(cmd, args...).Start() + } + + return browser.OpenURL(url) +} From c76660ffb4bec2bef3058a957eaa4d6a38758a44 Mon Sep 17 00:00:00 2001 From: Armin Date: Fri, 24 Sep 2021 18:17:00 -0400 Subject: [PATCH 2/3] use constants for runtime.GOOS --- internal/cmd/configssh.go | 4 ++-- internal/cmd/login.go | 2 +- internal/cmd/update.go | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/cmd/configssh.go b/internal/cmd/configssh.go index 9740a102..a4b9f8de 100644 --- a/internal/cmd/configssh.go +++ b/internal/cmd/configssh.go @@ -160,7 +160,7 @@ func binPath() (string, error) { // Bash and OpenSSH for Windows (used by Powershell and VS Code) to function // correctly. Check if the current executable is in $PATH, and warn the user // if it isn't. - if runtime.GOOS == "windows" { + if runtime.GOOS == goosWindows { binName := filepath.Base(exePath) // We use safeexec instead of os/exec because os/exec returns paths in @@ -268,7 +268,7 @@ func makeSSHConfig(binPath, workspaceName, privateKeyFilepath string, additional fmt.Sprintf("IdentityFile=%q", privateKeyFilepath), ) - if runtime.GOOS == "linux" || runtime.GOOS == "darwin" { + if runtime.GOOS == goosLinux || runtime.GOOS == goosDarwin { options = append(options, "ControlMaster auto", "ControlPath ~/.ssh/.connection-%r@%h:%p", diff --git a/internal/cmd/login.go b/internal/cmd/login.go index 2931a3c5..996eae72 100644 --- a/internal/cmd/login.go +++ b/internal/cmd/login.go @@ -120,7 +120,7 @@ func pingAPI(ctx context.Context, workspaceURL *url.URL, token string) error { // isWSL determines if coder-cli is running within Windows Subsystem for Linux func isWSL() (bool, error) { - if runtime.GOOS == "darwin" || runtime.GOOS == "windows" { + if runtime.GOOS == goosDarwin || runtime.GOOS == goosWindows { return false, nil } data, err := ioutil.ReadFile("/proc/version") diff --git a/internal/cmd/update.go b/internal/cmd/update.go index 0eda22ad..d321ab7e 100644 --- a/internal/cmd/update.go +++ b/internal/cmd/update.go @@ -36,6 +36,7 @@ import ( const ( goosWindows = "windows" goosLinux = "linux" + goosDarwin = "darwin" apiPrivateVersion = "/api/private/version" ) @@ -181,7 +182,7 @@ func (u *updater) Run(ctx context.Context, force bool, coderURLArg string, versi // TODO: validate the checksum of the downloaded file. GitHub does not currently provide this information // and we do not generate them yet. var updatedBinaryName string - if u.osF() == "windows" { + if u.osF() == goosWindows { updatedBinaryName = "coder.exe" } else { updatedBinaryName = "coder" From 15d241b7388468deb33ac006aecef38c3d2eeadc Mon Sep 17 00:00:00 2001 From: Armin Date: Fri, 24 Sep 2021 18:18:43 -0400 Subject: [PATCH 3/3] update lint errors --- internal/cmd/login.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/cmd/login.go b/internal/cmd/login.go index 996eae72..fe4025fd 100644 --- a/internal/cmd/login.go +++ b/internal/cmd/login.go @@ -143,7 +143,7 @@ func openURL(url string) error { if wsl { cmd = "cmd.exe" args = []string{"/c", "start"} - url = strings.Replace(url, "&", "^&", -1) + url = strings.ReplaceAll(url, "&", "^&") args = append(args, url) return exec.Command(cmd, args...).Start() }