check properly for username on different platforms

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
Evan Hazlett 2015-01-27 10:28:55 -05:00
parent d5a4f2f1c4
commit 720c4edfd2
No known key found for this signature in database
GPG Key ID: A519480096146526
2 changed files with 15 additions and 11 deletions

View File

@ -11,16 +11,12 @@ import (
) )
func before(c *cli.Context) error { func before(c *cli.Context) error {
caCertPath := c.GlobalString("tls-ca-cert") caCertPath := c.GlobalString("tls-ca-cert")
caKeyPath := c.GlobalString("tls-ca-key") caKeyPath := c.GlobalString("tls-ca-key")
clientCertPath := c.GlobalString("tls-client-cert") clientCertPath := c.GlobalString("tls-client-cert")
clientKeyPath := c.GlobalString("tls-client-key") clientKeyPath := c.GlobalString("tls-client-key")
org, err := utils.GetUsername() org := utils.GetUsername()
if err != nil {
return err
}
bits := 2048 bits := 2048
if _, err := os.Stat(utils.GetMachineDir()); err != nil { if _, err := os.Stat(utils.GetMachineDir()); err != nil {

View File

@ -3,7 +3,6 @@ package utils
import ( import (
"io" "io"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"runtime" "runtime"
) )
@ -27,13 +26,22 @@ func GetMachineClientCertDir() string {
return filepath.Join(GetMachineDir(), ".client") return filepath.Join(GetMachineDir(), ".client")
} }
func GetUsername() (string, error) { func GetUsername() string {
u, err := user.Current() u := "unknown"
if err != nil { osUser := ""
return "", err
switch runtime.GOOS {
case "darwin", "linux":
osUser = os.Getenv("USER")
case "windows":
osUser = os.Getenv("USERNAME")
} }
return u.Username, nil if osUser != "" {
u = osUser
}
return u
} }
func CopyFile(src, dst string) error { func CopyFile(src, dst string) error {