...

Source file src/cmd/go/internal/fix/fix.go

Documentation: cmd/go/internal/fix

     1  // Copyright 2011 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  // Package fix implements the “go fix” command.
     6  package fix
     7  
     8  import (
     9  	"cmd/go/internal/base"
    10  	"cmd/go/internal/cfg"
    11  	"cmd/go/internal/load"
    12  	"cmd/go/internal/modload"
    13  	"cmd/go/internal/str"
    14  	"cmd/go/internal/work"
    15  	"context"
    16  	"fmt"
    17  	"go/build"
    18  	"os"
    19  )
    20  
    21  var CmdFix = &base.Command{
    22  	UsageLine: "go fix [-fix list] [packages]",
    23  	Short:     "update packages to use new APIs",
    24  	Long: `
    25  Fix runs the Go fix command on the packages named by the import paths.
    26  
    27  The -fix flag sets a comma-separated list of fixes to run.
    28  The default is all known fixes.
    29  (Its value is passed to 'go tool fix -r'.)
    30  
    31  For more about fix, see 'go doc cmd/fix'.
    32  For more about specifying packages, see 'go help packages'.
    33  
    34  To run fix with other options, run 'go tool fix'.
    35  
    36  See also: go fmt, go vet.
    37  	`,
    38  }
    39  
    40  var fixes = CmdFix.Flag.String("fix", "", "comma-separated list of fixes to apply")
    41  
    42  func init() {
    43  	work.AddBuildFlags(CmdFix, work.DefaultBuildFlags)
    44  	CmdFix.Run = runFix // fix cycle
    45  }
    46  
    47  func runFix(ctx context.Context, cmd *base.Command, args []string) {
    48  	pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{}, args)
    49  	w := 0
    50  	for _, pkg := range pkgs {
    51  		if pkg.Error != nil {
    52  			base.Errorf("%v", pkg.Error)
    53  			continue
    54  		}
    55  		pkgs[w] = pkg
    56  		w++
    57  	}
    58  	pkgs = pkgs[:w]
    59  
    60  	printed := false
    61  	for _, pkg := range pkgs {
    62  		if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
    63  			if !printed {
    64  				fmt.Fprintf(os.Stderr, "go: not fixing packages in dependency modules\n")
    65  				printed = true
    66  			}
    67  			continue
    68  		}
    69  		// Use pkg.gofiles instead of pkg.Dir so that
    70  		// the command only applies to this package,
    71  		// not to packages in subdirectories.
    72  		files := base.RelPaths(pkg.InternalAllGoFiles())
    73  		goVersion := ""
    74  		if pkg.Module != nil {
    75  			goVersion = "go" + pkg.Module.GoVersion
    76  		} else if pkg.Standard {
    77  			goVersion = build.Default.ReleaseTags[len(build.Default.ReleaseTags)-1]
    78  		}
    79  		var fixArg []string
    80  		if *fixes != "" {
    81  			fixArg = []string{"-r=" + *fixes}
    82  		}
    83  		base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), "-go="+goVersion, fixArg, files))
    84  	}
    85  }
    86  

View as plain text