forked from project-everest/everest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.sh
94 lines (82 loc) · 1.47 KB
/
lib.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
# For pretty output
color () {
if [ -t 1 ]; then
tput setaf $2 || true
echo $1
tput sgr0 || true
else
echo $1
fi
}
red () {
color "$1" 1
}
green () {
color "$1" 2
}
blue () {
color "$1" 4
}
magenta () {
color "$1" 5
}
# [count log-file] displays the number of lines received so far in the terminal,
# while writing its output in log-file
count () {
i=0
echo -n > $1
while read line; do
echo $line >> $1
i=$(($i+1))
echo -ne "\r$i lines of output"
done
echo
blue "log written to $1"
}
# The return value of Bash function is the exit status of their last command;
# therefore, you can do things like [if is_osx; then ...].
is_osx () {
[[ $(uname) == "Darwin" ]]
}
is_windows () {
[[ $OS == "Windows_NT" ]]
}
# If a command [cmd] is not found in path, then [success_or cmd msg] prints
# [msg] if non-empty, then aborts with a non-zero exit status.
success_or ()
{
if ! command -v $1 >/dev/null 2>&1; then
red "ERROR: $1 not found"
if [[ $2 != "" ]]; then
echo "Hint: $2"
fi
exit 1
fi
echo ... $1 found
}
# [if_yes cmd] prompts the user, and runs [cmd] if user approved, and aborts
# otherwise
prompt_yes ()
{
if $INTERACTIVE; then
read ans
case "$ans" in
[Yy]|""|"yes")
$1
;;
*)
$2
;;
esac
else
$1
fi
}
if_yes ()
{
echo "Do you want to run: $1? [Yn]"
prompt_yes "$1" "exit 1"
}
cygwin_has () {
(( $(cygcheck -c -d $1 | wc -l) > 2 ))
}