Skip to content

Commit 5affb93

Browse files
committed
netrc.go: round-trip unknown keywords, tokens
1 parent 9fd32a8 commit 5affb93

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

netrc/examples/good.netrc

Lines changed: 7 additions & 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 justagmail #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

@@ -16,6 +16,12 @@ machine ray login demo password mypassword
1616

1717
machine weirdlogin login uname password pass#pass
1818

19+
machine google.com
20+
21+
not-a-keyword
22+
password secure
23+
also-not-a-keyword
24+
1925
default
2026
login anonymous
2127

netrc/netrc.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const (
2424
tkMacdef
2525
tkComment
2626
tkWhitespace
27+
tkUnknown
2728
)
2829

2930
var keywords = map[string]tkType{
@@ -70,7 +71,7 @@ func (n *Netrc) MarshalText() (text []byte, err error) {
7071
// TODO(bgentry): not safe for concurrency
7172
for i := range n.tokens {
7273
switch n.tokens[i].kind {
73-
case tkComment, tkDefault, tkWhitespace: // always append these types
74+
case tkComment, tkDefault, tkWhitespace, tkUnknown: // always append these types
7475
text = append(text, n.tokens[i].rawkind...)
7576
default:
7677
if n.tokens[i].value != "" { // skip empty-value tokens
@@ -391,9 +392,11 @@ func parse(r io.Reader, pos int) (*Netrc, error) {
391392
t, err = newToken(rawb)
392393
if err != nil {
393394
if currentMacro == nil {
394-
return nil, &Error{pos, err.Error()}
395+
t.kind = tkUnknown
396+
nrc.tokens = append(nrc.tokens, t)
397+
} else {
398+
currentMacro.rawvalue = append(currentMacro.rawvalue, rawb...)
395399
}
396-
currentMacro.rawvalue = append(currentMacro.rawvalue, rawb...)
397400
continue
398401
}
399402

netrc/netrc_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ 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: ""},
2020
&Machine{Name: "weirdlogin", Login: "uname", Password: "pass#pass", Account: ""},
21+
&Machine{Name: "google.com", Login: "[email protected]", Password: "secure"},
2122
&Machine{Name: "", Login: "anonymous", Password: "[email protected]", Account: ""},
2223
}
2324
var expectedMacros = Macros{
@@ -146,7 +147,7 @@ func TestFindMachine(t *testing.T) {
146147
if err != nil {
147148
t.Fatal(err)
148149
}
149-
if !eqMachine(m, expectedMachines[3]) {
150+
if !eqMachine(m, expectedMachines[4]) {
150151
t.Errorf("bad machine; expected %v, got %v\n", expectedMachines[3], m)
151152
}
152153
if !m.IsDefault() {

0 commit comments

Comments
 (0)