mirror of
https://github.com/ivuorinen/gibidify.git
synced 2026-02-10 20:48:20 +00:00
feat: more features, output formats, configs, etc
This commit is contained in:
@@ -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}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user