mirror of
https://github.com/golang/go
synced 2024-11-18 14:24:44 -07:00
c1ef9c75bf
Fixes golang/go#9469 Change-Id: I74e19e3d095e4c2ab62f755a408f58fd5e7ac2f2 Reviewed-on: https://go-review.googlesource.com/2176 Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
// The gorename command performs precise type-safe renaming of
|
|
// 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.
|
|
//
|
|
package main // import "golang.org/x/tools/cmd/gorename"
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"go/build"
|
|
"os"
|
|
"runtime"
|
|
|
|
"golang.org/x/tools/refactor/rename"
|
|
)
|
|
|
|
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 {
|
|
fmt.Fprintln(os.Stderr, "gorename: surplus arguments.")
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *helpFlag || (*offsetFlag == "" && *fromFlag == "" && *toFlag == "") {
|
|
fmt.Println(rename.Usage)
|
|
return
|
|
}
|
|
|
|
if err := rename.Main(&build.Default, *offsetFlag, *fromFlag, *toFlag); err != nil {
|
|
if err != rename.ConflictError {
|
|
fmt.Fprintf(os.Stderr, "gorename: %s\n", err)
|
|
}
|
|
os.Exit(1)
|
|
}
|
|
}
|