github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/scripts/golinters.bash (about)

     1  #!/bin/bash
     2  # Copyright 2019 Canonical Ltd.
     3  # Licensed under the AGPLv3, see LICENCE file for details.
     4  exitstatus=0
     5  
     6  OUTPUT_FILE=$(mktemp) || { echo "Failed to create temp file"; exit 1; }
     7  MOCK_HEADER="// Code generated by MockGen. DO NOT EDIT."
     8  
     9  # go get the golangci-lint, which will install the binary 
    10  # into $GOPATH/bin
    11  go get -u github.com/golangci/golangci-lint/cmd/golangci-lint
    12  
    13  # verify that the golangci-lint exists in the $GOPATH bin
    14  if [ ! -f "$GOPATH/bin/golangci-lint" ]; then
    15      (>&2 echo "Error: golangci-lint is not installed.")
    16      exit 1
    17  fi
    18  
    19  SKIP_DIRS=
    20  if [[ "${CGO_ENABLED}" != "1" ]]; then
    21    SKIP_DIRS=--skip-dirs worker/dbaccessor
    22  fi
    23  
    24  $GOPATH/bin/golangci-lint run \
    25      --disable-all \
    26      --no-config \
    27      --max-issues-per-linter=50 \
    28      --max-same-issues=50 \
    29      --issues-exit-code=0 \
    30      --enable=gofmt \
    31      --enable=goimports \
    32      --enable=misspell \
    33      --enable=unconvert \
    34      --enable=ineffassign \
    35      ${SKIP_DIRS} \
    36      &> $OUTPUT_FILE
    37  
    38  # go through each golangci-lint error and check to see if it's related
    39  # to a mock file.
    40  invalidLines=`cat $OUTPUT_FILE | grep -v '\(.*\/[^\/]*\.go\):[[:digit:]]*:[[:digit:]]*:.*' | wc -l`
    41  if [ $invalidLines -ne 0 ]; then
    42      # there was a parse error when reading the golangci-lint output
    43      exitstatus=1
    44      (>&2 cat $OUTPUT_FILE)
    45  else
    46      # valid set of lines, go through each and make sure they're valid
    47      # mock files, if not exit out.
    48      lines=`wc -l $OUTPUT_FILE | awk '{print $1}'`
    49      if [ $lines -ne 0 ]; then
    50          while read p; do
    51              header=`echo $p | cut -d':' -f1 | xargs head -n1`
    52              if [ "$header" != "$MOCK_HEADER" ]; then
    53                  exitstatus=1
    54                  (>&2 echo $p)
    55              fi
    56          done <$OUTPUT_FILE
    57      fi
    58  fi
    59  rm $OUTPUT_FILE
    60  exit $exitstatus