-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalk.go
89 lines (71 loc) · 2.33 KB
/
walk.go
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
package templit
import (
"fmt"
"os"
"path/filepath"
"strings"
)
// WalkAndProcessDir processes all files in a directory with the given data.
// If walkFunc is provided, it's called for each file and directory without writing the file to disk.
func (e *Executor) WalkAndProcessDir(inputDir, outputDir string, data interface{}) error {
// Create output directory
if err := os.MkdirAll(outputDir, 0755); err != nil {
return fmt.Errorf("failed to create output directory: %w", err)
}
err := filepath.Walk(inputDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("error walking through directory: %w", err)
}
parsedName, err := e.StringRender(filepath.Base(path), data)
if err != nil {
return fmt.Errorf("error rendering path template: %w", err)
}
relPath, err := filepath.Rel(inputDir, filepath.Dir(path))
if err != nil {
return fmt.Errorf("error getting relative path: %w", err)
}
outPath := filepath.Join(outputDir, relPath, parsedName)
parsedOutPath, err := e.StringRender(outPath, data)
if err != nil {
return fmt.Errorf("error rendering path template: %w", err)
}
if info.IsDir() {
// Skip directories with empty or "-" prefixed names
if parsedName == "" || strings.HasPrefix(parsedName, "-") {
return filepath.SkipDir
}
// Skip root directory
if filepath.Base(outPath) == filepath.Base(inputDir) {
return nil
}
if err := os.MkdirAll(parsedOutPath, info.Mode()); err != nil {
return fmt.Errorf("error creating directory: %w", err)
}
return nil
}
// Skip files with empty names or "-" prefixed
if parsedName == "" || strings.HasPrefix(parsedName, "-") {
return nil
}
content, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("error reading file from templates: %w", err)
}
tmpl, err := e.New(path).Parse(string(content))
if err != nil {
return fmt.Errorf("error parsing template: %w", err)
}
var buf strings.Builder
if err := tmpl.Execute(&buf, data); err != nil {
return fmt.Errorf("error executing template: %w", err)
}
if err := os.WriteFile(parsedOutPath, []byte(buf.String()), info.Mode()); err != nil {
return fmt.Errorf("error writing file to output: %w", err)
}
return nil
})
if err != nil {
return fmt.Errorf("error walking through directory: %w", err)
}
return nil
}