3 Star 0 Fork 0

mirrors_shirou/gopsutil_1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
v3migration.go 2.27 KB
一键复制 编辑 原始数据 按行查看 历史
shirou 提交于 2020-10-20 17:59 . rename tools to _tools
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()
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors_shirou/gopsutil_1.git
git@gitee.com:mirrors_shirou/gopsutil_1.git
mirrors_shirou
gopsutil_1
gopsutil_1
v3.21.9

搜索帮助