...

Source file src/github.com/shirou/gopsutil/_tools/v3migration/v3migration.go

Documentation: github.com/shirou/gopsutil/_tools/v3migration

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"go/ast"
     7  	"go/format"
     8  	"go/parser"
     9  	"go/token"
    10  	"log"
    11  	"os"
    12  
    13  	"golang.org/x/tools/go/ast/astutil"
    14  )
    15  
    16  // https://github.com/shirou/gopsutil/issues/429
    17  func issue429() error {
    18  	f := func(filename string) error {
    19  		fset := token.NewFileSet()
    20  		expr, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
    21  		if err != nil {
    22  			return err
    23  		}
    24  		n := astutil.Apply(expr, func(cr *astutil.Cursor) bool {
    25  			if cr.Name() == "Decls" {
    26  				switch n := cr.Node().(type) {
    27  				case *ast.FuncDecl:
    28  					if n.Name.Name == "NetIOCounters" || n.Name.Name == ("NetIOCountersWithContext") {
    29  						cr.Delete()
    30  					}
    31  				}
    32  			}
    33  			return true
    34  		}, nil)
    35  		return replace(filename, fset, n)
    36  	}
    37  
    38  	root := "process/"
    39  	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"}
    40  	for _, fname := range fnames {
    41  		if err := f(root + fname); err != nil {
    42  			log.Fatalln("run 429:", err)
    43  		}
    44  	}
    45  	return nil
    46  }
    47  
    48  func issueRemoveUnusedValue() error {
    49  	f := func(filename string) error {
    50  		fset := token.NewFileSet()
    51  		expr, err := parser.ParseFile(fset, filename, nil, parser.ParseComments)
    52  		if err != nil {
    53  			return err
    54  		}
    55  		n := astutil.Apply(expr, func(cr *astutil.Cursor) bool {
    56  			if cr.Name() == "Decls" {
    57  				switch n := cr.Node().(type) {
    58  				case *ast.GenDecl:
    59  					if n.Tok != token.TYPE {
    60  						break
    61  					}
    62  					ts := n.Specs[0].(*ast.TypeSpec)
    63  					if ts.Name.Name == "SystemProcessInformation" {
    64  						cr.Delete()
    65  					}
    66  				}
    67  			}
    68  			return true
    69  		}, nil)
    70  		return replace(filename, fset, n)
    71  	}
    72  
    73  	if err := f("process/process_windows.go"); err != nil {
    74  		log.Fatalln("run 429:", err)
    75  	}
    76  	return nil
    77  }
    78  
    79  func replace(filename string, fset *token.FileSet, n ast.Node) error {
    80  	if err := os.Remove(filename); err != nil {
    81  		return err
    82  	}
    83  	fp, err := os.Create(filename)
    84  	if err != nil {
    85  		return err
    86  	}
    87  	defer fp.Close()
    88  	if err := format.Node(fp, fset, n); err != nil {
    89  		return err
    90  	}
    91  	fp.WriteString("\n")
    92  	return nil
    93  }
    94  
    95  func main() {
    96  	flag.Parse()
    97  	for _, n := range flag.Args() {
    98  		fmt.Println("issue:" + n)
    99  		switch n {
   100  		case "429":
   101  			issue429()
   102  		case "issueRemoveUnusedValue":
   103  			issueRemoveUnusedValue()
   104  		}
   105  	}
   106  
   107  }
   108  

View as plain text