jnfrati 4 лет назад
Родитель
Сommit
e49ce886ea
3 измененных файлов с 42 добавлено и 16 удалено
  1. 8 1
      cli/cmd/login/server.go
  2. 5 15
      cli/cmd/utils/browser.go
  3. 29 0
      cli/cmd/utils/wsl.go

+ 8 - 1
cli/cmd/login/server.go

@@ -55,7 +55,14 @@ func Login(
 	}()
 
 	// open browser for host login
-	redirectHost := fmt.Sprintf("http://localhost:%d", port)
+	var redirectHost string
+	if utils.CheckIfWsl() {
+		fmt.Println("Got till here")
+		redirectHost = fmt.Sprintf("http://%s:%d", utils.GetWslHostName(), port)
+	} else {
+		redirectHost = fmt.Sprintf("http://localhost:%d", port)
+	}
+
 	loginURL := fmt.Sprintf("%s/api/cli/login?redirect=%s", host, url.QueryEscape(redirectHost))
 
 	err = utils.OpenBrowser(loginURL)

+ 5 - 15
cli/cmd/utils/browser.go

@@ -1,23 +1,11 @@
 package utils
 
 import (
+	"fmt"
 	"os/exec"
-	"regexp"
 	"runtime"
 )
 
-func checkIfWsl() bool {
-	out, err := exec.Command("uname", "-a").Output()
-	if err != nil {
-		return false
-	}
-	// On some cases, uname on wsl outputs microsoft capitalized
-	if matched, err := regexp.Match(`microsoft|Microsoft`, out); err != nil {
-		return matched
-	}
-	return false
-}
-
 // OpenBrowser opens the specified URL in the default browser of the user.
 func OpenBrowser(url string) error {
 	var cmd string
@@ -30,12 +18,14 @@ func OpenBrowser(url string) error {
 	case "darwin":
 		cmd = "open"
 	default: // "linux", "freebsd", "openbsd", "netbsd"
-		if checkIfWsl() {
-			cmd = "explorer.exe"
+		if CheckIfWsl() {
+			cmd = "cmd.exe"
+			args = []string{"/c", "start"}
 		} else {
 			cmd = "xdg-open"
 		}
 	}
+	fmt.Println(url)
 	args = append(args, url)
 	return exec.Command(cmd, args...).Start()
 }

+ 29 - 0
cli/cmd/utils/wsl.go

@@ -0,0 +1,29 @@
+package utils
+
+import (
+	"os/exec"
+	"regexp"
+	"strings"
+)
+
+// Checks based on uname if the linux environment is under wsl or not
+func CheckIfWsl() bool {
+	out, err := exec.Command("uname", "-a").Output()
+	if err != nil {
+		return false
+	}
+	// On some cases, uname on wsl outputs microsoft capitalized
+	matched, _ := regexp.Match(`microsoft|Microsoft`, out)
+	return matched
+}
+
+// Gets the subsystem host ip
+// If the CLI is running under WSL the localhost url will not work so
+// this function should return the real ip that we should redirect to
+func GetWslHostName() string {
+	out, err := exec.Command("wsl.exe", "hostname", "-I").Output()
+	if err != nil {
+		return "localhost"
+	}
+	return strings.TrimSpace(string(out))
+}