gitlab.com/polyapp-open-source/poly@v0.0.0-20200304172929-90b164ae7520/cmd/lint.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/exec"
     8  	"strings"
     9  
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  // lintCmd represents the Lint command
    14  //nolint
    15  var lintCmd = &cobra.Command{
    16  	Use:   "lint",
    17  	Short: "Runs 'golangci-lint run --fix'",
    18  	Long: `Uses configuration file in the current directory.
    19  If none is found a default configuration is loaded.
    20  Results are collected and printed one after the other. Example output:
    21  
    22  $ poly.exe lint
    23  cmd\hello\main_test.go:5:1: tests: Testmain has malformed name: first letter after 'Test' must not be lowercase (govet)
    24  func Testmain(t *testing.T) {
    25  ^
    26  golangci-lint failed: exit status 1
    27  
    28  `,
    29  	Run: func(cmd *cobra.Command, args []string) {
    30  		exitCode := Lint()
    31  		os.Exit(exitCode)
    32  	},
    33  }
    34  
    35  func init() {
    36  	rootCmd.AddCommand(lintCmd)
    37  }
    38  
    39  // Lint performs the work of 'poly lint'.
    40  func Lint() (exitCode int) {
    41  	configPath := ".golangci.yml"
    42  	f, err := os.Open(configPath)
    43  	if err != nil || f == nil {
    44  		err = ioutil.WriteFile(configPath, []byte(configFile), 0644)
    45  		if err != nil {
    46  			fmt.Println("Couldn't create configuration file: " + err.Error())
    47  			return 1
    48  		}
    49  		defer os.Remove(configPath)
    50  	}
    51  	f.Close()
    52  	golangciLintCmd := exec.Command("golangci-lint", "run", "--fix", "--config", configPath)
    53  	golangciLintCmd.Stderr = os.Stderr
    54  	golangciLintCmd.Stdout = os.Stdout
    55  	err = golangciLintCmd.Run()
    56  	if err != nil {
    57  		fmt.Println("golangci-lint failed: " + err.Error())
    58  		if strings.Contains(err.Error(), `executable file not found`) {
    59  			fmt.Println("golangci-lint not found. You should install it with these directions: https://github.com/golangci/golangci-lint#install ")
    60  			return 1
    61  		} else if err.Error() != "exit status 1" { //nolint
    62  			fmt.Printf("\nnon-standard error code detected. Running golanglint-ci again in verbose mode\n\n")
    63  			golangciLintCmdVerbose := exec.Command("golangci-lint", "run", "--fix", "--config", configPath, "-v")
    64  			golangciLintCmdVerbose.Stderr = os.Stderr
    65  			golangciLintCmdVerbose.Stdout = os.Stdout
    66  			err = golangciLintCmdVerbose.Run()
    67  			if err != nil {
    68  				fmt.Println("golangci-lint failed: " + err.Error())
    69  				return 1
    70  			}
    71  		}
    72  	}
    73  	return 0
    74  }
    75  
    76  var configFile = `
    77  linters-settings:
    78    depguard:
    79      list-type: blacklist
    80      packages:
    81        # logging is allowed only by logutils.Log, logrus
    82        # is allowed to use only in logutils package
    83        - github.com/sirupsen/logrus
    84      packages-with-error-message:
    85        - github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
    86    dupl:
    87      threshold: 100
    88    funlen:
    89      lines: 100
    90      statements: 50
    91    goconst:
    92      min-len: 2
    93      min-occurrences: 3
    94    gocritic:
    95      enabled-tags:
    96        - diagnostic
    97        - experimental
    98        - opinionated
    99        - performance
   100        - style
   101      disabled-checks:
   102        - dupImport # https://github.com/go-critic/go-critic/issues/845
   103        - ifElseChain
   104        - octalLiteral
   105        - whyNoLint
   106        - wrapperFunc
   107    gocyclo:
   108      min-complexity: 15
   109    goimports:
   110      local-prefixes: github.com/golangci/golangci-lint
   111    golint:
   112      min-confidence: 0
   113    govet:
   114      check-shadowing: true
   115      settings:
   116        printf:
   117          funcs:
   118            - (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
   119            - (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
   120            - (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
   121            - (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
   122    lll:
   123      line-length: 140
   124    maligned:
   125      suggest-new: true
   126    misspell:
   127      locale: US
   128  
   129  linters:
   130    disable-all: true
   131    enable:
   132      - bodyclose
   133      - deadcode
   134      - depguard
   135      - dogsled
   136      - dupl
   137      - errcheck
   138      - funlen
   139      - goconst
   140      - gocritic
   141      - gocyclo
   142      - gofmt
   143      - goimports
   144      - golint
   145      - gosec
   146      - gosimple
   147      - govet
   148      - ineffassign
   149      - interfacer
   150      - lll
   151      - misspell
   152      - nakedret
   153      - rowserrcheck
   154      - scopelint
   155      - staticcheck
   156      - structcheck
   157      - stylecheck
   158      - typecheck
   159      - unconvert
   160      - unparam
   161      - unused
   162      - varcheck
   163      - whitespace
   164  
   165    # don't enable:
   166    # - gochecknoglobals
   167    # - gocognit
   168    # - godox
   169    # - maligned
   170    # - prealloc
   171  
   172  issues:
   173    # Excluding configuration per-path, per-linter, per-text and per-source
   174    exclude-rules:
   175      - path: _test\.go
   176        linters:
   177          - gomnd
   178  
   179  run:
   180    skip-dirs:
   181      - test/testdata_etc
   182      - internal/cache
   183      - internal/renameio
   184      - internal/robustio
   185  
   186  # golangci.com configuration
   187  # https://github.com/golangci/golangci/wiki/Configuration
   188  service:
   189    golangci-lint-version: 1.23.x # use the fixed version to not introduce new linters unexpectedly
   190    prepare:
   191      - echo "here I can run custom commands, but no preparation needed for this repo"
   192  `