-
Notifications
You must be signed in to change notification settings - Fork 142
/
Copy pathcommon.sh
93 lines (82 loc) · 2.78 KB
/
common.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
init() {
find_executables
populate_environment
define_colors
}
find_executables() {
KUBECTL=$(find_executable "kubectl")
KIND=$(find_executable "kind")
DAPR=$(find_executable "dapr")
HELM=$(find_executable "helm")
STERN=$(find_executable "stern")
KN=$(find_executable "kn")
JQ=$(find_executable "jq")
}
populate_environment() {
export ARCH="${ARCH:-amd64}"
export CONTAINER_ENGINE=${CONTAINER_ENGINE:-docker}
export KUBECONFIG="${KUBECONFIG:-$(dirname "$(realpath "$0")")/bin/kubeconfig.yaml}"
export TERM="${TERM:-dumb}"
echo "KUBECONFIG=${KUBECONFIG}"
}
define_colors() {
# For some reason TERM=dumb results in the tput commands exiting 1. It must
# not support that terminal type. A reasonable fallback should be "xterm".
local TERM="$TERM"
if [[ -z "$TERM" || "$TERM" == "dumb" ]]; then
TERM="xterm" # Set TERM to a tput-friendly value when undefined or "dumb".
fi
# shellcheck disable=SC2155
red=$(tput bold)$(tput setaf 1)
# shellcheck disable=SC2155
green=$(tput bold)$(tput setaf 2)
# shellcheck disable=SC2155
blue=$(tput bold)$(tput setaf 4)
# shellcheck disable=SC2155
grey=$(tput bold)$(tput setaf 8)
# shellcheck disable=SC2155
yellow=$(tput bold)$(tput setaf 11)
# shellcheck disable=SC2155
reset=$(tput sgr0)
}
# find returns the path to an executable by name.
# An environment variable FUNC_TEST_$name takes precidence.
# Next is an executable matching the name in hack/bin/
# (the install location of hack/install-binaries.sh)
# Finally, a matching executable from the current PATH is used.
find_executable() {
local name="$1" # requested binary name
local path="" # the path to output
# Use the environment variable if defined
local env=$(echo "FUNC_TEST_$name" | awk '{print toupper($0)}')
local path="${!env:-}"
if [[ -x "$path" ]]; then
echo "$path" & return 0
fi
# Use the binary installed into hack/bin/ by allocate.sh if
# it exists.
path=$(dirname "$(realpath "$0")")"/bin/$name"
if [[ -x "$path" ]]; then
echo "$path" & return 0
fi
# Finally fallback to anything matchin in the current PATH
path=$(command -v "$name")
if [[ -x "$path" ]]; then
echo "$path" & return 0
fi
echo "Error: ${name} not found." >&2
return 1
}
init "$@"