Browse code

add pipefail * new functions library v3.9 with core-options

Nat! authored on 13-07-2017 12:09:39
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,208 @@
1
+#! /usr/bin/env bash
2
+#
3
+#   Copyright (c) 2017 Nat! - Mulle kybernetiK
4
+#   All rights reserved.
5
+#
6
+#   Redistribution and use in source and binary forms, with or without
7
+#   modification, are permitted provided that the following conditions are met:
8
+#
9
+#   Redistributions of source code must retain the above copyright notice, this
10
+#   list of conditions and the following disclaimer.
11
+#
12
+#   Redistributions in binary form must reproduce the above copyright notice,
13
+#   this list of conditions and the following disclaimer in the documentation
14
+#   and/or other materials provided with the distribution.
15
+#
16
+#   Neither the name of Mulle kybernetiK nor the names of its contributors
17
+#   may be used to endorse or promote products derived from this software
18
+#   without specific prior written permission.
19
+#
20
+#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+#   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+#   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24
+#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+#   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+#   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+#   POSSIBILITY OF SUCH DAMAGE.
31
+#
32
+MULLE_BOOTSTRAP_CORE_OPTIONS_SH="included"
33
+
34
+
35
+## core option parsing
36
+# not used by mulle-bootstrap itself at the moment
37
+
38
+#
39
+# variables called flag. because they are indirectly set by flags
40
+#
41
+core_dump_env()
42
+{
43
+   log_trace "FULL trace started"
44
+   log_trace "ARGS:${C_TRACE2} ${MULLE_ARGUMENTS}"
45
+   log_trace "PWD :${C_TRACE2} `pwd -P 2> /dev/null`"
46
+   log_trace "ENV :${C_TRACE2} `env | sort`"
47
+   log_trace "LS  :${C_TRACE2} `ls -a1F`"
48
+}
49
+
50
+
51
+core_setup_trace()
52
+{
53
+   case "${1}" in
54
+      VERBOSE)
55
+         MULLE_FLAG_LOG_VERBOSE="YES"
56
+      ;;
57
+
58
+      FLUFF)
59
+         MULLE_FLAG_LOG_FLUFF="YES"
60
+         MULLE_FLAG_LOG_VERBOSE="YES"
61
+         MULLE_FLAG_LOG_EXEKUTOR="YES"
62
+      ;;
63
+
64
+      TRACE)
65
+         MULLE_FLAG_LOG_SETTINGS="YES"
66
+         MULLE_FLAG_LOG_EXEKUTOR="YES"
67
+         MULLE_FLAG_LOG_FLUFF="YES"
68
+         MULLE_FLAG_LOG_VERBOSE="YES"
69
+         bootstrap_dump_env
70
+      ;;
71
+
72
+      1848)
73
+         MULLE_FLAG_LOG_SETTINGS="YES"
74
+         MULLE_FLAG_LOG_FLUFF="YES"
75
+         MULLE_FLAG_LOG_VERBOSE="YES"
76
+         MULLE_FLAG_VERBOSE_BUILD="YES"
77
+
78
+         bootstrap_dump_env
79
+
80
+         if [ "${MULLE_TRACE_POSTPONE}" = "NO" ]
81
+         then
82
+            log_trace "1848 trace (set -x) started"
83
+            set -x
84
+            PS4="+ ${ps4string} + "
85
+         fi
86
+      ;;
87
+   esac
88
+}
89
+
90
+
91
+core_technical_option_usage()
92
+{
93
+   cat <<EOF
94
+   -n        : dry run
95
+   -s        : be silent
96
+   -v        : be verbose
97
+EOF
98
+
99
+   if [ ! -z "${MULLE_TRACE}" ]
100
+   then
101
+      cat <<EOF
102
+   -ld       : additional debug output
103
+   -le       : external command execution log output
104
+   -t        : enable shell trace
105
+   -tpwd     : emit shortened PWD during trace
106
+   -vv       : be more verbose
107
+   -vvv      : be very verbose
108
+EOF
109
+   fi
110
+}
111
+
112
+
113
+core_technical_flags()
114
+{
115
+   case "$1" in
116
+      -n|--dry-run)
117
+         MULLE_FLAG_EXEKUTOR_DRY_RUN="YES"
118
+      ;;
119
+
120
+      -ld|--log-debug)
121
+         MULLE_FLAG_LOG_DEBUG="YES"
122
+      ;;
123
+
124
+      -le|--log-execution)
125
+         MULLE_FLAG_LOG_EXEKUTOR="YES"
126
+      ;;
127
+
128
+      -t|--trace)
129
+         MULLE_TRACE="1848"
130
+         ps4string='${BASH_SOURCE[1]##*/}:${LINENO}'
131
+      ;;
132
+
133
+      -tfpwd|--trace-full-pwd)
134
+         [ "${MULLE_TRACE}" = "1848" ] || fail "option \"$1\" must be specified after -t"
135
+         ps4string='${BASH_SOURCE[1]##*/}:${LINENO} \"\w\"'
136
+      ;;
137
+
138
+      -tp|--trace-profile)
139
+         [ "${MULLE_TRACE}" = "1848" ] || fail "option \"$1\" must be specified after -t"
140
+
141
+         case "${UNAME}" in
142
+            "")
143
+               internal_fail "UNAME must be set by now"
144
+            ;;
145
+            linux)
146
+               ps4string='$(date "+%s.%N (${BASH_SOURCE[1]##*/}:${LINENO})")'
147
+            ;;
148
+            *)
149
+               ps4string='$(date "+%s (${BASH_SOURCE[1]##*/}:${LINENO})")'
150
+            ;;
151
+         esac
152
+      ;;
153
+
154
+      -tpo|--trace-postpone)
155
+         [ "${MULLE_TRACE}" = "1848" ] || fail "option \"$1\" must be specified after -t"
156
+         MULLE_TRACE_POSTPONE="YES"
157
+      ;;
158
+
159
+      -tpwd|--trace-pwd)
160
+         [ "${MULLE_TRACE}" = "1848" ] || fail "option \"$1\" must be specified after -t"
161
+         ps4string='${BASH_SOURCE[1]##*/}:${LINENO} \".../\W\"'
162
+      ;;
163
+
164
+      -tx|--trace-options)
165
+         set -x
166
+      ;;
167
+
168
+      -v|--verbose)
169
+        [ "${MULLE_TRACE}" = "1848" ] && log_warning "${MULLE_EXECUTABLE_FAIL_PREFIX}: -v after -t invalidates -t"
170
+
171
+         MULLE_TRACE="VERBOSE"
172
+      ;;
173
+
174
+      -vv|--very-verbose)
175
+        [ "${MULLE_TRACE}" = "1848" ] && log_warning "${MULLE_EXECUTABLE_FAIL_PREFIX}: -vv after -t invalidates -t"
176
+
177
+         MULLE_TRACE="FLUFF"
178
+      ;;
179
+
180
+      -vvv|--very-very-verbose)
181
+        [ "${MULLE_TRACE}" = "1848" ] && log_warning "${MULLE_EXECUTABLE_FAIL_PREFIX}: -vvv after -t invalidates -t"
182
+
183
+         MULLE_TRACE="TRACE"
184
+      ;;
185
+
186
+      -s|--silent)
187
+         MULLE_TRACE=
188
+         MULLE_FLAG_LOG_TERSE="YES"
189
+      ;;
190
+
191
+      *)
192
+         return 1
193
+      ;;
194
+   esac
195
+
196
+   return 0
197
+}
198
+
199
+
200
+
201
+core_options_initialize()
202
+{
203
+   [ -z "${MULLE_BOOTSTRAP_LOGGING_SH}" ] && . mulle-bootstrap-logging.sh
204
+}
205
+
206
+core_options_initialize
207
+
208
+: