-
Notifications
You must be signed in to change notification settings - Fork 1
/
import_func.go
82 lines (69 loc) · 2.73 KB
/
import_func.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
package templit
import (
"fmt"
"os"
"path/filepath"
)
// ImportFunc returns a function that can be used as a template function to import and process a template from a remote git repository.
// ImportFunc allows embedding content from a remote repository into a Go template.
//
// Steps to use:
// 1. Add the function to the FuncMap.
// 2. Use the following syntax within your template:
// ```
// {{ import "<host>/<owner>/<repo>/<path>@<tag_or_hash_or_branch>" "<path_to_genrate_files>" . }}
// {{ import "<host>/<owner>/<repo>/<path>#<block>@<tag_or_hash_or_branch>" "<path_to_genrate_files>" . }}
// ```
//
// Placeholders:
// - `<host>`: Repository hosting service (e.g., "github.com").
// - `<owner>`: Repository owner or organization.
// - `<repo>`: Repository name.
// - `<path>`: Path to the desired file or directory within the repository.
// - `<tag_or_hash_or_branch>`: Specific Git reference (tag, commit hash, or branch name).
func (e *Executor) ImportFunc(outputDir string) func(repoAndTag, destPath string, data interface{}) (string, error) {
return func(repoAndTag, destPath string, data interface{}) (string, error) {
const tempDirPrefix = "temp_clone_"
depInfo, err := ParseDepURL(repoAndTag)
if err != nil {
return "", fmt.Errorf("failed to parse embed URL: %w", err)
}
tempDir, err := os.MkdirTemp("", tempDirPrefix)
if err != nil {
return "", fmt.Errorf("failed to create temp dir: %w", err)
}
defer os.RemoveAll(tempDir) // Cleanup
if err := e.git.Clone(depInfo.Host, depInfo.Owner, depInfo.Repo, tempDir); err != nil {
return "", fmt.Errorf("failed to clone repo: %w", err)
}
if depInfo.Tag != "" && depInfo.Tag != e.git.DefaultBranch() {
err = e.git.Checkout(tempDir, depInfo.Tag)
if err != nil {
return "", fmt.Errorf("failed to checkout ref %s: %w", depInfo.Tag, err)
}
}
sourcePath := filepath.Join(tempDir, depInfo.Path)
outputPath := filepath.Join(outputDir, destPath)
// check if path is a file
if info, err := os.Stat(sourcePath); err == nil && !info.IsDir() {
// parse the file
if err := e.ParsePath(filepath.Dir(sourcePath)); err != nil {
return "", fmt.Errorf("failed to create executor: %w", err)
}
// render the file
string, err := e.Render(sourcePath, data)
if err != nil {
return "", fmt.Errorf("failed to render template: %w", err)
}
// write the file
if err := os.WriteFile(filepath.Join(outputPath, filepath.Base(depInfo.Path)), []byte(string), 0644); err != nil {
return "", fmt.Errorf("failed to write file: %w", err)
}
return "", nil
}
if err := e.WalkAndProcessDir(sourcePath, outputPath, data); err != nil {
return "", fmt.Errorf("failed to process template: %w", err)
}
return "", nil
}
}