github.com/anchore/syft@v1.38.2/.github/scripts/go-mod-tidy-check.sh (about)

     1  #!/usr/bin/env bash
     2  set -eu
     3  
     4  ORIGINAL_STATE_DIR=$(mktemp -d "TEMP-original-state-XXXXXXXXX")
     5  TIDY_STATE_DIR=$(mktemp -d "TEMP-tidy-state-XXXXXXXXX")
     6  
     7  trap "cp -p ${ORIGINAL_STATE_DIR}/* ./ && git update-index -q --refresh && rm -fR ${ORIGINAL_STATE_DIR} ${TIDY_STATE_DIR}" EXIT
     8  
     9  # capturing original state of files...
    10  cp go.mod go.sum "${ORIGINAL_STATE_DIR}"
    11  
    12  # capturing state of go.mod and go.sum after running go mod tidy...
    13  go mod tidy
    14  cp go.mod go.sum "${TIDY_STATE_DIR}"
    15  
    16  set +e
    17  
    18  # detect difference between the git HEAD state and the go mod tidy state
    19  DIFF_MOD=$(diff -u "${ORIGINAL_STATE_DIR}/go.mod" "${TIDY_STATE_DIR}/go.mod")
    20  DIFF_SUM=$(diff -u "${ORIGINAL_STATE_DIR}/go.sum" "${TIDY_STATE_DIR}/go.sum")
    21  
    22  if [[ -n "${DIFF_MOD}" || -n "${DIFF_SUM}" ]]; then
    23      echo "go.mod diff:"
    24      echo "${DIFF_MOD}"
    25      echo "go.sum diff:"
    26      echo "${DIFF_SUM}"
    27      echo ""
    28      printf "FAILED! go.mod and/or go.sum are NOT tidy; please run 'go mod tidy'.\n\n"
    29      exit 1
    30  fi