-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnd_ls.sh
203 lines (191 loc) · 6.87 KB
/
nd_ls.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
#!/bin/bash
##################################################################
#
# About This Script:
# nd_ls: implements the BASH ls 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:
# sh nd_ls.sh -f (-folder) "folder_path_name_to_list"
# ............displays contents of local folder(s);
#
# sh nd_ls.sh -r (-rid) "palantir_object_rid"
# -u (-user_token) "user_token"
# -d (-domain) "domain_url"
# [default:https://nidap.nih.gov]
# ............displays contents of Palantir folder/dataset(s);
#
# sh nd_ls.sh -h (-help)
# ............displays function help;
#
# sh nd_ls.sh -t (-test)
# ............test function;
#
##################################################################
#
# Returns:
# SUCCESS or FAILURE; Contents of requested folder/dataset(s);
#
##################################################################
# temp code to fetch ttest objects/parameters
strTestFolder="."
strTestRID="ri.foundry.main.dataset.385838db-254c-4cf9-aafd-32c0f41cf90c"
strTestUserToken=`cat /mnt/c/Users/stephendh.ctr/Documents/GitHub/NIDAP_utilities/NIDAP_utilities/sdh.txt`
strTestDomain="https://nidap.nih.gov"
strDefaultDomain="https://nidap.nih.gov"
# execute request
strArg=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$#" -gt 1 ] && [ "$strArg" != "-h" ] && [ "$strArg" != "-help" ] && [ "$strArg" != "-t" ] && [ "$strArg" != "-test" ]
then
strFolder=""
strRID=""
strUserToken=""
strDomain=""
while [ $# -ne 0 ]
do
strArg=`echo "$1" | awk -va="$1" '{print tolower(a)}'`
if [ "$strArg" = "-f" ] || [ "$strArg" = "-folder" ]
then
shift
strFolder="$1"
elif [ "$strArg" = "-r" ] || [ "$strArg" = "-rid" ]
then
shift
strRID="$1"
elif [ "$strArg" = "-u" ] || [ "$strArg" = "-user_token" ]
then
shift
strUserToken="$1"
elif [ "$strArg" = "-d" ] || [ "$strArg" = "-domain" ]
then
shift
strDomain="$1"
fi
shift
done
if [ "$strDomain" = "" ]
then
strDomain="$strDefaultDomain"
fi
# local list
if [ "$strFolder" != "" ]
then
# echo "\nListing local folder(s): [$strFolder]:\n"
if ls "$strFolder" -C -al | grep "^d" && ls -la | grep -v "^d" | awk '{ print $6 "/" $7 "/" $8 "\t" $5 "\t" $9}'
then
echo "SUCCESS\n"
else
echo "FAILURE\n$?"
fi
# Palantir list
elif [ "$strRID" != "" ] && [ "$strUserToken" != "" ] && [ "$strDomain" != "" ]
then
# echo "Listing Palantir folder/dataset(s), RID: [$strRID]:"
strPost=$(curl \
-H "Authorization: Bearer $strUserToken" \
"$strDomain/api/v1/datasets/$strRID/files?preview=true")
if [ "$strPost" != "" ]
then
# Check if the JSON string contains "data" or "errorCode" and extract the corresponding values
# Use jq to check for the presence of "data" or "errorCode"
data_check=$(echo "$strPost" | jq '.data')
error_check=$(echo "$strPost" | jq '.errorCode')
if [[ "$data_check" != "null" ]]; then
result=$(echo "$strPost" | jq -r '.data[] | .path + " " + .sizeBytes')
elif [[ "$error_check" != "null" ]]; then
result=$(echo "$strPost" | jq -r '.errorCode ')
else
result="$strPost"
fi
echo "$result"
if [[ "$error_check" != "null" ]]; then
strError="$strPost"
strError="$strError\nLocal Folder:\t$strFolder"
strError="$strError\nObject RID:\t$strRID"
strError="$strError\nUser Token:\t$strUserToken"
strError="$strError\nDomain:\t$strDomain"
strError="$strError\n$0\n"
echo "$strError"
fi
else
echo "WARNING\nResponse is empty.\n$0"
fi
else
strError="FAILURE"
strError="$strError\nInvalid Input Set:"
strError="$strError\nLocal Folder:\t$strFolder"
strError="$strError\nObject RID:\t$strRID"
strError="$strError\nUser Token:\t$strUserToken"
strError="$strError\nDomain:\t$strDomain"
strError="$strError\n$0\n"
echo "$strError"
fi
# display help
elif [ "$#" -eq 1 ] && [ "$strArg" = "-h" ] || [ "$strArg" = "-help" ]
then
echo "##################################################################"
echo "#"
echo "# About This Script:"
echo "# nd_ls: implements the BASH ls 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 "# sh nd_ls.sh -f (-folder) \"folder_path_name_to_list\""
echo "# ............displays contents of local folder(s);"
echo "#"
echo "# sh nd_ls.sh -r (-rid) \"palantir_object_rid\""
echo "# -u (-user_token) \"user_token\""
echo "# -d (-domain) \"domain_url\""
echo "# [default:https://nidap.nih.gov]"
echo "# ............displays contents of Palantir folder/dataset(s);"
echo "#"
echo "# sh nd_ls.sh -h (-help)"
echo "# ............displays function help;"
echo "#"
echo "# sh nd_ls.sh -t (-test)"
echo "# ............test function;"
echo "#"
echo "##################################################################"
echo "#"
echo "# Returns:"
echo "# Contents of requested folder/dataset(s)"
echo "#"
echo "##################################################################"
# test routine
elif [ "$#" -eq 1 ] && [ "$strArg" = "-t" ] || [ "$strArg" = "-test" ]
then
# TODO: test routine
# temp code, testing
strFolder="$strTestFolder"
strRID="$strTestRID"
strUserToken="$strTestUserToken"
strDomain="$strTestDomain"
echo "$strDomain/api/v1/datasets/$strRID/files?preview=true"
strTest=$(ls "$strFolder" -C -al | grep "^d" && ls -la | grep -v "^d" | awk '{ print $6 "/" $7 "/" $8 "\t" $5 "\t" $9}')
#echo "$strTest"
if ! test -z "$strTest"
then
echo "SUCCESS\n"
echo "$strTest"
else
echo "FAILURE\n$?\n"
fi
# echo "Listing Palantir folder/dataset(s), RID: [$strRID]:"
strPost=$(curl \
-H "Authorization: Bearer $strUserToken" \
"$strDomain/api/v1/datasets/$strRID/files?preview=true")
echo sh ./nd_json_parser.sh "$strPost"
else
echo "FAIsLURE\nUnrecognized Input\nUsage: sh nd_ls.sh -h\n$0"
fi