-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwick-run-formula
executable file
·71 lines (60 loc) · 2.49 KB
/
wick-run-formula
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
#!/usr/bin/env bash
# Public: This exists so arguments passed to wickRun are not passed to
# formulas.
#
# $1 - Formula command to execute, including arguments. Already escaped.
# $2 - Number of the formula, for logging purposes.
#
# *Escaping note:* Formulas that are passed in are already quoted and must not
# be quoted again if you want this to run properly. See `wickFormula` for
# quoting.
#
# Sets `WICK_FORMULA_DIR` to the directory of the currently executing formula.
# Sets `WICK_FORMULA_NAME` to just the name of the currently executing
# formula.
#
# Returns the status code from the formula.
wickRunFormula() {
local formula formulaNumber result WICK_FORMULA_DIR WICK_FORMULA_NAME
formula=$1
formulaNumber=$2
shift 2
# Only use echo - do not log this to the file because the arguments
# may contain secret keys. Logging just the formula's name would be
# acceptable.
echo "Executing formula $formulaNumber: $formula"
# See escaping note above
eval "WICK_FORMULA_DIR=($formula)"
export WICK_FORMULA_DIR=${WICK_FORMULA_DIR[0]%/*}
export WICK_FORMULA_NAME=${WICK_FORMULA_DIR##*/}
# This one is safe to log because arguments have been removed.
wickDebug "Executing formula $formulaNumber (arguments not shown): $WICK_FORMULA_NAME"
# See escaping note in function header
# Can't use `. $formula` by itself because parameters get
# double-escaped.
#
# arg1() { printf "%q" "$1"; }
# arg1 a\{b # prints "a\{b"
# test="arg1 a\{b"
# $test # prints "a\\\{b"
#
# Also, careful with the new behavior of Bash. See
# doc/contexts-that-disable-exit-on-error.md. `wickStrictRun` will
# execute the formula in a subshell, so it will not be able to affect
# anything permanently, preventing it from adjusting the PATH as well
# as disallowing any other environment changes.
wickStrictRun result eval ". $formula"
# This one is safe to log because arguments have been removed.
wickDebug "Done running formula $formulaNumber (result $result): $WICK_FORMULA_NAME"
if [[ "$result" -ne 0 ]]; then
# The formula name is safe to log.
wickError "FAILURE DETECTED: $WICK_FORMULA_NAME"
# Only use echo when using $formula because the arguments may contain
# secret keys.
echo "Formula failure: $formula"
return 1
fi
# Only use echo - do not log this to the file because the arguments
# may contain secret keys.
echo "Formula success: $formula"
}