Skip to content

Commit 6996b5c

Browse files
committed
add RemoveMachine
1 parent da1906f commit 6996b5c

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

netrc/examples/good.netrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# I am a comment
22
machine mail.google.com
33
4-
account gmail #end of line comment with trailing space
4+
account justagmail #end of line comment with trailing space
55
password somethingSecret
66
# I am another comment
77

netrc/netrc.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,35 @@ func (n *Netrc) insertMachineTokensBeforeDefault(m *Machine) {
149149
return
150150
}
151151

152+
func (n *Netrc) RemoveMachine(name string) {
153+
n.updateLock.Lock()
154+
defer n.updateLock.Unlock()
155+
156+
for i := range n.machines {
157+
if n.machines[i] != nil && n.machines[i].Name == name {
158+
m := n.machines[i]
159+
for _, t := range []*token{
160+
m.nametoken, m.logintoken, m.passtoken, m.accounttoken,
161+
} {
162+
n.removeToken(t)
163+
}
164+
n.machines = append(n.machines[:i], n.machines[i+1:]...)
165+
return
166+
}
167+
}
168+
}
169+
170+
func (n *Netrc) removeToken(t *token) {
171+
if t != nil {
172+
for i := range n.tokens {
173+
if n.tokens[i] == t {
174+
n.tokens = append(n.tokens[:i], n.tokens[i+1:]...)
175+
return
176+
}
177+
}
178+
}
179+
}
180+
152181
// Machine contains information about a remote machine.
153182
type Machine struct {
154183
Name string

netrc/netrc_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,57 @@ func TestNewMachineGoesBeforeDefault(t *testing.T) {
325325
}
326326
}
327327

328+
func TestRemoveMachine(t *testing.T) {
329+
n, err := ParseFile("examples/good.netrc")
330+
if err != nil {
331+
t.Fatal(err)
332+
}
333+
334+
tests := []string{"mail.google.com", "weirdlogin"}
335+
336+
for _, name := range tests {
337+
mcount := len(n.machines)
338+
// sanity check
339+
m := n.FindMachine(name)
340+
if m == nil {
341+
t.Fatalf("machine %q not found", name)
342+
}
343+
if m.IsDefault() {
344+
t.Fatalf("expected machine %q, got default instead", name)
345+
}
346+
n.RemoveMachine(name)
347+
348+
if len(n.machines) != mcount-1 {
349+
t.Errorf("n.machines count expected %d, got %d", mcount-1, len(n.machines))
350+
}
351+
352+
// make sure Machine is no longer returned by FindMachine()
353+
if m2 := n.FindMachine(name); m2 != nil && !m2.IsDefault() {
354+
t.Errorf("Machine %q not removed from Machines list", name)
355+
}
356+
357+
// make sure tokens are not present in tokens list
358+
for _, token := range []*token{m.nametoken, m.logintoken, m.passtoken, m.accounttoken} {
359+
if token != nil {
360+
for _, tok2 := range n.tokens {
361+
if tok2 == token {
362+
t.Errorf("token not removed from tokens list: %v", token)
363+
break
364+
}
365+
}
366+
}
367+
}
368+
369+
bodyb, _ := n.MarshalText()
370+
body := string(bodyb)
371+
for _, value := range []string{m.Name, m.Login, m.Password, m.Account} {
372+
if value != "" && strings.Contains(body, value) {
373+
t.Errorf("MarshalText() after RemoveMachine() contained unexpected %q", value)
374+
}
375+
}
376+
}
377+
}
378+
328379
func TestUpdateLogin(t *testing.T) {
329380
n, err := ParseFile("examples/good.netrc")
330381
if err != nil {

0 commit comments

Comments
 (0)