kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/release/cloudbuild/pre-commit/go (about) 1 #!/bin/bash -e 2 # 3 # Copyright 2019 The Kythe Authors. 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 # Wrapper script to run the Go version specified by a go.mod file. 18 19 CACHE="$HOME/.cache/goversions" 20 21 GO="$(command -v go | xargs -L1 realpath | grep -v "$(realpath "$0")" | head -n1)" 22 if ! [[ -x "$GO" ]]; then 23 echo "ERROR: failed to find ambient go tool" >&2 24 exit 1 25 fi 26 27 find_gomod() { 28 local dir="$PWD" 29 local f="$dir/go.mod" 30 while true; do 31 if [[ -r "$f" ]]; then 32 echo "$f" 33 return 34 elif [[ "/" == "$dir" ]]; then 35 return 1 36 fi 37 dir="$(dirname "$dir")" 38 done 39 } 40 41 if ! gomod="$(find_gomod)"; then 42 echo "WARNING: could not find go.mod" >&2 43 exec "$GO" "$@" 44 fi 45 46 if ! version="$(grep -Po '(?<=^go\s)\d+(\.\d+)+$' "$gomod")"; then 47 echo "WARNING: could not find version in go.mod" >&2 48 exec "$GO" "$@" 49 fi 50 51 GOROOT="${CACHE}/${version}/go" 52 53 if [[ -x "$GOROOT/bin/go" ]]; then 54 # Cache hit: exec go tool 55 exec "$GOROOT/bin/go" "$@" 56 fi 57 58 # Setup temporary location for archive 59 archive="$(mktemp)" 60 trap "rm '$archive'" EXIT ERR INT 61 62 # Download and extract Go compiler 63 url="https://dl.google.com/go/go${version}.linux-amd64.tar.gz" 64 curl -L "$url">"$archive" 65 dir="$(dirname "$GOROOT")" 66 rm -rf "$dir" 67 mkdir -p "$dir" 68 tar xf "$archive" -C "$dir" 69 rm -f "$archive" 70 71 # Run go tool 72 exec "$GOROOT/bin/go" "$@"