This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
forked from camptocamp/build-debian-cloud
-
Notifications
You must be signed in to change notification settings - Fork 43
/
utils
151 lines (137 loc) · 3.22 KB
/
utils
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# Define some colors
txtdef="\e[0m" # revert to default color
bldred="\e[1;31m" # bold red
txtblu="\e[0;34m" # blue
txtbld="\e[1m" # bold
txtund="\e[4m" # underline
logfile=''
logcolor=$txtblu
errfile=''
errcolor=$bldred
# Logging function without coloring
function logplain {
for line in "$@"; do
out "$line\n" "$line\n" $logfile
done
}
# Logging function, each arg is a line
function log {
for line in "$@"; do
out "$logcolor$line$txtdef\n" "$line\n" $logfile
done
}
# Log without the newline
function logn {
out "$logcolor$1$txtdef" "$1" $logfile
}
# Custom die function, output to stderr instead of stdout, each arg is a line
function die {
for line in "$@"; do
out "$errcolor$line$txtdef\n" "$line\n" $errfile >&2
done
exit 1
}
# Output function, takes $msg, $filemsg and $file args in that order
function out {
printf -- "$1"
if [ -n "$3" ]; then
printf -- "$2" >>$3
fi
}
# Wait for the execution of $cmd not to return an empty string
function dotdot {
local cmd=$1
local status=`eval $cmd`
local sleep=5
[ ! -z "$2" ] && sleep=$2
while [ -z "$status" ]; do
logn '.'
sleep $sleep
# Don't error out if the command fails.
status=`eval $cmd || true`
done
logn "\n"
}
# Turns once per newline that is piped into it
function spin {
local cursor='|'
# Running `tput cols` in every iteration would cause significant slowdown.
# Only do this once, the spinner is not a "need to have" anyways.
local cols=$(( `tput cols` - 2 ))
while read line; do
printf -- "\r$logcolor$cursor$txtdef %-${cols}s" "${line:0:$cols}"
case $cursor in
'|') cursor='/' ;;
'/') cursor='-' ;;
'-') cursor='\\' ;;
'\\') cursor='|' ;;
esac
done
printf "\n"
}
function insert_task_before {
if [ ! -e $2 ]; then
die "Unable to add task `basename $2` to the tasklist." "$2 does not exist."
fi
if ! $(contains_basename $1 tasks[@]); then
die "Unable to add `basename $2` to the tasklist." "Task $1 does not exist."
fi
local i=0
for task in ${tasks[*]}; do
if [ `basename $task` == "$1" ]; then
log "Adding task `basename $2` before task `basename $task`"
tasks[$i]=$2
i=$[i+1]
fi
tasks[$i]=$task
i=$[i+1]
done
}
function insert_task_after {
if [ ! -e $2 ]; then
die "Unable to add task `basename $2` to the tasklist." "$2 does not exist."
fi
if ! $(contains_basename $1 tasks[@]); then
die "Unable to add `basename $2` to the tasklist." "Task $1 does not exist."
fi
local i=0
for task in ${tasks[*]}; do
tasks[$i]=$task
i=$[i+1]
if [ `basename $task` == "$1" ]; then
log "Adding task `basename $2` after task `basename $task`"
tasks[$i]=$2
i=$[i+1]
fi
done
}
function remove_task {
if ! $(contains_basename $1 tasks[@]); then
die "Unable to remove $1 from the tasklist." "The task does not exist."
fi
local i=0
for task in ${tasks[*]}; do
if [ `basename $task` == "$1" ]; then
log "Removing task $1"
else
tasks[$i]=$task
i=$[i+1]
fi
done
unset tasks[${#tasks[@]}-1]
}
function contains {
declare -a haystack=("${!2}")
for needle in ${haystack[*]}; do
[ "$needle" == "$1" ] && exit 0
done
exit 1
}
function contains_basename {
declare -a haystack=("${!2}")
for needle in ${haystack[*]}; do
[ "`basename $needle`" == "$1" ] && exit 0
done
exit 1
}