Skip to content

Export Machines member of Netrc #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 14 additions & 14 deletions netrc/netrc.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var keywords = map[string]tkType{

type Netrc struct {
tokens []*token
machines []*Machine
Machines []*Machine
macros Macros
updateLock sync.Mutex
}
Expand All @@ -50,7 +50,7 @@ type Netrc struct {
func (n *Netrc) FindMachine(name string) (m *Machine) {
// TODO(bgentry): not safe for concurrency
var def *Machine
for _, m = range n.machines {
for _, m = range n.Machines {
if m.Name == name {
return m
}
Expand Down Expand Up @@ -126,13 +126,13 @@ func (n *Netrc) NewMachine(name, login, password, account string) *Machine {
},
}
n.insertMachineTokensBeforeDefault(m)
for i := range n.machines {
if n.machines[i].IsDefault() {
n.machines = append(append(n.machines[:i], m), n.machines[i:]...)
for i := range n.Machines {
if n.Machines[i].IsDefault() {
n.Machines = append(append(n.Machines[:i], m), n.Machines[i:]...)
return m
}
}
n.machines = append(n.machines, m)
n.Machines = append(n.Machines, m)
return m
}

Expand Down Expand Up @@ -163,15 +163,15 @@ func (n *Netrc) RemoveMachine(name string) {
n.updateLock.Lock()
defer n.updateLock.Unlock()

for i := range n.machines {
if n.machines[i] != nil && n.machines[i].Name == name {
m := n.machines[i]
for i := range n.Machines {
if n.Machines[i] != nil && n.Machines[i].Name == name {
m := n.Machines[i]
for _, t := range []*token{
m.nametoken, m.logintoken, m.passtoken, m.accounttoken,
} {
n.removeToken(t)
}
n.machines = append(n.machines[:i], n.machines[i+1:]...)
n.Machines = append(n.Machines[:i], n.Machines[i+1:]...)
return
}
}
Expand Down Expand Up @@ -373,7 +373,7 @@ func parse(r io.Reader, pos int) (*Netrc, error) {
return nil, err
}

nrc := Netrc{machines: make([]*Machine, 0, 20), macros: make(Macros, 10)}
nrc := Netrc{Machines: make([]*Machine, 0, 20), macros: make(Macros, 10)}

defaultSeen := false
var currentMacro *token
Expand Down Expand Up @@ -415,7 +415,7 @@ func parse(r io.Reader, pos int) (*Netrc, error) {
return nil, &Error{pos, "multiple default token"}
}
if m != nil {
nrc.machines, m = append(nrc.machines, m), nil
nrc.Machines, m = append(nrc.Machines, m), nil
}
m = new(Machine)
m.Name = ""
Expand All @@ -425,7 +425,7 @@ func parse(r io.Reader, pos int) (*Netrc, error) {
return nil, &Error{pos, errBadDefaultOrder}
}
if m != nil {
nrc.machines, m = append(nrc.machines, m), nil
nrc.Machines, m = append(nrc.Machines, m), nil
}
m = new(Machine)
if t.rawvalue, m.Name, pos, err = scanValue(scanner, pos); err != nil {
Expand Down Expand Up @@ -470,7 +470,7 @@ func parse(r io.Reader, pos int) (*Netrc, error) {
}

if m != nil {
nrc.machines, m = append(nrc.machines, m), nil
nrc.Machines, m = append(nrc.Machines, m), nil
}
return &nrc, nil
}
Expand Down
22 changes: 11 additions & 11 deletions netrc/netrc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func eqMachine(a *Machine, b *Machine) bool {
}

func testExpected(n *Netrc, t *testing.T) {
if len(expectedMachines) != len(n.machines) {
t.Errorf("expected %d machines, got %d", len(expectedMachines), len(n.machines))
if len(expectedMachines) != len(n.Machines) {
t.Errorf("expected %d machines, got %d", len(expectedMachines), len(n.Machines))
} else {
for i, e := range expectedMachines {
if !eqMachine(e, n.machines[i]) {
t.Errorf("bad machine; expected %v, got %v\n", e, n.machines[i])
if !eqMachine(e, n.Machines[i]) {
t.Errorf("bad machine; expected %v, got %v\n", e, n.Machines[i])
}
}
}
Expand Down Expand Up @@ -269,7 +269,7 @@ func TestNewMachine(t *testing.T) {

func testNewMachine(t *testing.T, n *Netrc) {
for _, test := range newMachineTests {
mcount := len(n.machines)
mcount := len(n.Machines)
// sanity check
bodyb, _ := n.MarshalText()
body := string(bodyb)
Expand All @@ -290,8 +290,8 @@ func testNewMachine(t *testing.T, n *Netrc) {
t.Fatalf("NewMachine() returned nil")
}

if len(n.machines) != mcount+1 {
t.Errorf("n.machines count expected %d, got %d", mcount+1, len(n.machines))
if len(n.Machines) != mcount+1 {
t.Errorf("n.Machines count expected %d, got %d", mcount+1, len(n.Machines))
}
// check values
if m.Name != test.name {
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestNewMachineGoesBeforeDefault(t *testing.T) {
t.Fatal(err)
}
m := n.NewMachine("mymachine", "mylogin", "mypassword", "myaccount")
if m2 := n.machines[len(n.machines)-2]; m2 != m {
if m2 := n.Machines[len(n.Machines)-2]; m2 != m {
t.Errorf("expected machine %v, got %v", m, m2)
}
}
Expand All @@ -361,7 +361,7 @@ func TestRemoveMachine(t *testing.T) {
tests := []string{"mail.google.com", "weirdlogin"}

for _, name := range tests {
mcount := len(n.machines)
mcount := len(n.Machines)
// sanity check
m := n.FindMachine(name)
if m == nil {
Expand All @@ -372,8 +372,8 @@ func TestRemoveMachine(t *testing.T) {
}
n.RemoveMachine(name)

if len(n.machines) != mcount-1 {
t.Errorf("n.machines count expected %d, got %d", mcount-1, len(n.machines))
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()
Expand Down