64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package hooks
|
|
|
|
import (
|
|
"embed"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//go:embed template
|
|
var templateFS embed.FS
|
|
|
|
|
|
|
|
func TemplateDir(root string) string {
|
|
return filepath.Join(root, "template")
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func EnsureTemplate(root string) (string, error) {
|
|
dst := TemplateDir(root)
|
|
if err := os.MkdirAll(dst, 0o755); err != nil {
|
|
return "", fmt.Errorf("hooks: mkdir template: %w", err)
|
|
}
|
|
err := fs.WalkDir(templateFS, "template", func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if d.IsDir() {
|
|
return os.MkdirAll(filepath.Join(root, p), 0o755)
|
|
}
|
|
data, err := templateFS.ReadFile(p)
|
|
if err != nil {
|
|
return fmt.Errorf("hooks: read embedded %s: %w", p, err)
|
|
}
|
|
out := filepath.Join(root, p)
|
|
if err := os.WriteFile(out, data, 0o644); err != nil {
|
|
return fmt.Errorf("hooks: write %s: %w", p, err)
|
|
}
|
|
|
|
if filepath.Dir(p) == "template/hooks" {
|
|
if err := os.Chmod(out, 0o755); err != nil {
|
|
return fmt.Errorf("hooks: chmod %s: %w", p, err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return dst, nil
|
|
}
|