2014-09-23 08:23:04 -06:00
|
|
|
// The gorename command performs precise type-safe renaming of
|
2014-12-30 08:48:23 -07:00
|
|
|
// identifiers in Go source code.
|
|
|
|
//
|
|
|
|
// Run with -help for usage information, or view the Usage constant in
|
|
|
|
// package golang.org/x/tools/refactor/rename, which contains most of
|
|
|
|
// the implementation.
|
|
|
|
//
|
2014-12-08 21:00:58 -07:00
|
|
|
package main // import "golang.org/x/tools/cmd/gorename"
|
2014-09-23 08:23:04 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"go/build"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
2014-11-09 14:50:40 -07:00
|
|
|
"golang.org/x/tools/refactor/rename"
|
2014-09-23 08:23:04 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
offsetFlag = flag.String("offset", "", "file and byte offset of identifier to be renamed, e.g. 'file.go:#123'. For use by editors.")
|
|
|
|
fromFlag = flag.String("from", "", "identifier to be renamed; see -help for formats")
|
|
|
|
toFlag = flag.String("to", "", "new name for identifier")
|
|
|
|
helpFlag = flag.Bool("help", false, "show usage message")
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.BoolVar(&rename.Force, "force", false, "proceed, even if conflicts were reported")
|
|
|
|
flag.BoolVar(&rename.DryRun, "dryrun", false, "show the change, but do not apply it")
|
|
|
|
flag.BoolVar(&rename.Verbose, "v", false, "print verbose information")
|
|
|
|
|
|
|
|
// If $GOMAXPROCS isn't set, use the full capacity of the machine.
|
|
|
|
// For small machines, use at least 4 threads.
|
|
|
|
if os.Getenv("GOMAXPROCS") == "" {
|
|
|
|
n := runtime.NumCPU()
|
|
|
|
if n < 4 {
|
|
|
|
n = 4
|
|
|
|
}
|
|
|
|
runtime.GOMAXPROCS(n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
if len(flag.Args()) > 0 {
|
2014-11-17 10:58:28 -07:00
|
|
|
fmt.Fprintln(os.Stderr, "gorename: surplus arguments.")
|
2014-09-23 08:23:04 -06:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if *helpFlag || (*offsetFlag == "" && *fromFlag == "" && *toFlag == "") {
|
2014-12-30 08:48:23 -07:00
|
|
|
fmt.Println(rename.Usage)
|
2014-09-23 08:23:04 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rename.Main(&build.Default, *offsetFlag, *fromFlag, *toFlag); err != nil {
|
|
|
|
if err != rename.ConflictError {
|
2014-11-17 10:58:28 -07:00
|
|
|
fmt.Fprintf(os.Stderr, "gorename: %s\n", err)
|
2014-09-23 08:23:04 -06:00
|
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|