devtools: add lib.sh

Move common shell functions to a separate file in anticipation
of more shell scripts.

Change-Id: Idf6148809f9498f4656cb8c28618ec2881deb5f7
Reviewed-on: https://go-review.googlesource.com/c/vuln/+/369835
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/all.bash b/all.bash
index 32961f7..0a46ce8 100755
--- a/all.bash
+++ b/all.bash
@@ -3,42 +3,7 @@
 # Use of this source code is governed by a BSD-style
 # license that can be found in the LICENSE file.
 
-RED=; GREEN=; YELLOW=; NORMAL=;
-MAXWIDTH=0
-
-if tput setaf 1 >& /dev/null; then
-  RED=`tput setaf 1`
-  GREEN=`tput setaf 2`
-  YELLOW=`tput setaf 3`
-  NORMAL=`tput sgr0`
-  MAXWIDTH=$(( $(tput cols) - 2 ))
-fi
-
-EXIT_CODE=0
-
-info() { echo -e "${GREEN}$@${NORMAL}" 1>&2; }
-warn() { echo -e "${YELLOW}$@${NORMAL}" 1>&2; }
-err() { echo -e "${RED}$@${NORMAL}" 1>&2; EXIT_CODE=1; }
-
-die() {
-  err $@
-  exit 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
-# the script.
-runcmd() {
-  msg="$@"
-  # Truncate command logging for narrow terminals.
-  # Account for the 2 characters of '$ '.
-  if [[ $MAXWIDTH -gt 0 && ${#msg} -gt $MAXWIDTH ]]; then
-    msg="${msg::$(( MAXWIDTH - 3 ))}..."
-  fi
-
-  echo -e "$@\n" 1>&2;
-  $@ || err "command failed"
-}
+source devtools/lib.sh
 
 # ensure_go_binary verifies that a binary exists in $PATH corresponding to the
 # given go-gettable URI. If no such binary exists, it is fetched via `go get`.
diff --git a/devtools/lib.sh b/devtools/lib.sh
new file mode 100644
index 0000000..94dccbb
--- /dev/null
+++ b/devtools/lib.sh
@@ -0,0 +1,42 @@
+# 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=;
+MAXWIDTH=0
+
+if tput setaf 1 >& /dev/null; then
+  RED=`tput setaf 1`
+  GREEN=`tput setaf 2`
+  YELLOW=`tput setaf 3`
+  NORMAL=`tput sgr0`
+  MAXWIDTH=$(( $(tput cols) - 2 ))
+fi
+
+EXIT_CODE=0
+
+info() { echo -e "${GREEN}$@${NORMAL}" 1>&2; }
+warn() { echo -e "${YELLOW}$@${NORMAL}" 1>&2; }
+err() { echo -e "${RED}$@${NORMAL}" 1>&2; EXIT_CODE=1; }
+
+die() {
+  err $@
+  exit 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
+# the script.
+runcmd() {
+  msg="$@"
+  # Truncate command logging for narrow terminals.
+  # Account for the 2 characters of '$ '.
+  if [[ $MAXWIDTH -gt 0 && ${#msg} -gt $MAXWIDTH ]]; then
+    msg="${msg::$(( MAXWIDTH - 3 ))}..."
+  fi
+
+  echo -e "$@\n" 1>&2;
+  $@ || err "command failed"
+}