| # Copyright 2021 The Go Authors. All rights reserved. |
| # Use of this source code is governed by a BSD-style |
| # license that can be found in the LICENSE file. |
| # Library of useful bash functions and variables. |
| RED=; GREEN=; YELLOW=; NORMAL=; |
| if tput setaf 1 >& /dev/null; then |
| MAXWIDTH=$(( $(tput cols) - 2 )) |
| info() { echo -e "${GREEN}$@${NORMAL}" 1>&2; } |
| warn() { echo -e "${YELLOW}$@${NORMAL}" 1>&2; } |
| err() { echo -e "${RED}$@${NORMAL}" 1>&2; EXIT_CODE=1; } |
| # runcmd prints an info log describing the command that is about to be run, and |
| # then runs it. It sets EXIT_CODE to non-zero if the command fails, but does not exit |
| echo -e "${YELLOW}dryrun${GREEN}\$ $msg${NORMAL}" |
| # Truncate command logging for narrow terminals. |
| # Account for the 2 characters of '$ '. |
| if [[ $MAXWIDTH -gt 0 && ${#msg} -gt $MAXWIDTH ]]; then |
| msg="${msg::$(( MAXWIDTH - 3 ))}..." |
| $@ || err "command failed" |