-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bash_helpers.sh
130 lines (107 loc) · 3.5 KB
/
.bash_helpers.sh
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
#!/usr/bin/env bash
# declare global key-value pair variable
declare -g -A __COMMAND_EXTENSIONS
# to extend a command, start with a function that has the same name as the command
function add_extension() {
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case ${1} in
-c|--command-name)
COMMAND_NAME="$2"
shift
shift
;;
-s|--sub-command-name)
SUB_COMMAND_NAME="$2"
shift
shift
;;
-a|--argument-name)
ARGUMENT_NAME="$2"
shift
shift
;;
-r|--run)
RUN="$2"
shift
shift
;;
-*|--*)
echo "Unknown option ${1}"
exit 1
;;
*)
POSITIONAL_ARGS+=("${1}")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
local key=${COMMAND_NAME}
local value=${__COMMAND_EXTENSIONS[${key}]}
value="${value}$(cat <<EOF
if [[ "\${1}" == "${SUB_COMMAND_NAME}" && "\${2}" == "${ARGUMENT_NAME}" ]]; then
${RUN} "\$@"
return 0;
fi
EOF
)"
__COMMAND_EXTENSIONS["${key}"]="${value}"
}
# create a copy of the original command
# idea from https://stackoverflow.com/questions/1203583/how-do-i-rename-a-bash-function
copy_function() {
declare -F $1 > /dev/null || return 1
eval "$(echo "${2}()"; declare -f ${1} | tail -n +2)"
}
function setup_extensions() {
if [[ -d ~/.extensions ]]; then
for entry in ~/.extensions/*.sh; do
source ${entry}
FILE_NAME=${entry##*/}
COMMAND_NAME="${FILE_NAME%.*}"
COMMAND_INFO_JSON=$(_get_extension_info_json)
for (( i=0; i<$(echo "${COMMAND_INFO_JSON}" | jq ". | length"); i++ )); do
SUB_COMMAND_NAME=$(echo "${COMMAND_INFO_JSON}" | jq -r ".[${i}].SUB_COMMAND_NAME")
ARGUMENT_NAME=$(echo "${COMMAND_INFO_JSON}" | jq -r ".[${i}].ARGUMENT_NAME")
METHOD_NAME=$(echo "${COMMAND_INFO_JSON}" | jq -r ".[${i}].METHOD_NAME")
FUNCTION_NAME="$(echo ${COMMAND_NAME}_${SUB_COMMAND_NAME}_${ARGUMENT_NAME} | sed -e 's/[^a-zA-Z0-9_]//g' -e 's/ /_/g')"
copy_function "${METHOD_NAME}" ${FUNCTION_NAME}
add_extension \
--command-name "${COMMAND_NAME}" \
--sub-command-name "${SUB_COMMAND_NAME}" \
--argument-name "${ARGUMENT_NAME}" \
--run "${FUNCTION_NAME}"
done
done
fi
if [[ -d ~/.amplify/extensions ]]; then
for entry in ~/.amplify/extensions/*.sh; do
source ${entry}
FILE_NAME=${entry##*/}
COMMAND_NAME="${FILE_NAME%.*}"
COMMAND_INFO_JSON=$(_get_extension_info_json)
for (( i=0; i<$(echo "${COMMAND_INFO_JSON}" | jq ". | length"); i++ )); do
SUB_COMMAND_NAME=$(echo "${COMMAND_INFO_JSON}" | jq -r ".[${i}].SUB_COMMAND_NAME")
ARGUMENT_NAME=$(echo "${COMMAND_INFO_JSON}" | jq -r ".[${i}].ARGUMENT_NAME")
METHOD_NAME=$(echo "${COMMAND_INFO_JSON}" | jq -r ".[${i}].METHOD_NAME")
FUNCTION_NAME="$(echo ${COMMAND_NAME}_${SUB_COMMAND_NAME}_${ARGUMENT_NAME} | sed -e 's/[^a-zA-Z0-9_]//g' -e 's/ /_/g')"
copy_function "${METHOD_NAME}" ${FUNCTION_NAME}
add_extension \
--command-name "${COMMAND_NAME}" \
--sub-command-name "${SUB_COMMAND_NAME}" \
--argument-name "${ARGUMENT_NAME}" \
--run "${FUNCTION_NAME}"
done
done
fi
for COMMAND_NAME in "${!__COMMAND_EXTENSIONS[@]}"; do
eval "$(cat <<EOF
function ${COMMAND_NAME}() {
${__COMMAND_EXTENSIONS[${COMMAND_NAME}]}
command "${COMMAND_NAME}" "\${@}"
}
EOF
)"
done
}