feat: more features, output formats, configs, etc

This commit is contained in:
2025-02-08 22:36:28 +02:00
parent 7c09552196
commit 01210aaebe
13 changed files with 356 additions and 155 deletions

View File

@@ -3,25 +3,34 @@ package fileproc
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/sirupsen/logrus"
)
// WriteRequest represents the content to be written.
type WriteRequest struct {
Path string
Content string
}
// ProcessFile reads the file at filePath and sends a formatted output to outCh.
// The optional wg parameter is used when the caller wants to wait on file-level processing.
func ProcessFile(filePath string, outCh chan<- WriteRequest, wg *interface{}) {
content, err := ioutil.ReadFile(filePath)
func ProcessFile(filePath string, outCh chan<- WriteRequest, rootPath string) {
content, err := os.ReadFile(filePath)
if err != nil {
logrus.Errorf("Failed to read file %s: %v", filePath, err)
return
}
// Format: separator, file path, then content.
formatted := fmt.Sprintf("\n---\n%s\n%s\n", filePath, string(content))
outCh <- WriteRequest{Content: formatted}
// Compute path relative to rootPath, so /a/b/c/d.c becomes c/d.c
relPath, err := filepath.Rel(rootPath, filePath)
if err != nil {
// Fallback if something unexpected happens
relPath = filePath
}
// Format: separator, then relative path, then content
formatted := fmt.Sprintf("\n---\n%s\n%s\n", relPath, string(content))
outCh <- WriteRequest{Path: relPath, Content: formatted}
}