Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions netrc/examples/good.netrc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ put src2/*

machine ray login demo password mypassword

machine ray login demo1 password demo1_password

machine weirdlogin login uname password pass#pass

machine google.com
Expand Down
20 changes: 14 additions & 6 deletions netrc/netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ type Netrc struct {
}

// FindMachine returns the Machine in n named by name. If a machine named by
// name exists, it is returned. If no Machine with name name is found and there
// name exists, it is returned. It would also compare login name if it's provided.
// If no Machine with name name is found and there
// is a ``default'' machine, the ``default'' machine is returned. Otherwise, nil
// is returned.
func (n *Netrc) FindMachine(name string) (m *Machine) {
func (n *Netrc) FindMachine(name string, loginName string) (m *Machine) {
// TODO(bgentry): not safe for concurrency
var def *Machine
for _, m = range n.machines {
if m.Name == name {
if loginName != "" && m.Login != loginName {
continue
}
return m
}
if m.IsDefault() {
Expand Down Expand Up @@ -160,12 +164,15 @@ func (n *Netrc) insertMachineTokensBeforeDefault(m *Machine) {
return
}

func (n *Netrc) RemoveMachine(name string) {
func (n *Netrc) RemoveMachine(name string, loginName string) {
n.updateLock.Lock()
defer n.updateLock.Unlock()

for i := range n.machines {
if n.machines[i] != nil && n.machines[i].Name == name {
if loginName != "" && n.machines[i].Login != loginName {
continue
}
m := n.machines[i]
for _, t := range []*token{
m.nametoken, m.logintoken, m.passtoken, m.accounttoken,
Expand Down Expand Up @@ -500,14 +507,15 @@ func Parse(r io.Reader) (*Netrc, error) {
}

// FindMachine parses the netrc file identified by filename and returns the
// Machine named by name. If a problem occurs parsing the file at filename, an
// Machine named by name. It would also compare login name if it's provided.
// If a problem occurs parsing the file at filename, an
// error is returned. If a machine named by name exists, it is returned. If no
// Machine with name name is found and there is a ``default'' machine, the
// ``default'' machine is returned. Otherwise, nil is returned.
func FindMachine(filename, name string) (m *Machine, err error) {
func FindMachine(filename, name string, loginName string) (m *Machine, err error) {
n, err := ParseFile(filename)
if err != nil {
return nil, err
}
return n.FindMachine(name), nil
return n.FindMachine(name, loginName), nil
}
63 changes: 47 additions & 16 deletions netrc/netrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
var expectedMachines = []*Machine{
&Machine{Name: "mail.google.com", Login: "joe@gmail.com", Password: "somethingSecret", Account: "justagmail"},
&Machine{Name: "ray", Login: "demo", Password: "mypassword", Account: ""},
&Machine{Name: "ray", Login: "demo1", Password: "demo1_password", Account: ""},
&Machine{Name: "weirdlogin", Login: "uname", Password: "pass#pass", Account: ""},
&Machine{Name: "google.com", Login: "alice@google.com", Password: "secure"},
&Machine{Name: "", Login: "anonymous", Password: "joe@example.com", Account: ""},
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestParseFile(t *testing.T) {
}

func TestFindMachine(t *testing.T) {
m, err := FindMachine("examples/good.netrc", "ray")
m, err := FindMachine("examples/good.netrc", "ray", "")
if err != nil {
t.Fatal(err)
}
Expand All @@ -143,12 +144,34 @@ func TestFindMachine(t *testing.T) {
t.Errorf("expected m.IsDefault() to be false")
}

m, err = FindMachine("examples/good.netrc", "non.existent")
m, err = FindMachine("examples/good.netrc", "ray", "demo1")
if err != nil {
t.Fatal(err)
}
if !eqMachine(m, expectedMachines[4]) {
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[3], m)
if !eqMachine(m, expectedMachines[2]) {
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[2], m)
}
if m.IsDefault() {
t.Errorf("expected m.IsDefault() to be false")
}

m, err = FindMachine("examples/good.netrc", "ray", "non.existent")
if err != nil {
t.Fatal(err)
}
if !eqMachine(m, expectedMachines[5]) {
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[5], m)
}
if !m.IsDefault() {
t.Errorf("expected m.IsDefault() to be true")
}

m, err = FindMachine("examples/good.netrc", "non.existent", "")
if err != nil {
t.Fatal(err)
}
if !eqMachine(m, expectedMachines[5]) {
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[5], m)
}
if !m.IsDefault() {
t.Errorf("expected m.IsDefault() to be true")
Expand All @@ -161,7 +184,7 @@ func TestNetrcFindMachine(t *testing.T) {
t.Fatal(err)
}

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

n = &Netrc{}
m = n.FindMachine("nonexistent")
m = n.FindMachine("nonexistent", "")
if m != nil {
t.Errorf("expected nil, got %v", m)
}
Expand All @@ -197,7 +220,7 @@ func TestMarshalText(t *testing.T) {
}

// make sure tokens w/ no value are not serialized
m := n.FindMachine("mail.google.com")
m := n.FindMachine("mail.google.com", "")
m.UpdatePassword("")
result, err = n.MarshalText()
if err != nil {
Expand Down Expand Up @@ -359,26 +382,34 @@ func TestRemoveMachine(t *testing.T) {
t.Fatal(err)
}

tests := []string{"mail.google.com", "weirdlogin"}
tests := []struct {
name string
login string
}{
{"mail.google.com", ""},
{"weirdlogin", "uname"},
}

for _, name := range tests {
for _, test := range tests {
name := test.name
loginName := test.login
mcount := len(n.machines)
// sanity check
m := n.FindMachine(name)
m := n.FindMachine(name, loginName)
if m == nil {
t.Fatalf("machine %q not found", name)
}
if m.IsDefault() {
t.Fatalf("expected machine %q, got default instead", name)
}
n.RemoveMachine(name)
n.RemoveMachine(name, loginName)

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

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

Expand Down Expand Up @@ -429,7 +460,7 @@ func TestUpdateLogin(t *testing.T) {
}

for _, test := range tests {
m := n.FindMachine(test.name)
m := n.FindMachine(test.name, "")
if m.IsDefault() == test.exists {
t.Errorf("expected machine %s to not exist, but it did", test.name)
} else {
Expand All @@ -441,7 +472,7 @@ func TestUpdateLogin(t *testing.T) {
continue
}
m.UpdateLogin(test.newlogin)
m := n.FindMachine(test.name)
m := n.FindMachine(test.name, "")
if m.Login != test.newlogin {
t.Errorf("expected new login %q, got %q", test.newlogin, m.Login)
}
Expand Down Expand Up @@ -491,7 +522,7 @@ func TestUpdatePassword(t *testing.T) {
}

for _, test := range tests {
m := n.FindMachine(test.name)
m := n.FindMachine(test.name, "")
if m.IsDefault() == test.exists {
t.Errorf("expected machine %s to not exist, but it did", test.name)
} else {
Expand All @@ -503,7 +534,7 @@ func TestUpdatePassword(t *testing.T) {
continue
}
m.UpdatePassword(test.newpassword)
m = n.FindMachine(test.name)
m = n.FindMachine(test.name, "")
if m.Password != test.newpassword {
t.Errorf("expected new password %q, got %q", test.newpassword, m.Password)
}
Expand Down