-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathembed.go
More file actions
82 lines (71 loc) · 2.09 KB
/
embed.go
File metadata and controls
82 lines (71 loc) · 2.09 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
package resources
import (
"embed"
"fmt"
"io/fs"
"os"
"path/filepath"
)
// EmbeddedResources contains all resource files embedded at compile time
// This includes Tomcat configuration files and other framework resources
//
//go:embed files/**/*
var EmbeddedResources embed.FS
// GetResource reads a single embedded resource file
// path is relative to files/ directory (e.g., "tomcat/conf/server.xml")
func GetResource(path string) ([]byte, error) {
fullPath := filepath.Join("files", path)
data, err := EmbeddedResources.ReadFile(fullPath)
if err != nil {
return nil, fmt.Errorf("failed to read embedded resource %s: %w", path, err)
}
return data, nil
}
// ExtractToDir extracts all embedded resources to the target directory
// Preserves directory structure relative to files/
func ExtractToDir(targetDir string) error {
return fs.WalkDir(EmbeddedResources, "files", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// Calculate relative path (remove "files/" prefix)
relPath, err := filepath.Rel("files", path)
if err != nil {
return fmt.Errorf("failed to calculate relative path: %w", err)
}
targetPath := filepath.Join(targetDir, relPath)
if d.IsDir() {
return os.MkdirAll(targetPath, 0755)
}
data, err := EmbeddedResources.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read %s: %w", path, err)
}
return os.WriteFile(targetPath, data, 0644)
})
}
// ListResources returns all available resource file paths
// Paths are relative to files/ directory
func ListResources() ([]string, error) {
var paths []string
err := fs.WalkDir(EmbeddedResources, "files", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if !d.IsDir() {
relPath, err := filepath.Rel("files", path)
if err != nil {
return err
}
paths = append(paths, relPath)
}
return nil
})
return paths, err
}
// Exists checks if a resource file exists in embedded resources
func Exists(path string) bool {
fullPath := filepath.Join("files", path)
_, err := fs.Stat(EmbeddedResources, fullPath)
return err == nil
}