github.com/sirkon/goproxy@v1.4.8/internal/vet/vet.go (about)

     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 vet implements the ``go vet'' command.
     6  package vet
     7  
     8  import (
     9  	"github.com/sirkon/goproxy/internal/base"
    10  	"github.com/sirkon/goproxy/internal/load"
    11  	"github.com/sirkon/goproxy/internal/modload"
    12  	"github.com/sirkon/goproxy/internal/work"
    13  	"path/filepath"
    14  )
    15  
    16  var CmdVet = &base.Command{
    17  	Run:         runVet,
    18  	CustomFlags: true,
    19  	UsageLine:   "go vet [-n] [-x] [build flags] [vet flags] [packages]",
    20  	Short:       "report likely mistakes in packages",
    21  	Long: `
    22  Vet runs the Go vet command on the packages named by the import paths.
    23  
    24  For more about vet and its flags, see 'go doc cmd/vet'.
    25  For more about specifying packages, see 'go help packages'.
    26  
    27  The -n flag prints commands that would be executed.
    28  The -x flag prints commands as they are executed.
    29  
    30  The build flags supported by go vet are those that control package resolution
    31  and execution, such as -n, -x, -v, -tags, and -toolexec.
    32  For more about these flags, see 'go help build'.
    33  
    34  See also: go fmt, go fix.
    35  	`,
    36  }
    37  
    38  func runVet(cmd *base.Command, args []string) {
    39  	modload.LoadTests = true
    40  
    41  	vetFlags, pkgArgs := vetFlags(args)
    42  
    43  	work.BuildInit()
    44  	work.VetFlags = vetFlags
    45  	if vetTool != "" {
    46  		var err error
    47  		work.VetTool, err = filepath.Abs(vetTool)
    48  		if err != nil {
    49  			base.Fatalf("%v", err)
    50  		}
    51  	}
    52  
    53  	pkgs := load.PackagesForBuild(pkgArgs)
    54  	if len(pkgs) == 0 {
    55  		base.Fatalf("no packages to vet")
    56  	}
    57  
    58  	var b work.Builder
    59  	b.Init()
    60  
    61  	root := &work.Action{Mode: "go vet"}
    62  	for _, p := range pkgs {
    63  		_, ptest, pxtest, err := load.TestPackagesFor(p, nil)
    64  		if err != nil {
    65  			base.Errorf("%v", err)
    66  			continue
    67  		}
    68  		if len(ptest.GoFiles) == 0 && len(ptest.CgoFiles) == 0 && pxtest == nil {
    69  			base.Errorf("go vet %s: no Go files in %s", p.ImportPath, p.Dir)
    70  			continue
    71  		}
    72  		if len(ptest.GoFiles) > 0 || len(ptest.CgoFiles) > 0 {
    73  			root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, ptest))
    74  		}
    75  		if pxtest != nil {
    76  			root.Deps = append(root.Deps, b.VetAction(work.ModeBuild, work.ModeBuild, pxtest))
    77  		}
    78  	}
    79  	b.Do(root)
    80  }