-
Notifications
You must be signed in to change notification settings - Fork 2
/
.functions.sh
executable file
·112 lines (100 loc) · 3.08 KB
/
.functions.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
103
104
105
106
107
108
109
110
111
112
#
# ScreenInvader - A shared media experience. Instant and seamless.
# Copyright (C) 2012 Amir Hassan <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#!/bin/bash
function black { echo -ne "\033[0;30m$1\033[0m"; tput sgr0; }
function blue { echo -ne "\033[0;34m$1\033[0m"; tput sgr0; }
function green { echo -ne "\033[0;32m$1\033[0m"; tput sgr0; }
function cyan { echo -ne "\033[0;36m$1\033[0m"; tput sgr0; }
function red { echo -ne "\033[1;31m$1\033[0m"; tput sgr0; }
function purple { echo -ne "\033[0;35m$1\033[0m"; tput sgr0; }
function brown { echo -ne "\033[0;33m$1\033[0m"; tput sgr0; }
function lightgray { echo -ne "\033[0;37m$1\033[0m"; tput sgr0; }
function lightgreen { echo -ne "\033[1;32m$1\033[0m"; tput sgr0; }
function lightcyan { echo -ne "\033[1;36m$1\033[0m"; tput sgr0; }
function lightred { echo -ne "\033[0;31m$1\033[0m"; tput sgr0; }
function lightpurple { echo -ne "\033[1;35m$1\033[0m"; tput sgr0; }
function yellow { echo -ne "\033[1;33m$1\033[0m"; tput sgr0; }
function white { echo -ne "\033[1;37m$1\033[0m"; tput sgr0; }
function warn { red "$1\n"; }
function ok { green "ok\n"; }
function failed { red "failed\n"; }
function verbose { [ ! -z $VERBOSE ] && echo "$@ " 1>&2; }
function verboseexec { verbose $@; bash -c "$@"; }
function error {
msg=$1;
errcode=$2;
[ -z "$errcode" ] && errcode=1;
[ -z "$msg" ] && failed || red "$msg\n";
exit $errcode;
}
function try {
fatal=true
targetfd=2
OPTIND=0
while getopts 'no:' c
do
case $c in
n)fatal="false";;
o)targetfd="$OPTARG";;
\?)echo "Invalid option: -$OPTARG" >&2;;
esac
done
shift $((OPTIND-1));
errcode=0;
msg="$1"
echo -en "$msg: " 1>&2;
shift;
verbose $@;
bash -c "$@ 2>&1 1>&$targetfd"
errcode=$?;
if [ $errcode != 0 ]; then
[ "$fatal" != "true" ] && failed 1>&2 || error 1>&2;
else
ok 1>&2;
fi
return $errcode;
}
function check {
log="&1"
if [ -n "$BOOTSTRAP_LOG" ]; then
echo "### $1" >> "$BOOTSTRAP_LOG"
try -o "1" "$1" "$2" >>"$BOOTSTRAP_LOG"
else
try "$1" "$2"
fi
[ $? -ne 0 ] && exit $?
}
function checkcat {
try -n -o "1" "$1" "echo; $2"
}
function trycat {
try -o "1" "$1" "$2"
}
function absDir() {
dir="`dirname $1`"
absdir="`cd $dir; pwd`"
echo $absdir
}
function absPath() {
dir="`dirname $1`"
base="`basename $1`"
absdir="`cd $dir; pwd`"
echo $absdir/$base
}
export -f check try error ok failed red green yellow verbose