github.com/cockroachdb/tools@v0.0.0-20230222021103-a6d27438930d/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  package main // import "golang.org/x/tools/cmd/gorename"
    12  
    13  import (
    14  	"flag"
    15  	"fmt"
    16  	"go/build"
    17  	"log"
    18  	"os"
    19  
    20  	"golang.org/x/tools/go/buildutil"
    21  	"golang.org/x/tools/refactor/rename"
    22  )
    23  
    24  var (
    25  	offsetFlag = flag.String("offset", "", "file and byte offset of identifier to be renamed, e.g. 'file.go:#123'.  For use by editors.")
    26  	fromFlag   = flag.String("from", "", "identifier to be renamed; see -help for formats")
    27  	toFlag     = flag.String("to", "", "new name for identifier")
    28  	helpFlag   = flag.Bool("help", false, "show usage message")
    29  )
    30  
    31  func init() {
    32  	flag.Var((*buildutil.TagsFlag)(&build.Default.BuildTags), "tags", buildutil.TagsFlagDoc)
    33  	flag.BoolVar(&rename.Force, "force", false, "proceed, even if conflicts were reported")
    34  	flag.BoolVar(&rename.Verbose, "v", false, "print verbose information")
    35  	flag.BoolVar(&rename.Diff, "d", false, "display diffs instead of rewriting files")
    36  	flag.StringVar(&rename.DiffCmd, "diffcmd", "diff", "diff command invoked when using -d")
    37  }
    38  
    39  func main() {
    40  	log.SetPrefix("gorename: ")
    41  	log.SetFlags(0)
    42  	flag.Parse()
    43  	if len(flag.Args()) > 0 {
    44  		log.Fatal("surplus arguments")
    45  	}
    46  
    47  	if *helpFlag || (*offsetFlag == "" && *fromFlag == "" && *toFlag == "") {
    48  		fmt.Print(rename.Usage)
    49  		return
    50  	}
    51  
    52  	if err := rename.Main(&build.Default, *offsetFlag, *fromFlag, *toFlag); err != nil {
    53  		if err != rename.ConflictError {
    54  			log.Fatal(err)
    55  		}
    56  		os.Exit(1)
    57  	}
    58  }