forked from mdlayher/ethtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mklinkmodes.go
86 lines (67 loc) · 1.71 KB
/
mklinkmodes.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//go:build ignore
// +build ignore
// mklinkmodes is a Go script which generates the linkModes lookup table using
// a ztypes_linux.go from x/sys as input.
package main
import (
"bufio"
"bytes"
"fmt"
"go/format"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
// Assume a local GOPATH checkout of x/sys.
f, err := os.Open(filepath.Join(os.Getenv("GOPATH"), "src/golang.org/x/sys/unix/ztypes_linux.go"))
if err != nil {
log.Fatalf("failed to open ztypes_linux.go: %v", err)
}
defer f.Close()
b := bytes.NewBuffer(nil)
pf(b, `//go:build linux
// +build linux
// Code generated by "go run mklinkmodes.go"; DO NOT EDIT.
package ethtool
import "golang.org/x/sys/unix"
var linkModes = [...]struct{
bit uint32
str string
}{`)
s := bufio.NewScanner(f)
for s.Scan() {
if !bytes.HasPrefix(s.Bytes(), []byte("\tETHTOOL_LINK_MODE")) {
continue
}
// Found a link mode, we only care about the name.
ss := strings.Fields(s.Text())
if len(ss) != 3 {
continue
}
// The constant and a stringified name like 1000baseT/Full is written
// for each line found.
bit := ss[0]
str := strings.TrimPrefix(ss[0], "ETHTOOL_LINK_MODE_")
str = strings.TrimSuffix(str, "_BIT")
str = strings.Replace(str, "_", "/", -1)
pf(b, `{bit: unix.%s, str: %q},`, bit, str)
}
pf(b, `}`)
if err := s.Err(); err != nil {
log.Fatalf("failed to scan: %v", err)
}
out, err := format.Source(b.Bytes())
if err != nil {
log.Fatalf("failed to format: %v", err)
}
if err := ioutil.WriteFile("linkmodes_linux.go", out, 0o644); err != nil {
log.Fatalf("failed to write output file: %v", err)
}
}
func pf(w io.Writer, format string, args ...interface{}) {
fmt.Fprintf(w, format+"\n", args...)
}