forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-workflow-aliases
66 lines (56 loc) · 2.28 KB
/
adpn-workflow-aliases
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
#!/bin/bash
#
# adpn-workflow-aliases: Define some common subroutines for getting data in and out from adpn-cli workflows
[[ -r "${_ADPN_CONF_DIR}" ]] && [[ -r "${_ADPN_CONF_DIR}/steps.tsv" ]] && _ADPN_STEPS_FILE="${_ADPN_CONF_DIR}/steps.tsv" || _ADPN_STEPS_FILE=""
function adpn_workflow_get_steps() {
local result=255
cat "${_ADPN_STEPS_FILE}" ; result="$?"
return "${result}"
}
function adpn_workflow_get_step() {
local result=255
local lineno="$1" ; shift
local LINES=""
local LINE=""
local FLAGS SW
if [[ "${lineno}" =~ ^[0-9]$ ]] ; then
LINES="$( adpn_workflow_get_steps | tail -n "+${lineno}" )" ; result="$?"
elif [[ "${lineno}" =~ ^-([A-Za-z]+: .*)$ ]] ; then
LINES="$( adpn_workflow_get_steps | grep -F -i -- $'\t'"${lineno}" )"
else
LINES="$( adpn_workflow_get_steps | grep -E "^${lineno}\b" )" ; result="$?"
fi
LINE="$( printf "%s\n" "${LINES}" | head -n 1 )"
local -a _PIPE_SW=() _CMD_SW=()
if [[ -n "${LINE}" ]] ; then
CMD="$( printf "%s\n" "${LINE}" | cut -sf 2 | sed -E 's/(^\s+|\s+$)//g' )"
SW="$( printf "%s\n" "${LINE}" | cut -sf 3 | sed -E 's/(^\s+|\s+$)//g' )"
TITLE="$( printf "%s\n" "${LINE}" | cut -sf 4 | sed -E 's/(^\s+|\s+$)//g' )"
MESSAGE="$( printf "%s\n" "${LINE}" | cut -sf 5 | sed -E 's/(^\s+|\s+$)//g' )"
[[ -z "${SW}" || "${SW}" =~ ^--$ ]] || _CMD_SW+=( "${SW}" )
_PIPE_SW+=( "$( printf -- "--label=%q" "${TITLE}" )" )
_PIPE_SW+=( "$( printf -- "--in-order-to=%q" "${MESSAGE}" )" )
SW=""
for FIELD in 6 7 ; do
FLAGS="$( printf "%s\n" "${LINE}" | cut -sf "${FIELD}" | sed -E 's/(^\s+|\s+$)//g' )"
if [[ -n "${FLAGS}" ]] ; then
if [[ "${FLAGS}" =~ ^[+](.*)$ ]] ; then
FLAGS="${BASH_REMATCH[1]}"
SW="--add_labels=%q"
elif [[ "${FLAGS}" =~ ^[-](.*)$ ]] ; then
FLAGS="${BASH_REMATCH[1]}"
SW="--remove_labels=%q"
else
SW="--add_labels=%q"
fi
_PIPE_SW+=( "$( printf -- "${SW}" "${FLAGS}" )" )
fi
done
if [[ "${CMD}" =~ ^([^&]+)(&&)([^&]+)$ ]] ; then
printf "%s %s %s && %s %s %s %s %s\n" "${ADPN}" "${BASH_REMATCH[1]}" "$( printf "%q " "$@")" "${ADPN}" "${BASH_REMATCH[3]}" "${_PIPE_SW[*]}" "${_CMD_SW[*]}" "$( printf "%q " "$@")"
else
printf "%s pipe %s -- %s %s %s\n" "${ADPN}" "${_PIPE_SW[*]}" "${CMD}" "${_CMD_SW[*]}" "$(printf "%q " "$@" )"
fi
fi
return "${result}"
}