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