forked from kubernetes-retired/kpng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.sh
executable file
·103 lines (92 loc) · 4.38 KB
/
utils.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
94
95
96
97
98
99
100
101
102
#!/bin/bash
# Copyright 2021 The Kubernetes Authors.
#
# 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
#
# http://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.
function if_error_exit {
###########################################################################
# Description: #
# Validate if previous command failed and show an error msg (if provided) #
# #
# Arguments: #
# $1 - error message if not provided, it will just exit #
###########################################################################
if [ "$?" != "0" ]; then
if [ -n "$1" ]; then
RED="\033[91m"
ENDCOLOR="\033[0m"
echo -e "[ ${RED}FAILED${ENDCOLOR} ] ${1}"
fi
exit 1
fi
}
function pass_message {
###########################################################################
# Description: #
# show [PASSED] in green and a message as the validation passed. #
# #
# Arguments: #
# $1 - message to output #
###########################################################################
if [ -z "${1}" ]; then
echo "pass_message() requires a message"
exit 1
fi
GREEN="\033[92m"
ENDCOLOR="\033[0m"
echo -e "[ ${GREEN}PASSED${ENDCOLOR} ] ${1}"
}
function info_message {
###########################################################################
# Description: #
# show [INFO] in blue and a message as the validation passed. #
# #
# Arguments: #
# $1 - message to output #
###########################################################################
if [ -z "${1}" ]; then
echo "info_message() requires a message"
exit 1
fi
BLUE="\033[94m"
ENDCOLOR="\033[0m"
echo -e "[ ${BLUE}INFO${ENDCOLOR} ] ${1}"
}
function add_to_path {
###########################################################################
# Description: #
# Add directory to path #
# #
# Arguments: #
# arg1: directory #
###########################################################################
[ $# -eq 1 ]
if_error_exit "Wrong number of arguments to ${FUNCNAME[0]}"
local directory="${1}"
[ -d "${directory}" ]
if_error_exit "Directory \"${directory}\" does not exist"
case ":${PATH:-}:" in
*:${directory}:*) ;;
*) PATH="${directory}${PATH:+:$PATH}" ;;
esac
}
command_exists() {
###########################################################################
# Description: #
# Checks if a binary exists #
# #
# Arguments: #
# arg1: binary name #
###########################################################################
cmd="$1"
command -v "${cmd}" >/dev/null 2>&1
}