-
Notifications
You must be signed in to change notification settings - Fork 0
/
update
executable file
·38 lines (28 loc) · 1.19 KB
/
update
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
#!/usr/bin/env bash
set -e
SOURCE_DIR="${BASH_SOURCE[0]%/*}"
TEMPLATE_FILE="${SOURCE_DIR}/template.bash"
NAMESPACE="${1:?Missing namespace argument.}"
COMMAND="${2:?Missing command argument.}"
EXECUTABLE="${3:?Missing executable argument.}"
TARGET_FILE="${4:?Missing target file argument.}"
if [[ "${COMMAND}" =~ - ]]; then
echo "command argument contains '-'"
exit 1
fi
if ! [[ -f "${TEMPLATE_FILE}" ]]; then
echo "could not find template file at '${TEMPLATE_FILE}'"
exit 1
fi
TEMPLATE_CONTENT="$(< "${TEMPLATE_FILE}")"
TEMPLATE_CONTENT="$(echo "${TEMPLATE_CONTENT}" | sed -ne '/^# completion function$/,$p')"
if ! [[ -f "${TARGET_FILE}" ]]; then
echo "could not find source file at '${TARGET_FILE}'"
exit 1
fi
TARGET_CONTENT="$(< "${TARGET_FILE}")"
TARGET_CONTENT="$(echo "${TARGET_CONTENT}" | sed -ne '1,/^# completion function$/p' | sed -e '$d')"
TEMPLATE_CONTENT="$(echo "${TEMPLATE_CONTENT}" | sed -e "s/{{bc_namespace}}/${NAMESPACE}/")"
TEMPLATE_CONTENT="$(echo "${TEMPLATE_CONTENT}" | sed -e "s/{{bc_command}}/${COMMAND}/")"
TEMPLATE_CONTENT="$(echo "${TEMPLATE_CONTENT}" | sed -e "s/{{bc_executable}}/${EXECUTABLE}/")"
echo -e "${TARGET_CONTENT}\n\n${TEMPLATE_CONTENT}" > "${TARGET_FILE}"