Black Lives Matter. Support the Equal Justice Initiative.

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  	"context"
    15  	"fmt"
    16  	"os"
    17  )
    18  
    19  var CmdFix = &base.Command{
    20  	Run:       runFix,
    21  	UsageLine: "go fix [packages]",
    22  	Short:     "update packages to use new APIs",
    23  	Long: `
    24  Fix runs the Go fix command on the packages named by the import paths.
    25  
    26  For more about fix, see 'go doc cmd/fix'.
    27  For more about specifying packages, see 'go help packages'.
    28  
    29  To run fix with specific options, run 'go tool fix'.
    30  
    31  See also: go fmt, go vet.
    32  	`,
    33  }
    34  
    35  func runFix(ctx context.Context, cmd *base.Command, args []string) {
    36  	pkgs := load.PackagesAndErrors(ctx, args)
    37  	w := 0
    38  	for _, pkg := range pkgs {
    39  		if pkg.Error != nil {
    40  			base.Errorf("%v", pkg.Error)
    41  			continue
    42  		}
    43  		pkgs[w] = pkg
    44  		w++
    45  	}
    46  	pkgs = pkgs[:w]
    47  
    48  	printed := false
    49  	for _, pkg := range pkgs {
    50  		if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
    51  			if !printed {
    52  				fmt.Fprintf(os.Stderr, "go: not fixing packages in dependency modules\n")
    53  				printed = true
    54  			}
    55  			continue
    56  		}
    57  		// Use pkg.gofiles instead of pkg.Dir so that
    58  		// the command only applies to this package,
    59  		// not to packages in subdirectories.
    60  		files := base.RelPaths(pkg.InternalAllGoFiles())
    61  		base.Run(str.StringList(cfg.BuildToolexec, base.Tool("fix"), files))
    62  	}
    63  }
    64  

View as plain text