Skip to content

Commit

Permalink
Vault login for WSL - Hardened openURL function for WSL (#77)
Browse files Browse the repository at this point in the history
Fixes #76
  • Loading branch information
jamesburns-rts authored and Jim Kalafut committed Oct 10, 2019
1 parent 6e442bc commit 65cf93b
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jwtauth
import (
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -135,18 +136,31 @@ func fetchAuthURL(c *api.Client, role, mount, port string, callbackHost string)
return authURL, nil
}

// isWSL tests if the binary is being run in Windows Subsystem for Linux
func isWSL() bool {
if runtime.GOOS == "darwin" || runtime.GOOS == "windows" {
return false
}
data, err := ioutil.ReadFile("/proc/version")
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to read /proc/version.\n")
return false
}
return strings.Contains(strings.ToLower(string(data)), "microsoft")
}

// openURL opens the specified URL in the default browser of the user.
// Source: https://stackoverflow.com/a/39324149/453290
func openURL(url string) error {
var cmd string
var args []string

switch runtime.GOOS {
case "windows":
cmd = "cmd"
switch {
case "windows" == runtime.GOOS || isWSL():
cmd = "cmd.exe"
args = []string{"/c", "start"}
url = strings.Replace(url, "&", "^&", -1)
case "darwin":
case "darwin" == runtime.GOOS:
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
Expand Down

0 comments on commit 65cf93b

Please sign in to comment.