diff options
| author | Daniel Theophanes <kardianos@gmail.com> | 2016-03-29 07:14:43 -0700 |
|---|---|---|
| committer | Andrew Gerrand <adg@golang.org> | 2016-05-31 03:30:11 +0000 |
| commit | 55c7d78c71a67acd97b52778826c5fe108bfc529 (patch) | |
| tree | a7e4165413e69e5f58b929310b1e6684e61593f3 /git-codereview/api.go | |
| parent | 510dcfe65574591e23e3e3e8488deee0642b2f2e (diff) | |
| download | go-x-review-55c7d78c71a67acd97b52778826c5fe108bfc529.tar.xz | |
review: look in correct location for netrc on Windows.
Change-Id: I38a58a2fb1bf6f4840cca8fe24aaa85ad3d6289e
Reviewed-on: https://go-review.googlesource.com/21252
Run-TryBot: Andrew Gerrand <adg@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Andrew Gerrand <adg@golang.org>
Diffstat (limited to 'git-codereview/api.go')
| -rw-r--r-- | git-codereview/api.go | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/git-codereview/api.go b/git-codereview/api.go index 786ac60..910900f 100644 --- a/git-codereview/api.go +++ b/git-codereview/api.go @@ -12,7 +12,9 @@ import ( "io/ioutil" "net/http" "net/url" - "os" + "os/user" + "path/filepath" + "runtime" "sort" "strings" ) @@ -120,6 +122,19 @@ func loadGerritOriginInternal(origin, remoteOrigin string) error { return nil } +// testHomeDir is empty for normal use. During tests it may be set and used +// in place of the actual home directory. Tests may still need to +// set the HOME var for sub-processes such as git. +var testHomeDir = "" + +func netrcName() string { + // Git on Windows will look in $HOME\_netrc. + if runtime.GOOS == "windows" { + return "_netrc" + } + return ".netrc" +} + // loadAuth loads the authentication tokens for making API calls to // the Gerrit origin host. func loadAuth() { @@ -152,7 +167,16 @@ func loadAuth() { // If not there, then look in $HOME/.netrc, which is where Gerrit // used to tell users to store the information, until the passwords // got so long that old versions of curl couldn't handle them. - data, _ := ioutil.ReadFile(os.Getenv("HOME") + "/.netrc") + netrc := netrcName() + homeDir := testHomeDir + if homeDir == "" { + usr, err := user.Current() + if err != nil { + dief("failed to get current user home directory to look for %q: %v", netrc, err) + } + homeDir = usr.HomeDir + } + data, _ := ioutil.ReadFile(filepath.Join(homeDir, netrc)) for _, line := range lines(string(data)) { if i := strings.Index(line, "#"); i >= 0 { line = line[:i] |
