github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/scripts/presubmit.sh (about)

     1  #!/bin/bash
     2  #
     3  # Presubmit checks for Trillian examples.
     4  #
     5  # Checks for lint errors, spelling, licensing, correct builds / tests and so on.
     6  # Flags may be specified to allow suppressing of checks or automatic fixes, try
     7  # `scripts/presubmit.sh --help` for details.
     8  #
     9  # Globals:
    10  #   GO_TEST_TIMEOUT: timeout for 'go test'. Optional (defaults to 5m).
    11  set -eu
    12  
    13  check_pkg() {
    14    local cmd="$1"
    15    local pkg="$2"
    16    check_cmd "$cmd" "try running 'go get -u $pkg'"
    17  }
    18  
    19  check_cmd() {
    20    local cmd="$1"
    21    local msg="$2"
    22    if ! type -p "${cmd}" > /dev/null; then
    23      echo "${cmd} not found, ${msg}"
    24      return 1
    25    fi
    26  }
    27  
    28  usage() {
    29    echo "$0 [--coverage] [--fix] [--no-build] [--no-linters] [--no-generate] [--no-actions] [--no-docker] [--no-serverless-wasm] [--cloud-build]"
    30  }
    31  
    32  main() {
    33    local coverage=0
    34    local fix=0
    35    local flag_cloud_build=0
    36    local run_build=1
    37    local build_actions=1
    38    local build_docker=1
    39    local run_lint=1
    40    local run_generate=1
    41    while [[ $# -gt 0 ]]; do
    42      case "$1" in
    43        --coverage)
    44          coverage=1
    45          ;;
    46        --fix)
    47          fix=1
    48          ;;
    49        --help)
    50          usage
    51          exit 0
    52          ;;
    53        --no-build)
    54          run_build=0
    55          ;;
    56        --no-linters)
    57          run_lint=0
    58          ;;
    59        --no-generate)
    60          run_generate=0
    61          ;;
    62        --no-actions)
    63          build_actions=0
    64          ;;
    65        --no-docker)
    66          build_docker=0
    67          ;;
    68        --cloud-build)
    69          flag_cloud_build=1
    70          ;;
    71        *)
    72          usage
    73          exit 1
    74          ;;
    75      esac
    76      shift 1
    77    done
    78  
    79    cd "$(dirname "$0")"  # at scripts/
    80    cd ..  # at top level
    81  
    82    go_srcs="$(find . -name '*.go' | \
    83      grep -v vendor/ | \
    84      grep -v registers/ | \
    85      grep -v mock_ | \
    86      grep -v .pb.go | \
    87      grep -v .pb.gw.go | \
    88      grep -v _string.go | \
    89      tr '\n' ' ')"
    90  
    91    if [[ "$fix" -eq 1 ]]; then
    92      check_pkg goimports golang.org/x/tools/cmd/goimports || exit 1
    93  
    94      echo 'running gofmt'
    95      gofmt -s -w ${go_srcs}
    96      echo 'running goimports'
    97      goimports -w ${go_srcs}
    98    fi
    99  
   100    if [[ "${flag_cloud_build}" -eq 1 ]]; then
   101        export GO_TEST_DOCKER_INTEGRATION="cloudbuild"
   102    fi
   103  
   104    if [[ "${run_build}" -eq 1 ]]; then
   105      echo 'running go build'
   106      go build ./...
   107  
   108      local coverflags=""
   109      if [[ ${coverage} -eq 1 ]]; then
   110          coverflags="-covermode=atomic -coverprofile=coverage.txt"
   111      fi
   112  
   113      echo "running go test"
   114      go test \
   115          -short \
   116          -timeout=${GO_TEST_TIMEOUT:-5m} \
   117          ${coverflags} \
   118          ./...
   119    fi
   120  
   121    if [[ "${build_docker}" -eq 1 ]]; then
   122      echo "Building non-serverless-action dockerfiles ===================="
   123      for i in $(find . -name Dockerfile ); do
   124        echo "Building ${i} ------------------------------------------------"
   125        docker build -f "${i}" .
   126      done
   127    fi
   128  
   129    if [[ "${run_lint}" -eq 1 ]]; then
   130      check_cmd golangci-lint \
   131        'have you installed github.com/golangci/golangci-lint?' || exit 1
   132  
   133      echo 'running golangci-lint'
   134      golangci-lint run --deadline=10m
   135      echo 'checking license headers'
   136      ./scripts/check_license.sh ${go_srcs}
   137    fi
   138  
   139    if [[ "${run_generate}" -eq 1 ]]; then
   140      check_cmd protoc 'have you installed protoc?'
   141  
   142      echo 'running go generate'
   143      go generate -run="protoc" ./...
   144    fi
   145  }
   146  
   147  main "$@"