-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilestructure
More file actions
135 lines (121 loc) · 4.27 KB
/
Copy pathfilestructure
File metadata and controls
135 lines (121 loc) · 4.27 KB
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
#!/bin/bash
# Define usage and options message
usage() {
echo "Usage: $0 -r <folder> [-i|--ignore <folders>,...] [-g|--gitignore] [-f|--file <output_file>] [-h|--help]"
echo " Options:"
echo " -r, --root <folder>: Specify the root folder to generate the structure from (required)"
echo " -i, --ignore <folders>: Specify a comma-separated list of folders to ignore"
echo " -g, --gitignore: Add a .gitignore file and use its contents to generate the structure"
echo " -f, --file <output_file>: Specify the name of the output file (including extension)"
echo " -h, --help: Display this help message"
}
# Define variables
FOLDER=""
IGNORE_FOLDERS=""
OUTPUT_FILE=""
USE_GITIGNORE=false # Flag for gitignore
# Parse options and arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-h | --help)
usage
exit 0
;;
-r | --root)
if [ $# -lt 2 ]; then
echo "Error: Option $1 requires an argument."
usage
exit 1
fi
FOLDER="$2"
shift 2
;;
-i | --ignore)
if [ $# -lt 2 ]; then
echo "Error: Option $1 requires an argument."
usage
exit 1
fi
IGNORE_FOLDERS="$2"
shift 2
;;
-g | --gitignore)
USE_GITIGNORE=true
shift
;;
-f | --file)
if [ $# -lt 2 ]; then
echo "Error: Option $1 requires an argument."
usage
exit 1
fi
OUTPUT_FILE="$2"
shift 2
;;
-*) # Handle unknown options
echo "Error: Invalid option: $1"
usage
exit 1
;;
*) # Handle unexpected positional arguments
echo "Error: Unexpected argument: $1"
usage
exit 1
;;
esac
done
# After parsing, check if the required folder argument was provided using -r
if [ -z "$FOLDER" ]; then
echo "Error: Root folder (-r) argument is required."
usage
exit 1
fi
# Default output file if not specified
if [ -z "$OUTPUT_FILE" ]; then
OUTPUT_FILE="structure.txt" # Or some default
fi
# Handle gitignore option (using the flag set during parsing)
if [ "$USE_GITIGNORE" = true ]; then
# The original script's logic here was incorrect.
# A correct implementation would involve parsing .gitignore and filtering `find`.
# For simplicity, let's just add .git and .gitignore file itself to the ignore list
# if the flag is used, as a simplified interpretation.
echo "Warning: --gitignore flag is partially implemented. Only '.git' and '.gitignore' are added to ignore list."
if [ -n "$IGNORE_FOLDERS" ]; then
IGNORE_FOLDERS="$IGNORE_FOLDERS,.git,.gitignore"
else
IGNORE_FOLDERS=".git,.gitignore"
fi
fi
# Build the find command with ignores
# Use -path ... -prune -o to correctly ignore directories and their contents.
FIND_CMD="find \"$FOLDER\""
IGNORE_PATTERNS_FIND=""
# Build the find ignore patterns string from the combined ignore list
if [ -n "$IGNORE_FOLDERS" ]; then
IFS=',' read -ra ADDR <<< "$IGNORE_FOLDERS"
for i in "${ADDR[@]}"; do
folder_to_ignore=$(echo "$i" | xargs) # Trim whitespace
if [ -n "$folder_to_ignore" ]; then
# Pattern to ignore the folder and its contents relative to the base folder
# Escape special characters in folder_to_ignore for the shell command string.
escaped_folder=$(printf %q "$folder_to_ignore")
if [ -z "$IGNORE_PATTERNS_FIND" ]; then
IGNORE_PATTERNS_FIND="-path \"$FOLDER/$escaped_folder\""
else
IGNORE_PATTERNS_FIND="$IGNORE_PATTERNS_FIND -o -path \"$FOLDER/$escaped_folder\""
fi
fi
done
fi
# Construct the final find command
FIND_CMD="find \"$FOLDER\""
if [ -n "$IGNORE_PATTERNS_FIND" ]; then
# Add the ignore patterns group and prune action
FIND_CMD="$FIND_CMD \( $IGNORE_PATTERNS_FIND \) -prune -o"
fi
# Add the print action for everything not pruned
FIND_CMD="$FIND_CMD -print"
# Execute the command
# echo "Executing: $FIND_CMD" # Debugging
eval $FIND_CMD > "$OUTPUT_FILE" 2> /dev/null