2
0

wsl.go 723 B

1234567891011121314151617181920212223242526272829
  1. package utils
  2. import (
  3. "os/exec"
  4. "regexp"
  5. "strings"
  6. )
  7. // Checks based on uname if the linux environment is under wsl or not
  8. func CheckIfWsl() bool {
  9. out, err := exec.Command("uname", "-a").Output()
  10. if err != nil {
  11. return false
  12. }
  13. // On some cases, uname on wsl outputs microsoft capitalized
  14. matched, _ := regexp.Match(`microsoft|Microsoft`, out)
  15. return matched
  16. }
  17. // Gets the subsystem host ip
  18. // If the CLI is running under WSL the localhost url will not work so
  19. // this function should return the real ip that we should redirect to
  20. func GetWslHostName() string {
  21. out, err := exec.Command("wsl.exe", "hostname", "-I").Output()
  22. if err != nil {
  23. return "localhost"
  24. }
  25. return strings.TrimSpace(string(out))
  26. }