-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnetrc.go
51 lines (49 loc) · 999 Bytes
/
netrc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"bufio"
"bytes"
"io"
)
func netrc(r io.Reader, machine string) (username, password string, err error) {
scanner := bufio.NewScanner(r)
scanner.Split(func(b []byte, eof bool) (int, []byte, error) {
skip := 0
for {
n, w, err := bufio.ScanWords(b, eof)
if err != nil || len(w) == 0 || w[0] != '#' {
return skip + n, w, err
}
i := bytes.IndexByte(b[n:], '\n')
if i < 0 {
return len(b), nil, nil
}
skip = skip + n + i
b = b[n+i:]
}
})
foundMachine := false
for scanner.Scan() {
if foundMachine {
switch scanner.Text() {
case "login":
if scanner.Scan() {
username = scanner.Text()
}
case "password":
if scanner.Scan() {
password = scanner.Text()
}
}
}
switch scanner.Text() {
case "machine":
foundMachine = false
if scanner.Scan() && scanner.Text() == machine {
foundMachine = true
}
case "default":
foundMachine = true
}
}
return username, password, scanner.Err()
}