github.com/google/cadvisor@v0.49.1/build/check_gotidy.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2020 Google Inc. All rights reserved.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  # TODO(bobbypage): Replace this with `go mod tidy --check` when it exists:
    18  # https://github.com/golang/go/issues/27005.
    19  
    20  # Checks if go mod tidy changes are needed for a given go module.
    21  # If changes are needed, prints the diff and exits 1 status code.
    22  # Arguments:
    23  #   Directory of go module
    24  function lint_gotidy() {
    25      MODULE_DIRECTORY="$1"
    26  
    27      pushd "${MODULE_DIRECTORY}" > /dev/null
    28      TMP_GOMOD=$(mktemp)
    29      TMP_GOSUM=$(mktemp)
    30  
    31      # Make a copy of the current files
    32      cp go.mod "${TMP_GOMOD}"
    33      cp go.sum "${TMP_GOSUM}"
    34  
    35      go mod tidy
    36  
    37      DIFF_MOD=$(diff -u "${TMP_GOMOD}" go.mod)
    38      DIFF_SUM=$(diff -u "${TMP_GOSUM}" go.sum)
    39  
    40      # Copy the files back
    41      cp "${TMP_GOMOD}" go.mod
    42      cp "${TMP_GOSUM}" go.sum
    43  
    44      if [[ -n "${DIFF_MOD}" || -n "${DIFF_SUM}" ]]; then
    45          echo "go tidy changes are needed; please run make tidy"
    46          echo "go.mod diff:"
    47          echo "${DIFF_MOD}"
    48          echo "go.sum diff:"
    49          echo "${DIFF_SUM}"
    50          exit 1
    51      fi
    52  
    53      popd > /dev/null
    54  }
    55  
    56  # Check if go mod tidy changes needed on main module
    57  lint_gotidy "."
    58  
    59  # Check if go mod tidy changes needed on cmd module
    60  lint_gotidy "cmd"