-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnd_cp.sh
220 lines (216 loc) · 8.87 KB
/
nd_cp.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
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
#!/bin/bash
##################################################################
#
# About This Script:
# nd_cp: implements the BA/bin/bash cp command with options for local
# and/or Palantir target system;
# NOTE: requires curl, jq;
#
##################################################################
#
# NOTE: options and associated input, if any, may be passed
# in any order;
#
# Usage:
# /bin/bash nd_cp./bin/bash -ff (-from_file) "from_local_file_name.extension"
# -tf (-to_file) "to_local_file_name.extension"
# -fr (-from_rid) "from_palantir_object_rid"
# -fp (-from_path) "from_palantir_object_path"
# -tr (-to_rid) "to_palantir_object_rid"
# -tp (-to_path) "to_palantir_object_path"
# -bp (-base_path)
# -u (-user_token) "user_token"
# -d (-domain) "domain_url"
# [default:https://nidap.nih.gov]
# ............copies from_file/_path as to_file/_path; direction
# determined by input; e.g., providing -ff and -tf
# results in local copy, providing -fr,-fp, -tr, -tp,
# and -u results in Palantir copy, providing combin-
# ations produce intuitive results; i.e., providing
# -tf with -fr, -fp and -u copies from Palantir to
# local system, while -ff with -tr, -tp and -u copies
# from local system to Palantir;
#
# /bin/bash nd_cp./bin/bash -h (-help)
# ............displays function help;
#
# /bin/bash nd_cp./bin/bash -t (-test)
# ............test function;
#
##################################################################
#
# Returns:
# string: SUCCESS or FAILURE
#
##################################################################
# execute request
strCheck=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$#" -gt 1 ] && [ "$strCheck" != "-h" ] && [ "$strCheck" != "-help" ] && [ "$strCheck" != "-t" ] && [ "$strCheck" != "-test" ]
then
strFromFile=""
strToFile=""
strFromRID=""
strFromPath=""
strToRID=""
strToPath=""
strUserToken=""
strDomain=""
while [ $# -ne 0 ]
do
strArg=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$strArg" = "-ff" ] || [ "$strArg" = "-from_file" ]
then
shift
strFromFile="$1"
elif [ "$strArg" = "-tf" ] || [ "$strArg" = "-to_file" ]
then
shift
strToFile="$1"
elif [ "$strArg" = "-fr" ] || [ "$strArg" = "-from_rid" ]
then
shift
strFromRID="$1"
elif [ "$strArg" = "-fp" ] || [ "$strArg" = "-from_path" ]
then
shift
strFromPath="$1"
elif [ "$strArg" = "-tr" ] || [ "$strArg" = "-to_rid" ]
then
shift
strToRID="$1"
elif [ "$strArg" = "-tp" ] || [ "$strArg" = "-to_path" ]
then
shift
strToPath="$1"
elif [ "$strArg" = "-u" ] || [ "$strArg" = "-user_token" ]
then
shift
strUserToken="$1"
elif [ "$strArg" = "-d" ] || [ "$strArg" = "-domain" ]
then
shift
strDomain="$1"
fi
shift
done
strTimeStamp=$(date '+%Y-%m-%d %H:%M:%S')
strWorkingDirectory=$(pwd)
strTempFile="$strWorkingDirectory/temp/$strTimeStamp.___"
# local copy
if [ "$strFromFile" != "" ] && [ "$strToFile" != "" ]
then
#echo "Copying local file: [$strFromFile] to [$strToFile]...";
if cp "$strFromFile" "$strToFile"
then
echo "SUCCESS\n"
else
echo "FAILURE\n$?"
fi
# palatir copy
elif [ "$strFromRID" != "" ] && [ "$strFromPath" != "" ] && [ "$strToRID" != "" ] && [ "$strToPath" != "" ] && [ "$strUserToken" != "" ] && [ "$strDomain" != "" ]
then
#echo "Copying file: [$strFromRID]:[$strFromPath] to [$strToRID]:[$strToPath]...";
strDownloadResult=/bin/bash ./nd_pd.sh -tf "$strTempFile" -fr "$strFromRID" -fp "$strFromPath" -u "$strUserToken" -d "$strDomain"
if touch "$strTempFile"
then
strUploadResult=/bin/bash ./nd_pu.sh -ff "$strTempFile" -tr "$strToRID" -tp "$strToPath" -u "$strUserToken" -d "$strDomain"
/bin/bash ./nd_json_parser.sh "$strDownloadResult"
/bin/bash ./nd_json_parser.sh "$strUploadResult"
rm "$strTempFile"
else
echo "FAILURE\n$?"
fi
# local to palatir copy (upload without local delete)
elif [ "$strFromFile" != "" ] && [ "$strToRID" != "" ] && [ "$strToPath" != "" ] && [ "$strUserToken" != "" ] && [ "$strDomain" != "" ]
then
#echo "Copying file: [$strFromRID]:[$strFromPath] to [$strToRID]:[$strToPath]...";
strUploadResult=/bin/bash ./nd_pu.sh -ff "$strFromFile" -tr "$strToRID" -tp "$strToPath" -u "$strUserToken" -d "$strDomain"
/bin/bash ./nd_json_parser.sh "$strUploadResult"
# palatir to local copy (download without Palantir delete)
elif [ "$strToFile" != "" ] && [ "$strFromRID" != "" ] && [ "$strFromPath" != "" ] && [ "$strUserToken" != "" ] && [ "$strDomain" != "" ]
then
#echo "Copying file: [$strFromRID]:[$strFromPath] to [$strToRID]:[$strToPath]...";
strDownloadResult=/bin/bash ./nd_pd.sh -tf "$strToFile" -fr "$strFromRID" -fp "$strFromPath" -u "$strUserToken" -d "$strDomain"
/bin/bash ./nd_json_parser.sh "$strDownloadResult"
else
strError="FAILURE"
strError="$strError\nInvalid Input Set:"
strError="$strError\nLocal From File:\t$strFromFile"
strError="$strError\nLocal To File:\t$strToFile"
strError="$strError\nPalantir From RID:\t$strFromRID"
strError="$strError\nPalantir From RID:\t$strFromRID"
strError="$strError\nPalantir To RID:\t$strToRID"
strError="$strError\nPalantir To Path:\t$strToPath"
strError="$strError\nUser Token:\t$strUserToken"
strError="$strError\nDomain:\t$strDomain"
strError="$strError\n$0\n"
echo "$strError"
fi
# display help
elif [ "$#" -eq 1 ] && [ "$strCheck" = "-h" ] || [ "$strCheck" = "-help" ]
then
echo "##################################################################"
echo "#"
echo "# About This Script:"
echo "# nd_cp: implements the BA/bin/bash cp command with options for local"
echo "# and/or Palantir target system;"
echo "#"
echo "##################################################################"
echo "#"
echo "# NOTE: options and associated input, if any, may be passed"
echo "# in any order;"
echo "#"
echo "# Usage:"
echo "# /bin/bash nd_cp./bin/bash -ff (-from_file) \"from_local_file_name.extension\""
echo "# -tf (-to_file) \"to_local_file_name.extension\""
echo "# -fr (-from_rid) \"from_palantir_object_rid\""
echo "# -fp (-from_path) \"from_palantir_object_path\""
echo "# -tr (-to_rid) \"to_palantir_object_rid\""
echo "# -tp (-to_path) \"to_palantir_object_path\""
echo "# -u (-user_token) \"user_token\""
echo "# -d (-domain) \"domain_url\""
echo "# [default:https://nidap.nih.gov]"
echo "# ............copies from_file/_path as to_file/_path; direction"
echo "# determined by input; e.g., providing -ff and -tf"
echo "# results in local copy, providing -fr,-fp, -tr, -tp,"
echo "# and -u results in Palantir copy, providing combin-"
echo "# ations produce intuitive results; i.e., providing"
echo "# -tf with -fr, -fp and -u copies from Palantir to"
echo "# local system, while -ff with -tr, -tp and -u copies"
echo "# from local system to Palantir;"
echo "#"
echo "# /bin/bash nd_cp./bin/bash -h (-help)"
echo "# ............displays function help;"
echo "#"
echo "# /bin/bash nd_cp./bin/bash -t (-test)"
echo "# ............test function;"
echo "#"
echo "##################################################################"
echo "#"
echo "# Returns:"
echo "# Contents of requested folder/dataset(s)"
echo "#"
echo "##################################################################"
# test routine
elif [ "$#" -eq 1 ] && [ "$strCheck" = "-t" ] || [ "$strCheck" = "-test" ]
then
touch "test_original.___"
if test -f "test_original.___"
then
cp -i "test_original.___" "test_new.___"
if test -f "test_new.___"
then
echo "SUCCESS\n"
rm "test_original.___"
rm "test_new.___"
else
#echo "nd_cp: ERROR: failed to copy test file; verify file pathname and/or permissions."
echo "FAILURE\n$?"
rm "test_original.___"
fi
else
echo "FAILURE\n$?"
fi
else
echo "FAILURE\nUnrecognized Input\nUsage: /bin/bash nd_cp./bin/bash -h\n$0"
fi