-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.sh
executable file
·93 lines (81 loc) · 2.57 KB
/
import.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
#!/bin/bash
# Requires 'jq'
# Run `brew install jq` on mac to install
# Load environment variables from .env file, skipping lines starting with #
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
fi
# Check for necessary environment variables
if [ -z "$ROOT_URL" ]; then
echo "Missing ROOT_URL in .env"
exit 1
fi
if [ -z "$IMPORT_API_TOKEN" ]; then
echo "Missing IMPORT_API_TOKEN in .env"
exit 1
fi
API_URL="$ROOT_URL/api/v1/documents"
AUTH_TOKEN="$IMPORT_API_TOKEN"
# Function to display usage
usage() {
echo "Usage: $0 -l LIBRARY_ID -d DIRECTORY"
echo " -l Library id which contains documents (required)"
echo " -d Directory path of docs to import (required)"
exit 1
}
# Parse command line options
while getopts ":l:d:" opt; do
case ${opt} in
l )
LIBRARY_ID=$OPTARG
;;
d )
DIRECTORY_PATH=$OPTARG
;;
\? )
usage
;;
esac
done
# Check if all required options are present
if [ -z "$LIBRARY_ID" ] || [ -z "$DIRECTORY_PATH" ]; then
usage
fi
# Function to process files in directory
process_directory() {
local directory_path="$1"
local library_id="$2"
local api_url="$3"
local auth_token="$4"
for file_path in "$directory_path"/*; do
relative_path=${file_path#$directory_path/} # Remove directory path from file path
if [ -d "$file_path" ]; then
# Recursively process subdirectories
process_directory "$file_path" "$library_id" "$api_url" "$auth_token"
elif [[ $relative_path == *.md || $relative_path == *.txt ]]; then
echo -e "# Processing: $relative_path"
local file_content=$(<"$file_path")
local external_id=$relative_path
local title=$(basename "$relative_path")
local data_json=$(jq -n \
--arg doc "$file_content" \
--arg title "$title" \
--arg externalId "$external_id" \
--arg libraryId "$library_id" \
'{document: {document: $doc, title: $title, external_id: $externalId, library_id: $libraryId}}')
# Send the POST request to create the new document
response=$(curl -s -o /dev/null -w "%{http_code}" -X POST "$api_url" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $auth_token" \
-d "$data_json")
# Check the response status code
if [ "$response" -eq 201 ]; then
echo "Document '$relative_path' uploaded successfully."
else
echo "Failed to create document '$relative_path'. Response status code: $response"
fi
fi
done
}
# Start processing the directory
process_directory "$DIRECTORY_PATH" "$LIBRARY_ID" "$API_URL" "$AUTH_TOKEN"