-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwick-formula
executable file
·123 lines (103 loc) · 3.96 KB
/
wick-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
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
#!/usr/bin/env bash
# Public: List of formulas to execute. It is better to log this list
# instead of the one with arguments because arguments may contain
# sensitive information.
WICK_FORMULA_LIST=()
# Public: List of formulas with their arguments. Try to avoid logging these.
WICK_FORMULA_LIST_WITH_ARGS=()
# Public: Commands to execute in order to make the formula execute. This
# will only add an entry if there is a "run" script. The command may have
# arguments appended so try to avoid logging these.
WICK_FORMULA_COMMANDS=()
# Public: Mark a formula as required for a given role or as a dependency
# for other formulas.
#
# $1 - Formula to require
# $2-@ - Arguments for formula's run script
#
# If the formula was already marked as required, this will error as long as
# the arguments don't match and there are arguments for this invocation.
# We can't do this another way because the `depends` file would already
# have been executed with a set of arguments.
#
# Examples
#
# wickFormula test --arg=1
# wickFormula test # Will not error
# wickFormula test --blue # Errors
#
# Returns true if formula was found and added to the list.
wickFormula() {
local args file formula formulaEscaped location preservedPwd temp
formula="$1"
wickArgumentString formulaEscaped "$formula"
shift
args=""
if [[ $# -gt 0 ]]; then
wickArgumentString args "$@"
args=" $args"
fi
if [[ -z "$formula" ]]; then
echo "You must specify a formula name." >&2
return 1
fi
if ! wickFind location "formulas/$formula"; then
echo "Unable to find formula $formula" >&2
return 1
fi
if wickInArray "$formula" "${WICK_FORMULA_LIST[@]-}"; then
# Specifying the same formula is ok as long as the arguments are the same
if wickInArray "$formulaEscaped$args" "${WICK_FORMULA_LIST_WITH_ARGS[@]}"; then
echo "Formula is already in list: $formula"
return 0
fi
# You can add the formula as a dependency with no arguments when it
# was already added with arguments
if [[ "$args" == "" ]]; then
echo "Formula is already in list and has additional arguments: $formula"
return 0
fi
# Mismatched arguments OR previously did not have arguments and current
# one has arguments. Do not continue.
echo "Formula specified multiple times with different arguments: $formula" >&2
return 1
fi
# 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 "Adding formula $formula$args ($location)"
WICK_FORMULA_LIST[${#WICK_FORMULA_LIST[@]}]=$formula
WICK_FORMULA_LIST_WITH_ARGS[${#WICK_FORMULA_LIST_WITH_ARGS[@]}]=$formulaEscaped$args
if [[ -f "$location/depends" ]]; then
#: Can't run this in a subshell because subshells won't be able to
#: modify parent variables, like WICK_FORMULA_LIST.
export WICK_FORMULA_DIR=$location
preservedPwd=$PWD
# See escaping note in wickRunFormula for why we are unable to use
# . "$location/depends" $args
eval "wickStrictMode ; . \"$location/depends\" $args"
cd "$preservedPwd"
fi
if [[ -f "$location/run" ]]; then
wickArgumentString temp "$location/run"
WICK_FORMULA_COMMANDS[${#WICK_FORMULA_COMMANDS[@]}]=$temp$args
fi
if [[ -d "$location/functions" ]]; then
for file in "$location/functions/"*; do
if [[ -f "$file" ]]; then
echo "Loading function: $file"
. "$file"
fi
done
fi
if [[ -d "$location/explorers" ]]; then
for file in "$location/explorers/"*; do
if [[ -f "$file" ]]; then
echo "Running explorer: $file"
(
wickExplorer temp "$formula" "${file##*/}"
)
fi
done
fi
}