-
Notifications
You must be signed in to change notification settings - Fork 30
feat: \i directive for folder #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import ( | |
| "os" | ||
| "path/filepath" | ||
| "regexp" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
|
|
@@ -83,19 +84,28 @@ func (p *Processor) processIncludes(content string, currentDir string) (string, | |
| if matches != nil { | ||
| // Found an include directive | ||
| includePath := matches[1] | ||
|
|
||
| // Resolve the include path | ||
| resolvedPath, err := p.resolveIncludePath(includePath, currentDir) | ||
| resolvedPath, isFolder, err := p.resolveIncludePath(includePath, currentDir) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to resolve include path %s: %w", includePath, err) | ||
| } | ||
|
|
||
| // Process the included file recursively | ||
| includedContent, err := p.processFileRecursive(resolvedPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to process included file %s: %w", resolvedPath, err) | ||
|
|
||
| var includedContent string | ||
| if isFolder { | ||
| // Process the folder recursively | ||
| includedContent, err = p.processFolderRecursive(resolvedPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to process included folder %s: %w", resolvedPath, err) | ||
| } | ||
| } else { | ||
| // Process the included file recursively | ||
| includedContent, err = p.processFileRecursive(resolvedPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to process included file %s: %w", resolvedPath, err) | ||
| } | ||
| } | ||
|
|
||
| // Split included content into lines and add them | ||
| includedLines := strings.Split(includedContent, "\n") | ||
| // Remove the last empty line if the content ends with \n | ||
|
|
@@ -114,40 +124,108 @@ func (p *Processor) processIncludes(content string, currentDir string) (string, | |
|
|
||
| // resolveIncludePath resolves an include path relative to the current directory | ||
| // Only allows files within the base directory and its subdirectories | ||
| func (p *Processor) resolveIncludePath(includePath string, currentDir string) (string, error) { | ||
| // Returns the resolved path and a flag indicating if it's a folder | ||
| func (p *Processor) resolveIncludePath(includePath string, currentDir string) (string, bool, error) { | ||
| // Check if this is a folder path (ends with /) | ||
| isFolder := strings.HasSuffix(includePath, "/") | ||
|
|
||
| // Clean the path to remove any . or .. components | ||
| cleanPath := filepath.Clean(includePath) | ||
|
|
||
| // Check for directory traversal attempts | ||
| if strings.Contains(cleanPath, "..") { | ||
| return "", fmt.Errorf("directory traversal not allowed: %s", includePath) | ||
| return "", false, fmt.Errorf("directory traversal not allowed: %s", includePath) | ||
| } | ||
|
|
||
| // Resolve relative to current directory | ||
| resolvedPath := filepath.Join(currentDir, cleanPath) | ||
|
|
||
| // Get absolute path | ||
| absPath, err := filepath.Abs(resolvedPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get absolute path: %w", err) | ||
| return "", false, fmt.Errorf("failed to get absolute path: %w", err) | ||
| } | ||
|
|
||
| // Ensure the resolved path is within the base directory | ||
| baseAbs, err := filepath.Abs(p.baseDir) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get absolute base path: %w", err) | ||
| return "", false, fmt.Errorf("failed to get absolute base path: %w", err) | ||
| } | ||
|
|
||
| // Check if the resolved path is within the base directory | ||
| relPath, err := filepath.Rel(baseAbs, absPath) | ||
| if err != nil || strings.HasPrefix(relPath, "..") { | ||
| return "", fmt.Errorf("include path %s is outside the base directory %s", includePath, p.baseDir) | ||
| return "", false, fmt.Errorf("include path %s is outside the base directory %s", includePath, p.baseDir) | ||
| } | ||
|
|
||
| // Check if file exists | ||
| if _, err := os.Stat(absPath); os.IsNotExist(err) { | ||
| return "", fmt.Errorf("included file does not exist: %s", absPath) | ||
|
|
||
| // Check if path exists | ||
| stat, err := os.Stat(absPath) | ||
| if os.IsNotExist(err) { | ||
| if isFolder { | ||
| return "", false, fmt.Errorf("included folder does not exist: %s", absPath) | ||
| } else { | ||
| return "", false, fmt.Errorf("included file does not exist: %s", absPath) | ||
| } | ||
| } | ||
|
|
||
| return absPath, nil | ||
| if err != nil { | ||
| return "", false, fmt.Errorf("failed to stat path %s: %w", absPath, err) | ||
| } | ||
|
|
||
| // Validate that the path type matches the expectation | ||
| if isFolder && !stat.IsDir() { | ||
| return "", false, fmt.Errorf("expected folder but found file: %s", absPath) | ||
| } | ||
| if !isFolder && stat.IsDir() { | ||
| return "", false, fmt.Errorf("expected file but found folder: %s (use %s/ for folder includes)", absPath, includePath) | ||
| } | ||
|
|
||
| return absPath, isFolder, nil | ||
| } | ||
|
|
||
| // processFolderRecursive processes all .sql files in a folder using DFS | ||
| func (p *Processor) processFolderRecursive(folderPath string) (string, error) { | ||
| // Read directory contents | ||
| entries, err := os.ReadDir(folderPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to read directory %s: %w", folderPath, err) | ||
| } | ||
|
|
||
| // Sort entries alphabetically (natural filename order) | ||
| sort.Slice(entries, func(i, j int) bool { | ||
| return entries[i].Name() < entries[j].Name() | ||
| }) | ||
|
|
||
| var resultParts []string | ||
|
|
||
| // Process each entry in alphabetical order | ||
| for _, entry := range entries { | ||
| entryPath := filepath.Join(folderPath, entry.Name()) | ||
|
|
||
| if entry.IsDir() { | ||
| // Recursively process subdirectory (DFS) | ||
| subFolderContent, err := p.processFolderRecursive(entryPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to process subdirectory %s: %w", entryPath, err) | ||
| } | ||
| if subFolderContent != "" { | ||
| resultParts = append(resultParts, subFolderContent) | ||
| } | ||
| } else if strings.HasSuffix(entry.Name(), ".sql") { | ||
| // Process .sql file | ||
| fileContent, err := p.processFileRecursive(entryPath) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to process file %s: %w", entryPath, err) | ||
| } | ||
| if fileContent != "" { | ||
| // Ensure the file content ends with a newline for proper concatenation | ||
| if !strings.HasSuffix(fileContent, "\n") { | ||
| fileContent += "\n" | ||
| } | ||
|
Comment on lines
+220
to
+223
|
||
| resultParts = append(resultParts, fileContent) | ||
| } | ||
| } | ||
| // Ignore non-.sql files | ||
| } | ||
|
|
||
| return strings.Join(resultParts, ""), nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider trimming the trailing slash from includePath when isFolder is true to avoid potential path resolution issues. The current implementation may cause problems when constructing file paths later.