newNetDev creates a new NetDev from the contents of the given file.
(file string)
| 59 | |
| 60 | // newNetDev creates a new NetDev from the contents of the given file. |
| 61 | func newNetDev(file string) (NetDev, error) { |
| 62 | f, err := os.Open(file) |
| 63 | if err != nil { |
| 64 | return NetDev{}, err |
| 65 | } |
| 66 | defer f.Close() |
| 67 | |
| 68 | netDev := NetDev{} |
| 69 | s := bufio.NewScanner(f) |
| 70 | for n := 0; s.Scan(); n++ { |
| 71 | // Skip the 2 header lines. |
| 72 | if n < 2 { |
| 73 | continue |
| 74 | } |
| 75 | |
| 76 | line, err := netDev.parseLine(s.Text()) |
| 77 | if err != nil { |
| 78 | return netDev, err |
| 79 | } |
| 80 | |
| 81 | netDev[line.Name] = *line |
| 82 | } |
| 83 | |
| 84 | return netDev, s.Err() |
| 85 | } |
| 86 | |
| 87 | // parseLine parses a single line from the /proc/net/dev file. Header lines |
| 88 | // must be filtered prior to calling this method. |