Skip to content

Commit b571854

Browse files
committed
netrc.go: consider same machine may have different login names
1 parent e0e9ca4 commit b571854

File tree

3 files changed

+63
-22
lines changed

3 files changed

+63
-22
lines changed

netrc/examples/good.netrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ put src2/*
1414

1515
machine ray login demo password mypassword
1616

17+
machine ray login demo1 password demo1_password
18+
1719
machine weirdlogin login uname password pass#pass
1820

1921
machine google.com

netrc/netrc.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,18 @@ type Netrc struct {
4545
}
4646

4747
// FindMachine returns the Machine in n named by name. If a machine named by
48-
// name exists, it is returned. If no Machine with name name is found and there
48+
// name exists, it is returned. It would also compare login name if it's provided.
49+
// If no Machine with name name is found and there
4950
// is a ``default'' machine, the ``default'' machine is returned. Otherwise, nil
5051
// is returned.
51-
func (n *Netrc) FindMachine(name string) (m *Machine) {
52+
func (n *Netrc) FindMachine(name string, loginName string) (m *Machine) {
5253
// TODO(bgentry): not safe for concurrency
5354
var def *Machine
5455
for _, m = range n.machines {
5556
if m.Name == name {
57+
if loginName != "" && m.Login != loginName {
58+
continue
59+
}
5660
return m
5761
}
5862
if m.IsDefault() {
@@ -160,12 +164,15 @@ func (n *Netrc) insertMachineTokensBeforeDefault(m *Machine) {
160164
return
161165
}
162166

163-
func (n *Netrc) RemoveMachine(name string) {
167+
func (n *Netrc) RemoveMachine(name string, loginName string) {
164168
n.updateLock.Lock()
165169
defer n.updateLock.Unlock()
166170

167171
for i := range n.machines {
168172
if n.machines[i] != nil && n.machines[i].Name == name {
173+
if loginName != "" && n.machines[i].Login != loginName {
174+
continue
175+
}
169176
m := n.machines[i]
170177
for _, t := range []*token{
171178
m.nametoken, m.logintoken, m.passtoken, m.accounttoken,
@@ -500,14 +507,15 @@ func Parse(r io.Reader) (*Netrc, error) {
500507
}
501508

502509
// FindMachine parses the netrc file identified by filename and returns the
503-
// Machine named by name. If a problem occurs parsing the file at filename, an
510+
// Machine named by name. It would also compare login name if it's provided.
511+
// If a problem occurs parsing the file at filename, an
504512
// error is returned. If a machine named by name exists, it is returned. If no
505513
// Machine with name name is found and there is a ``default'' machine, the
506514
// ``default'' machine is returned. Otherwise, nil is returned.
507-
func FindMachine(filename, name string) (m *Machine, err error) {
515+
func FindMachine(filename, name string, loginName string) (m *Machine, err error) {
508516
n, err := ParseFile(filename)
509517
if err != nil {
510518
return nil, err
511519
}
512-
return n.FindMachine(name), nil
520+
return n.FindMachine(name, loginName), nil
513521
}

netrc/netrc_test.go

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
var expectedMachines = []*Machine{
1818
&Machine{Name: "mail.google.com", Login: "[email protected]", Password: "somethingSecret", Account: "justagmail"},
1919
&Machine{Name: "ray", Login: "demo", Password: "mypassword", Account: ""},
20+
&Machine{Name: "ray", Login: "demo1", Password: "demo1_password", Account: ""},
2021
&Machine{Name: "weirdlogin", Login: "uname", Password: "pass#pass", Account: ""},
2122
&Machine{Name: "google.com", Login: "[email protected]", Password: "secure"},
2223
&Machine{Name: "", Login: "anonymous", Password: "[email protected]", Account: ""},
@@ -132,7 +133,7 @@ func TestParseFile(t *testing.T) {
132133
}
133134

134135
func TestFindMachine(t *testing.T) {
135-
m, err := FindMachine("examples/good.netrc", "ray")
136+
m, err := FindMachine("examples/good.netrc", "ray", "")
136137
if err != nil {
137138
t.Fatal(err)
138139
}
@@ -143,12 +144,34 @@ func TestFindMachine(t *testing.T) {
143144
t.Errorf("expected m.IsDefault() to be false")
144145
}
145146

146-
m, err = FindMachine("examples/good.netrc", "non.existent")
147+
m, err = FindMachine("examples/good.netrc", "ray", "demo1")
147148
if err != nil {
148149
t.Fatal(err)
149150
}
150-
if !eqMachine(m, expectedMachines[4]) {
151-
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[3], m)
151+
if !eqMachine(m, expectedMachines[2]) {
152+
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[2], m)
153+
}
154+
if m.IsDefault() {
155+
t.Errorf("expected m.IsDefault() to be false")
156+
}
157+
158+
m, err = FindMachine("examples/good.netrc", "ray", "non.existent")
159+
if err != nil {
160+
t.Fatal(err)
161+
}
162+
if !eqMachine(m, expectedMachines[5]) {
163+
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[5], m)
164+
}
165+
if !m.IsDefault() {
166+
t.Errorf("expected m.IsDefault() to be true")
167+
}
168+
169+
m, err = FindMachine("examples/good.netrc", "non.existent", "")
170+
if err != nil {
171+
t.Fatal(err)
172+
}
173+
if !eqMachine(m, expectedMachines[5]) {
174+
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[5], m)
152175
}
153176
if !m.IsDefault() {
154177
t.Errorf("expected m.IsDefault() to be true")
@@ -161,7 +184,7 @@ func TestNetrcFindMachine(t *testing.T) {
161184
t.Fatal(err)
162185
}
163186

164-
m := n.FindMachine("ray")
187+
m := n.FindMachine("ray", "")
165188
if !eqMachine(m, expectedMachines[1]) {
166189
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[1], m)
167190
}
@@ -170,7 +193,7 @@ func TestNetrcFindMachine(t *testing.T) {
170193
}
171194

172195
n = &Netrc{}
173-
m = n.FindMachine("nonexistent")
196+
m = n.FindMachine("nonexistent", "")
174197
if m != nil {
175198
t.Errorf("expected nil, got %v", m)
176199
}
@@ -197,7 +220,7 @@ func TestMarshalText(t *testing.T) {
197220
}
198221

199222
// make sure tokens w/ no value are not serialized
200-
m := n.FindMachine("mail.google.com")
223+
m := n.FindMachine("mail.google.com", "")
201224
m.UpdatePassword("")
202225
result, err = n.MarshalText()
203226
if err != nil {
@@ -359,26 +382,34 @@ func TestRemoveMachine(t *testing.T) {
359382
t.Fatal(err)
360383
}
361384

362-
tests := []string{"mail.google.com", "weirdlogin"}
385+
tests := []struct {
386+
name string
387+
login string
388+
}{
389+
{"mail.google.com", ""},
390+
{"weirdlogin", "uname"},
391+
}
363392

364-
for _, name := range tests {
393+
for _, test := range tests {
394+
name := test.name
395+
loginName := test.login
365396
mcount := len(n.machines)
366397
// sanity check
367-
m := n.FindMachine(name)
398+
m := n.FindMachine(name, loginName)
368399
if m == nil {
369400
t.Fatalf("machine %q not found", name)
370401
}
371402
if m.IsDefault() {
372403
t.Fatalf("expected machine %q, got default instead", name)
373404
}
374-
n.RemoveMachine(name)
405+
n.RemoveMachine(name, loginName)
375406

376407
if len(n.machines) != mcount-1 {
377408
t.Errorf("n.machines count expected %d, got %d", mcount-1, len(n.machines))
378409
}
379410

380411
// make sure Machine is no longer returned by FindMachine()
381-
if m2 := n.FindMachine(name); m2 != nil && !m2.IsDefault() {
412+
if m2 := n.FindMachine(name, loginName); m2 != nil && !m2.IsDefault() {
382413
t.Errorf("Machine %q not removed from Machines list", name)
383414
}
384415

@@ -429,7 +460,7 @@ func TestUpdateLogin(t *testing.T) {
429460
}
430461

431462
for _, test := range tests {
432-
m := n.FindMachine(test.name)
463+
m := n.FindMachine(test.name, "")
433464
if m.IsDefault() == test.exists {
434465
t.Errorf("expected machine %s to not exist, but it did", test.name)
435466
} else {
@@ -441,7 +472,7 @@ func TestUpdateLogin(t *testing.T) {
441472
continue
442473
}
443474
m.UpdateLogin(test.newlogin)
444-
m := n.FindMachine(test.name)
475+
m := n.FindMachine(test.name, "")
445476
if m.Login != test.newlogin {
446477
t.Errorf("expected new login %q, got %q", test.newlogin, m.Login)
447478
}
@@ -491,7 +522,7 @@ func TestUpdatePassword(t *testing.T) {
491522
}
492523

493524
for _, test := range tests {
494-
m := n.FindMachine(test.name)
525+
m := n.FindMachine(test.name, "")
495526
if m.IsDefault() == test.exists {
496527
t.Errorf("expected machine %s to not exist, but it did", test.name)
497528
} else {
@@ -503,7 +534,7 @@ func TestUpdatePassword(t *testing.T) {
503534
continue
504535
}
505536
m.UpdatePassword(test.newpassword)
506-
m = n.FindMachine(test.name)
537+
m = n.FindMachine(test.name, "")
507538
if m.Password != test.newpassword {
508539
t.Errorf("expected new password %q, got %q", test.newpassword, m.Password)
509540
}

0 commit comments

Comments
 (0)