forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-property-do
executable file
·155 lines (124 loc) · 4.57 KB
/
adpn-property-do
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
#
# adpn-property-do: script to handle adpn property commands
#
# @version 2021.0910
SCRIPTPATH="$(which "$0")"
SCRIPTPATH="$( readlink --canonicalize "${SCRIPTPATH}" )"
SCRIPTDIR="$(dirname "${SCRIPTPATH}")"
SCRIPT="$(basename "${SCRIPTPATH}")"
source "${SCRIPTDIR}/adpn-define-aliases"
__USAGE__="Usage: ${SCRIPT} [--version] [--help] <CMD> [<ARGS>]"
__HELP__="[${SCRIPT}] Try '${SCRIPT_CMD_NAME} help' for more information."
__DOC__="""${__USAGE__}
Example: ${SCRIPT} get 'peer'
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
get Retrieve the value of a chosen property from adpn settings
set Assign a new value to a chosen property in adpn settings
undo Undo the most recent reassignment of a property value, if backup file still exists
list List the currently assigned adpn settings with their values
Exit codes:
0 = success (successful operation and expected result)
1-254 = error in executing command
255 = command not supported
"""
##########################################################################################
### COMMAND LINE: loop through switches ##################################################
##########################################################################################
declare -a _CMDLINE ; _CMDLINE=("$0")
declare -a SWITCHFILES ; SWITCHFILES=()
if [[ -r "${CONFFILE}" ]] ; then
SWITCHFILES+=(${CONFFILE})
fi
CMDLINETXT=$(mktemp)
until [[ "$#" -lt 1 ]] ; do
_CMDLINE+=("$1")
printf "%s\n" "$1" >> "${CMDLINETXT}"
shift
done
SWITCHFILES+=(${CMDLINETXT})
adpn_command_line "${SWITCHFILES[@]}"
rm "${CMDLINETXT}"
##########################################################################################
### SCRIPT: DETERMINE COMMAND, THEN EXECUTE PIPELINE ####################################
##########################################################################################
EXITCODE=0
CMD="${_ARGV[1]}"
adpn_set_display_settings # V, VV, Q, QQ, DBG, DBGLEVEL, DDBG, SCRIPT_CMD_NAME / @see adpn-define-aliases
adpn_script_handle_version_or_help
if [[ -n "${_PARAM[remote]}" && -z "${_PARAM[no-remote]}" ]] ; then
adpn_script_handle_remote_execution -t "property" "${_CMDLINE[@]:1}"
EXITCODE="$?"
else
case "${CMD}" in
""|"list")
adpnprop
printf "\n"
;;
"get")
adpnprop "${_ARGV[2]}"
EXITCODE=$?
;;
"set")
declare -a WORDS
WORDS=( "${_ARGV[@]:2}" )
if [[ -n "${_PARAM[key]}" ]] ; then
KEY="${_PARAM[key]}"
elif [[ -n "${WORDS[0]}" ]] ; then
KEY="${WORDS[0]}"
WORDS=( "${WORDS[@]:1}" )
fi
if [[ -n "${_PARAM[value]}" ]] ; then
VALUE="${_PARAM[value]}"
elif [[ -n "${WORDS[0]}" ]] ; then
VALUE="${WORDS[0]}"
WORDS=( "${WORDS[@]:1}" )
fi
APS_OUTPUT=$(mktemp)
JSON_KEYVALUE="$( adpn-json.py --key="${KEY}" --value="${VALUE}" )"
JSON_OTHERS="$( adpnprop --output=application/json )"
printf "%s\n%s\n" "${JSON_OTHERS}" "${JSON_KEYVALUE}" | adpn-json.py --output="application/json;prettyprint" --cascade > "${APS_OUTPUT}"
printf "\n" >> "${APS_OUTPUT}"
JSONORIGINAL="$( readlink -f "${JSONCONFFILE}" )"
JSONBACKUP="$( printf "%s~" "${JSONORIGINAL}" )"
if [[ -r "${JSONORIGINAL}" ]] ; then
cp "${JSONORIGINAL}" "${JSONBACKUP}"
else
printf "{}\n" > "${JSONBACKUP}"
fi
mv "${APS_OUTPUT}" "${JSONORIGINAL}"
if [[ -z "${_PARAM[quiet]}" ]] ; then
declare -a DIFF_SW=( "-u" )
( diff --color /dev/null /dev/null 2>/dev/null ) && DIFF_SW+=( "--color" )
diff "${DIFF_SW[@]}" "${JSONBACKUP}" "${JSONORIGINAL}"
fi
EXITCODE=0
;;
"undo")
JSON_CURRENT="$( readlink -f "${JSONCONFFILE}" )"
JSON_BACKUP="$( printf '%s~' "${JSON_CURRENT}" )"
JSON_TEMP="$( mktemp )"
[[ -r "${JSON_BACKUP}" ]] && mv "${JSON_CURRENT}" "${JSON_TEMP}" && mv "${JSON_BACKUP}" "${JSON_CURRENT}" && mv "${JSON_TEMP}" "${JSON_BACKUP}"
if [[ -z "${_PARAM[quiet]}" ]] ; then
declare -a DIFF_SW=( "-u" )
( diff --color /dev/null /dev/null 2>/dev/null ) && DIFF_SW+=( "--color" )
diff "${DIFF_SW[@]}" "${JSON_BACKUP}" "${JSON_CURRENT}"
fi
[[ -r "${JSON_TEMP}" ]] && rm "${JSON_TEMP}"
;;
"version"|"help")
EXITCODE=0
;;
*)
printf "[%s:%d] '%s' command not understood.\n" "${SCRIPT_CMD_NAME}" "${LINENO}" "${SUBCMD}" 1>&2
printf "${__HELP__}" 1>&2
EXITCODE=255
;;
esac
fi
##########################################################################################
### CLEANUP: exit with settled exit code. ################################################
##########################################################################################
exit ${EXITCODE}