-
Notifications
You must be signed in to change notification settings - Fork 0
/
move_data.sh
executable file
·44 lines (38 loc) · 1.3 KB
/
move_data.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
#!/bin/bash
set -Eeuo pipefail
# Get the last PK
>&2 echo "Fetching the greatest primary key..."
pk="$(curl --fail --silent \
"$URL?select=$PK_NAME&order=$PK_NAME.desc&limit=1" \
--header "Accept-Profile: $CONTENTPROFILE" \
--header "Authorization: Bearer $CONSUMER_TOKEN" \
--header "Accept: text/csv" | tail --lines=1)"
# Empty table
if [ ! -n "$pk" ]
then
>&2 echo "The table is empty."
exit
fi
# Fetch the data and convert it
>&2 echo "Fetching the data..."
curl --fail --silent \
"$URL?$PK_NAME=lte.$pk" \
--header "Accept-Profile: $CONTENTPROFILE" \
--header "Authorization: Bearer $CONSUMER_TOKEN" \
--header "Accept: application/json" |
mlr --ijson --ocsv --ofs tab --quote-none --ors $'\n' cat |
awk -F$'\t' '{OFS=FS} { print gensub(/([0-9]{4}-[0-9]{2}-[0-9]{2})T([0-9]{2}:[0-9]{2}:[0-9]{2})([+-][0-9]{2}:[0-9]{2})/, "\\1 \\2 \\3", "g") }' > exported.tsv
# Run the command
>&2 echo "Executing the program..."
"$@"
>&2 echo "Program executed successfully."
# Delete (optional)
if [ "$DELETE" = true ]
then
>&2 echo "Deleting the original data..."
curl --fail --silent --request DELETE \
"$URL?$PK_NAME=lte.$pk" \
--header "Content-Profile: $CONTENTPROFILE" \
--header "Authorization: Bearer $CONSUMER_TOKEN"
>&2 echo "Data deleted."
fi