forked from shirou/gopsutil
/
v3migration.go
107 lines (98 loc) · 2.27 KB
/
v3migration.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import (
"flag"
"fmt"
"go/ast"
"go/format"
"go/parser"
"go/token"
"log"
"os"
"golang.org/x/tools/go/ast/astutil"
)
// https://github.com/shirou/gopsutil/issues/429
func issue429() error {
f := func(filename string) error {
fset := token.NewFileSet()
expr, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return err
}
n := astutil.Apply(expr, func(cr *astutil.Cursor) bool {
if cr.Name() == "Decls" {
switch n := cr.Node().(type) {
case *ast.FuncDecl:
if n.Name.Name == "NetIOCounters" || n.Name.Name == ("NetIOCountersWithContext") {
cr.Delete()
}
}
}
return true
}, nil)
return replace(filename, fset, n)
}
root := "process/"
fnames := []string{"process.go", "process_darwin.go", "process_fallback.go", "process_freebsd.go", "process_linux.go", "process_openbsd.go", "process_bsd.go", "process_posix.go", "process_windows.go", "process_test.go"}
for _, fname := range fnames {
if err := f(root + fname); err != nil {
log.Fatalln("run 429:", err)
}
}
return nil
}
func issueRemoveUnusedValue() error {
f := func(filename string) error {
fset := token.NewFileSet()
expr, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
if err != nil {
return err
}
n := astutil.Apply(expr, func(cr *astutil.Cursor) bool {
if cr.Name() == "Decls" {
switch n := cr.Node().(type) {
case *ast.GenDecl:
if n.Tok != token.TYPE {
break
}
ts := n.Specs[0].(*ast.TypeSpec)
if ts.Name.Name == "SystemProcessInformation" {
cr.Delete()
}
}
}
return true
}, nil)
return replace(filename, fset, n)
}
if err := f("process/process_windows.go"); err != nil {
log.Fatalln("run 429:", err)
}
return nil
}
func replace(filename string, fset *token.FileSet, n ast.Node) error {
if err := os.Remove(filename); err != nil {
return err
}
fp, err := os.Create(filename)
if err != nil {
return err
}
defer fp.Close()
if err := format.Node(fp, fset, n); err != nil {
return err
}
fp.WriteString("\n")
return nil
}
func main() {
flag.Parse()
for _, n := range flag.Args() {
fmt.Println("issue:" + n)
switch n {
case "429":
issue429()
case "issueRemoveUnusedValue":
issueRemoveUnusedValue()
}
}
}