github.com/jonsyu1/godel@v0.0.0-20171017211503-64567a0cf169/apps/okgo/checks/definitions.go (about)

     1  // Copyright 2016 Palantir Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package checks
    16  
    17  import (
    18  	"sort"
    19  	"strings"
    20  
    21  	"github.com/palantir/amalgomate/amalgomated"
    22  	"github.com/palantir/pkg/pkgpath"
    23  	"github.com/pkg/errors"
    24  
    25  	"github.com/palantir/godel/apps/okgo/checkoutput"
    26  	"github.com/palantir/godel/apps/okgo/cmd/cmdlib"
    27  )
    28  
    29  type byName []Checker
    30  
    31  func (a byName) Len() int           { return len(a) }
    32  func (a byName) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
    33  func (a byName) Less(i, j int) bool { return a[i].Cmd().Name() < a[j].Cmd().Name() }
    34  
    35  type allArgType string
    36  
    37  func GetChecker(cmd amalgomated.Cmd) (Checker, error) {
    38  	for _, checker := range checkers() {
    39  		if cmd == checker.Cmd() {
    40  			return checker, nil
    41  		}
    42  	}
    43  	return nil, errors.Errorf("no checker exists for %s", cmd.Name())
    44  }
    45  
    46  func checkers() []Checker {
    47  	checkers := []Checker{
    48  		&checkerDefinition{
    49  			cmd:        cmdlib.Instance().MustNewCmd("compiles"),
    50  			lineParser: checkoutput.MultiLineParser(pkgpath.Absolute),
    51  			rawLineFilter: func(line string) bool {
    52  				return strings.HasPrefix(line, "err: couldn't load packages due to errors:") || strings.HasPrefix(line, "-: ")
    53  			},
    54  		},
    55  		&checkerDefinition{
    56  			cmd:        cmdlib.Instance().MustNewCmd("deadcode"),
    57  			lineParser: checkoutput.StartAfterFirstWhitespaceParser(pkgpath.Relative),
    58  		},
    59  		&checkerDefinition{
    60  			cmd:        cmdlib.Instance().MustNewCmd("errcheck"),
    61  			lineParser: checkoutput.DefaultParser(pkgpath.Relative),
    62  		},
    63  		&checkerDefinition{
    64  			cmd:        cmdlib.Instance().MustNewCmd("extimport"),
    65  			lineParser: checkoutput.DefaultParser(pkgpath.Absolute),
    66  		},
    67  		&checkerDefinition{
    68  			cmd:        cmdlib.Instance().MustNewCmd("golint"),
    69  			lineParser: checkoutput.DefaultParser(pkgpath.Relative),
    70  			allArg:     splat,
    71  		},
    72  		&checkerDefinition{
    73  			cmd:        cmdlib.Instance().MustNewCmd("govet"),
    74  			lineParser: checkoutput.DefaultParser(pkgpath.Relative),
    75  			rawLineFilter: func(line string) bool {
    76  				return line == "exit status 1"
    77  			},
    78  		},
    79  		&checkerDefinition{
    80  			cmd:        cmdlib.Instance().MustNewCmd("importalias"),
    81  			lineParser: checkoutput.DefaultParser(pkgpath.Relative),
    82  		},
    83  		&checkerDefinition{
    84  			cmd:         cmdlib.Instance().MustNewCmd("ineffassign"),
    85  			lineParser:  checkoutput.DefaultParser(pkgpath.Absolute),
    86  			allArg:      rootDir,
    87  			globalCheck: true,
    88  		},
    89  		&checkerDefinition{
    90  			cmd:        cmdlib.Instance().MustNewCmd("nobadfuncs"),
    91  			lineParser: checkoutput.DefaultParser(pkgpath.Absolute),
    92  		},
    93  		&checkerDefinition{
    94  			cmd:        cmdlib.Instance().MustNewCmd("novendor"),
    95  			lineParser: checkoutput.RawParser(),
    96  		},
    97  		&checkerDefinition{
    98  			cmd:        cmdlib.Instance().MustNewCmd("outparamcheck"),
    99  			lineParser: checkoutput.DefaultParser(pkgpath.Relative),
   100  			rawLineFilter: func(line string) bool {
   101  				return strings.HasSuffix(line, `the parameters listed above require the use of '&', for example f(&x) instead of f(x)`)
   102  			},
   103  		},
   104  		&checkerDefinition{
   105  			cmd:        cmdlib.Instance().MustNewCmd("unconvert"),
   106  			lineParser: checkoutput.DefaultParser(pkgpath.Absolute),
   107  		},
   108  		&checkerDefinition{
   109  			cmd:        cmdlib.Instance().MustNewCmd("varcheck"),
   110  			lineParser: checkoutput.StartAfterFirstWhitespaceParser(pkgpath.Absolute),
   111  		},
   112  	}
   113  	sort.Sort(byName(checkers))
   114  	return checkers
   115  }