diff options
author | Kim, Hirokuni <yangkookkim@gmail.com> | 2017-08-08 00:19:01 +0900 |
---|---|---|
committer | Kim, Hirokuni <yangkookkim@gmail.com> | 2017-08-08 00:19:01 +0900 |
commit | e9aca1407200b5243bf15d9c8d02241a0492d588 (patch) | |
tree | 7f43d146edb6203b29c4a75461ea0ffca4680e27 /plumbing/transport/ssh/auth_method.go | |
parent | da410ded51ddab7729992540b54c739e43090244 (diff) | |
download | go-git-e9aca1407200b5243bf15d9c8d02241a0492d588.tar.gz |
Avoid using user.Current()
user.Current() causes 'Current not implemented' error when
crosscompiled. See https://github.com/golang/go/issues/6376
Diffstat (limited to 'plumbing/transport/ssh/auth_method.go')
-rw-r--r-- | plumbing/transport/ssh/auth_method.go | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/plumbing/transport/ssh/auth_method.go b/plumbing/transport/ssh/auth_method.go index f95235b..baae181 100644 --- a/plumbing/transport/ssh/auth_method.go +++ b/plumbing/transport/ssh/auth_method.go @@ -3,6 +3,7 @@ package ssh import ( "crypto/x509" "encoding/pem" + "errors" "fmt" "io/ioutil" "os" @@ -11,6 +12,7 @@ import ( "gopkg.in/src-d/go-git.v4/plumbing/transport" + "github.com/mitchellh/go-homedir" "github.com/xanzy/ssh-agent" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/knownhosts" @@ -164,6 +166,19 @@ func (a *PublicKeys) clientConfig() *ssh.ClientConfig { } } +func username() (string, error) { + var username string + if user, err := user.Current(); err == nil { + username = user.Username + } else { + username = os.Getenv("USER") + } + if username == "" { + return "", errors.New("failed to get username") + } + return username, nil +} + // PublicKeysCallback implements AuthMethod by asking a // ssh.agent.Agent to act as a signer. type PublicKeysCallback struct { @@ -176,13 +191,12 @@ type PublicKeysCallback struct { // a pipe with the SSH agent and uses the pipe as the implementer of the public // key callback function. func NewSSHAgentAuth(u string) (AuthMethod, error) { + var err error if u == "" { - usr, err := user.Current() + u, err = username() if err != nil { - return nil, fmt.Errorf("error getting current user: %q", err) + return nil, err } - - u = usr.Username } a, _, err := sshagent.New() @@ -241,13 +255,13 @@ func getDefaultKnownHostsFiles() ([]string, error) { return files, nil } - user, err := user.Current() + homeDirPath, err := homedir.Dir() if err != nil { return nil, err } return []string{ - filepath.Join(user.HomeDir, "/.ssh/known_hosts"), + filepath.Join(homeDirPath, "/.ssh/known_hosts"), "/etc/ssh/ssh_known_hosts", }, nil } |