/
functions
135 lines (110 loc) · 3.77 KB
/
functions
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
#!/bin/sh
__sm.templates.find()
{
typeset -g template_path template_name
typeset _extensions_path _extension _template_path _params_path
_extensions_path="$1"
(( $# )) && shift || __sm.log.fail "No extensions path and extension name given."
_params_path="$*"
_params_path="${_params_path// //}"
if __sm.string.match "/" "${_params_path}"
then
_extension="${_params_path%%/*}"
_params_path="${_params_path#*/}"
else
_extension="${extension}"
fi
_template_path="${_extensions_path}/${_extension}/templates/${_params_path}"
if [[ -f "${_template_path}.template" ]]
then
template_path="${_template_path}.template"
template_name="${_extension}/${_params_path}"
return 0
elif [[ -f "${_template_path}.mustache" ]]
then
template_path="${_template_path}.mustache"
template_name="${_extension}/${_params_path}"
return 0
elif [[ -f "${_template_path}" ]]
then
template_path="${_template_path}"
template_name="${_extension}/${_params_path}"
return 0
fi
return 1
}
__sm.templates.exists()
{
if __sm.extension.in.paths __sm.templates.find "$@" ||
__sm.cli.in.path __sm.templates.find "$@"
then
__sm.log.search "template" "$template_name" "${template_path##${sm_path}\/}"
return 0
else
return 1
fi
}
__sm.templates.install()
{
typeset _template _target _owner _mode _variable _path _append _json \
_json_file _tmp_file _target_path _data_string
typeset -a _params _data _strings
_template="${1}" ; _target="${2}" ; _owner="${3}" ; _mode="${4}" ;
_variable="${5}" ; _json="${6}" ; _json_file="${7}" ; _append="${8:-0}"
shift 8
_data=("$@")
_tmp_file=${_template}.${timestamp}.$$
_target_path="$(dirname "${_target}")"
if [[ -n "${_variable}" ]] && eval "(( ( \${#${_variable}} % 2 ) != 0 ))"
then __sm.log.fail "Cannot render template: every given variable key must have a value."
fi
__sm.templates.exists "${_template}" ||
__sm.log.fail "No template found matching '${_template}'."
[[ ! -d "${_target}" ]] ||
__sm.log.fail "Cannot install the template '${_template}' to '${_target}',"\
" which exists and is a directory."
[[ -d "${_target_path}" ]] || mkdir -p "${_target_path}"
_params+=( --template "${template_path}" --output "${_target}" )
[[ -z "${_json}" ]] || _params+=( --json "${_json}" )
[[ -z "${_json_file}" ]] || _params+=( --json "${_json_file}" )
(( _append == 0 )) || _params+=(--append)
if [[ -n "${_variable}" ]]
then eval "
for (( _index=1 ; _index <= \${#${_variable}} ; _index = _index + 2 ))
do _params+=( --data \"\${${_variable}[\${_index}]}=\${${_variable}[\$((_index+1))]}\" )
done"
fi
_data_string+="--data \""
for (( _index=1 ; _index <= ${#_data} ; _index = _index + 2 ))
do _data_string+="${_data[${_index}]}=${_data[$((_index+1))]}:~"
done
_params+=(" ${_data_string}\"" )
if ! "${sm_path}/bin/sm-mustache" "${_params[@]}"
then log error "There was an error rendering the template!"
fi
chmod "${_mode}" "${_target}"
chown "${_owner}" "${_target}"
}
# template diff {name}
# Identical to install except that instead of doing the install it will check the files for differences.
# 0 - identical
# 1 - difference ; print diff to STDERR
#
# template diff \
# {{ all of the same args that you can pass to template install }}
#
__sm.templates.diff()
{
__sm.templates.exists "${_name}" ||
__sm.log.fail "No template found matching '${_name}'."
_template="${template_path}"
_name="$(basename "${_name}")"
# Step 1. change the target,
# Step 2. do the actual install
# Step 3. then do diff of changed target vs actual
# Step 4. check mode,owner
# 1. file missing
# 2. Actual diff differences
# 3. different owner than specified (if specified)
# 4. different mode than specified (if specified)
}