-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode-unpackager
executable file
·122 lines (106 loc) · 3.54 KB
/
code-unpackager
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
#!/bin/bash
# Version information
VERSION="0.3.1"
# Function to display help
show_help() {
echo "Usage: $0 -j <json_file> -d <destination_directory> [options]"
echo ""
echo "Options:"
echo " -j <json_file> Path to the JSON file generated by code-packager."
echo " -d <destination_dir> Path to the directory where the folder structure should be restored."
echo " -s, --silent Skip confirmation before restoration."
echo " -v, --version Display version information and exit."
echo " -h, --help Display this help and exit."
echo ""
}
# Function to display version
show_version() {
echo "Code Unpackager for Language Models - Version $VERSION"
}
# Function to check for required dependencies
check_dependencies() {
local dependencies=("jq")
local missing_deps=0
for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &> /dev/null; then
echo "Error: Required dependency '$dep' is not installed."
missing_deps=1
fi
done
if [ "$missing_deps" -ne 0 ]; then
echo "Please install the missing dependencies and try again."
exit 1
fi
}
# Check dependencies before proceeding
check_dependencies
# Parse command line arguments
SILENT=false
while getopts "j:d:svh-" opt; do
case $opt in
j) JSON_FILE="${OPTARG}" ;;
d) DESTINATION_DIRECTORY="${OPTARG}" ;;
s) SILENT=true ;;
v) show_version
exit 0 ;;
h) show_help
exit 0 ;;
-) case "${OPTARG}" in
version) show_version
exit 0 ;;
help) show_help
exit 0 ;;
silent) SILENT=true ;;
*) echo "Error: Invalid option -${OPTARG}. Use -h or --help for usage information." >&2
exit 1 ;;
esac ;;
esac
done
# Check if required parameters are provided
if [ -z "$JSON_FILE" ] || [ -z "$DESTINATION_DIRECTORY" ]; then
echo "Error: Both JSON file path and destination directory are required."
show_help
exit 1
fi
# Check if JSON file exists
if [ ! -f "$JSON_FILE" ]; then
echo "Error: JSON file '$JSON_FILE' does not exist."
exit 1
fi
# Validate JSON structure
if ! jq -e '.files | length > 0' "$JSON_FILE" > /dev/null; then
echo "Error: JSON structure is invalid or does not contain any files."
exit 1
fi
# Confirm restoration unless silent mode is enabled
if [ "$SILENT" = false ]; then
read -p "Are you sure you want to restore the folder structure to '$DESTINATION_DIRECTORY'? (y/n): " confirm
if [ "$confirm" != "y" ]; then
echo "Operation cancelled."
exit 0
fi
fi
# Create destination directory if it doesn't exist
mkdir -p "$DESTINATION_DIRECTORY"
# Iterate through the files in the JSON file
jq -c '.files[]' "$JSON_FILE" | while read -r file; do
filename=$(echo "$file" | jq -r '.filename')
content=$(echo "$file" | jq -r '.content')
path=$(echo "$file" | jq -r '.path')
# Validate each file entry
if [ -z "$filename" ] || [ -z "$path" ]; then
echo "Error: Invalid file entry in JSON. Missing 'filename' or 'path'."
exit 1
fi
# Create the directory structure
full_path="$DESTINATION_DIRECTORY$path"
mkdir -p "$full_path"
# Write the file content
if [ "$content" != "null" ]; then
echo "$content" > "$full_path/$filename"
else
# Create an empty file for binary files (content is null)
touch "$full_path/$filename"
fi
done
echo "Folder structure restored to: $DESTINATION_DIRECTORY"