github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/lint/golint/golint.go (about)

     1  // Copyright (c) 2013 The Go Authors. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file or at
     5  // https://developers.google.com/open-source/licenses/bsd.
     6  
     7  // golint lints the Go source files named on its command line.
     8  package main
     9  
    10  import (
    11  	"flag"
    12  	"fmt"
    13  	"go/build"
    14  	"io/ioutil"
    15  	"os"
    16  	"path/filepath"
    17  	"strings"
    18  
    19  	"github.com/insionng/yougam/libraries/golang/lint"
    20  )
    21  
    22  var minConfidence = flag.Float64("min_confidence", 0.8, "minimum confidence of a problem to print it")
    23  
    24  func usage() {
    25  	fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
    26  	fmt.Fprintf(os.Stderr, "\tgolint [flags] # runs on package in current directory\n")
    27  	fmt.Fprintf(os.Stderr, "\tgolint [flags] package\n")
    28  	fmt.Fprintf(os.Stderr, "\tgolint [flags] directory\n")
    29  	fmt.Fprintf(os.Stderr, "\tgolint [flags] files... # must be a single package\n")
    30  	fmt.Fprintf(os.Stderr, "Flags:\n")
    31  	flag.PrintDefaults()
    32  }
    33  
    34  func main() {
    35  	flag.Usage = usage
    36  	flag.Parse()
    37  
    38  	switch flag.NArg() {
    39  	case 0:
    40  		lintDir(".")
    41  	case 1:
    42  		arg := flag.Arg(0)
    43  		if strings.HasSuffix(arg, "/...") && isDir(arg[:len(arg)-4]) {
    44  			for _, dirname := range allPackagesInFS(arg) {
    45  				lintDir(dirname)
    46  			}
    47  		} else if isDir(arg) {
    48  			lintDir(arg)
    49  		} else if exists(arg) {
    50  			lintFiles(arg)
    51  		} else {
    52  			for _, pkgname := range importPaths([]string{arg}) {
    53  				lintPackage(pkgname)
    54  			}
    55  		}
    56  	default:
    57  		lintFiles(flag.Args()...)
    58  	}
    59  }
    60  
    61  func isDir(filename string) bool {
    62  	fi, err := os.Stat(filename)
    63  	return err == nil && fi.IsDir()
    64  }
    65  
    66  func exists(filename string) bool {
    67  	_, err := os.Stat(filename)
    68  	return err == nil
    69  }
    70  
    71  func lintFiles(filenames ...string) {
    72  	files := make(map[string][]byte)
    73  	for _, filename := range filenames {
    74  		src, err := ioutil.ReadFile(filename)
    75  		if err != nil {
    76  			fmt.Fprintln(os.Stderr, err)
    77  			continue
    78  		}
    79  		files[filename] = src
    80  	}
    81  
    82  	l := new(lint.Linter)
    83  	ps, err := l.LintFiles(files)
    84  	if err != nil {
    85  		fmt.Fprintf(os.Stderr, "%v\n", err)
    86  		return
    87  	}
    88  	for _, p := range ps {
    89  		if p.Confidence >= *minConfidence {
    90  			fmt.Printf("%v: %s\n", p.Position, p.Text)
    91  		}
    92  	}
    93  }
    94  
    95  func lintDir(dirname string) {
    96  	pkg, err := build.ImportDir(dirname, 0)
    97  	lintImportedPackage(pkg, err)
    98  }
    99  
   100  func lintPackage(pkgname string) {
   101  	pkg, err := build.Import(pkgname, ".", 0)
   102  	lintImportedPackage(pkg, err)
   103  }
   104  
   105  func lintImportedPackage(pkg *build.Package, err error) {
   106  	if err != nil {
   107  		if _, nogo := err.(*build.NoGoError); nogo {
   108  			// Don't complain if the failure is due to no Go source files.
   109  			return
   110  		}
   111  		fmt.Fprintln(os.Stderr, err)
   112  		return
   113  	}
   114  
   115  	var files []string
   116  	files = append(files, pkg.GoFiles...)
   117  	files = append(files, pkg.CgoFiles...)
   118  	files = append(files, pkg.TestGoFiles...)
   119  	if pkg.Dir != "." {
   120  		for i, f := range files {
   121  			files[i] = filepath.Join(pkg.Dir, f)
   122  		}
   123  	}
   124  	// TODO(dsymonds): Do foo_test too (pkg.XTestGoFiles)
   125  
   126  	lintFiles(files...)
   127  }