forked from Cross-PLN-Technical-Working-Group/adpn-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadpn-do-publisher
executable file
·234 lines (184 loc) · 7.2 KB
/
adpn-do-publisher
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/bin/bash
#
# adpn-do-publisher: script to handle adpn publisher commands
#
# @version 2021.0302
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} help' for more information."
__DOC__="""${__USAGE__}
--version Display the version of the script
--help Display these usage notes
The most commonly used ${SCRIPT} commands are:
list list the publisher codes in config server database
add [<AU_PUB_ID>] [<PUBLISHER_NAME>] add a publisher code for a new content source
remove [<AU_PUB_ID>] remove a publisher code
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")
shopt -s lastpipe
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 "publisher" "${_CMDLINE[@]:1}"
EXITCODE="$?"
else
case "${CMD}" in
""|"list")
# @method adpn publisher list
# Display a list of available publishers with their au_pub_id codes.
#
# Usage: adpn publisher list
#
# @version 2021.0302
if [[ -z "${CMD}" ]] ; then
CMD="list"
fi
"${SCRIPTPATH}" sql --execute="SELECT * FROM au_publishers"
EXITCODE="$?"
;;
"add")
# @method adpn publisher add
# Add a new publisher to the list of AU publishers on the props server.
#
# Usage: adpn publisher add [<AU_PUB_ID>|--id=<AU_PUB_ID>] [--name=<PUBLISHER_NAME>] [<OPTIONS>]...
#
# AU_PUB_ID is a unique alphanumeric code for the publisher (e.g.: "AUB", "ADAH")
# PUBLISHER_NAME is a human-readable institution name (e.g.: "Auburn University")
#
# If either is not provided on the command-line, the script will prompt for values
# interactively from standard input.
#
# Options:
# --pub_name=<AUS_TITLE> human-readable label for all AUs from this publisher (ex.: All Auburn University AUs)
# --pub_class=<PUB_CLASS> au_pub_class. This defaults to, and typically should remain, "xpath".
# --pub_xpath=<XPATH> an XPath expression for au_pub_xpath
#
# If any options are omitted, a sensible default will be used based on the PUBLISHER_NAME.
#
# @version 2021.0302
declare -a WORDS
WORDS=( "${_ARGV[@]:2}" )
ARG_ID="${_PARAM[id]}"
if [[ -z "${ARG_ID}" ]] ; then
ARG_ID="${WORDS[0]}"
WORDS=( "${WORDS[@]:1}" )
fi
ARG_NAME="${_PARAM[name]}"
if [[ -z "${ARG_NAME}" ]] ; then
ARG_NAME="${WORDS[0]}"
WORDS=( "${WORDS[@]:1}" )
fi
if [[ -z "${ARG_ID}" ]] ; then
read -p "Publisher ID code (e.g.: AUB, ADAH): " ARG_ID
fi
if [[ -z "${ARG_NAME}" ]] ; then
read -p "Institution Name (e.g.: Alabama Department of Archives and History): " ARG_NAME
fi
ARG_PUB_NAME="${_PARAM[pub_name]}"
if [[ -z "${ARG_PUB_NAME}" ]] ; then
ARG_PUB_NAME="$( printf "All %s AUs" "${ARG_NAME}" )"
fi
ARG_PUB_CLASS="${_PARAM[pub_class]}"
if [[ -z "${ARG_PUB_CLASS}" ]] ; then
ARG_PUB_CLASS="xpath"
fi
ARG_PUB_XPATH="${_PARAM[pub_xpath]}"
if [[ -z "${ARG_PUB_XPATH}" ]] ; then
ARG_PUB_XPATH="$( printf "[attributes/publisher='%s']" "${ARG_NAME}" )"
fi
SQL_PREPARE=$( printf "PREPARE stmt1 FROM '%s';" "INSERT INTO au_publishers (au_pub_id, au_pub, au_pub_name, au_pub_class, au_pub_xpath) VALUES (?, ?, ?, ?, ?)" )
SQL_SET=$( printf 'SET @V_PUB_ID="%s", @V_PUB="%s", @V_PUB_NAME="%s", @V_PUB_CLASS="%s", @V_PUB_XPATH="%s"' "${ARG_ID}" "${ARG_NAME}" "${ARG_PUB_NAME}" "${ARG_PUB_CLASS}" "${ARG_PUB_XPATH}" )
SQL_EXECUTE=$( printf 'EXECUTE stmt1 USING @V_PUB_ID, @V_PUB, @V_PUB_NAME, @V_PUB_CLASS, @V_PUB_XPATH' )
SQL_DEALLOCATE=$( printf 'DEALLOCATE PREPARE stmt1' )
SQL_SCRIPT=$( printf "%s; %s; %s; %s;" "${SQL_PREPARE}" "${SQL_SET}" "${SQL_EXECUTE}" "${SQL_DEALLOCATE}" )
"${SCRIPTPATH}" sql --execute="${SQL_SCRIPT}"
EXITCODE="$?"
;;
"remove")
# @method adpn publisher remove
# Remove a publisher from the list of AU publishers on the props server.
#
# Usage: adpn publisher remove [<AU_PUB_ID>|--id=<AU_PUB_ID>]
#
# AU_PUB_ID is a unique alphanumeric code for the publisher (e.g.: "FOO", "BAR")
#
# If not provided on the command-line, the script will prompt for values
# interactively from standard input.
#
# @version 2021.0405
declare -a WORDS
WORDS=( "${_ARGV[@]:2}" )
ARG_ID="${_PARAM[id]}"
if [[ -z "${ARG_ID}" ]] ; then
ARG_ID="${WORDS[0]}"
WORDS=( "${WORDS[@]:1}" )
fi
if [[ -z "${ARG_ID}" ]] ; then
read -p "Publisher ID code (e.g.: AUB, ADAH): " ARG_ID
fi
SQL_PREPARE=$( printf "PREPARE stmt1 FROM '%s';" "DELETE FROM au_publishers WHERE au_pub_id=?" )
SQL_SET=$( printf 'SET @V_PUB_ID="%s"' "${ARG_ID}" )
SQL_EXECUTE=$( printf 'EXECUTE stmt1 USING @V_PUB_ID' )
SQL_DEALLOCATE=$( printf 'DEALLOCATE PREPARE stmt1' )
SQL_SCRIPT=$( printf "%s; %s; %s; %s;" "${SQL_PREPARE}" "${SQL_SET}" "${SQL_EXECUTE}" "${SQL_DEALLOCATE}" )
"${SCRIPTPATH}" sql --execute="${SQL_SCRIPT}"
EXITCODE="$?"
;;
"sql")
MYSQL_HOST="$( adpn_parameter_from "--mysql-host" ".mysql/host" )"
MYSQL_DB="$( adpn_parameter_from "--mysql-db" ".mysql/db" )"
MYSQL_USER="$( adpn_parameter_from "--mysql-user" ".mysql/user" )"
MYSQL_PASSWORD="$( adpn_parameter_from "--mysql-password" ".mysql/password" )"
if [[ -z "${MYSQL_PASSWORD}" ]] ; then
MYSQL_PASSWORD_SWITCH=""
elif [[ "${MYSQL_PASSWORD}" == "-" ]] ; then
MYSQL_PASSWORD_SWITCH="--password"
else
MYSQL_PASSWORD_SWITCH=$(printf "%s--password=%s" "" "${MYSQL_PASSWORD}")
fi
mysql --user="${MYSQL_USER}" ${MYSQL_PASSWORD_SWITCH} --database="${MYSQL_DB}" --host="${MYSQL_HOST}" "${_SWITCHES[execute]}"
EXITCODE="$?"
;;
"version"|"help")
EXITCODE=0
;;
*)
echo "[${SCRIPT_CMD_NAME}] '${CMD}' command not understood." 1>&2
echo "${__HELP__}" 1>&2
EXITCODE=255
;;
esac
fi
##########################################################################################
### CLEANUP: remove temporary output file. ###############################################
##########################################################################################
exit ${EXITCODE}