1// The gorename command performs precise type-safe renaming of 2// identifiers in Go source code. 3// 4// Run with -help for usage information, or view the Usage constant in 5// package golang.org/x/tools/refactor/rename, which contains most of 6// the implementation. 7// 8package main // import "golang.org/x/tools/cmd/gorename" 9 10import ( 11 "flag" 12 "fmt" 13 "go/build" 14 "log" 15 "os" 16 17 "golang.org/x/tools/go/buildutil" 18 "golang.org/x/tools/refactor/rename" 19) 20 21var ( 22 offsetFlag = flag.String("offset", "", "file and byte offset of identifier to be renamed, e.g. 'file.go:#123'. For use by editors.") 23 fromFlag = flag.String("from", "", "identifier to be renamed; see -help for formats") 24 toFlag = flag.String("to", "", "new name for identifier") 25 helpFlag = flag.Bool("help", false, "show usage message") 26) 27 28func init() { 29 flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc) 30 flag.BoolVar(&rename.Force, "force", false, "proceed, even if conflicts were reported") 31 flag.BoolVar(&rename.Verbose, "v", false, "print verbose information") 32 flag.BoolVar(&rename.Diff, "d", false, "display diffs instead of rewriting files") 33 flag.StringVar(&rename.DiffCmd, "diffcmd", "diff", "diff command invoked when using -d") 34} 35 36func main() { 37 log.SetPrefix("gorename: ") 38 log.SetFlags(0) 39 flag.Parse() 40 if len(flag.Args()) > 0 { 41 log.Fatal("surplus arguments") 42 } 43 44 if *helpFlag || (*offsetFlag == "" && *fromFlag == "" && *toFlag == "") { 45 fmt.Println(rename.Usage) 46 return 47 } 48 49 if err := rename.Main(&build.Default, *offsetFlag, *fromFlag, *toFlag); err != nil { 50 if err != rename.ConflictError { 51 log.Fatal(err) 52 } 53 os.Exit(1) 54 } 55} 56