ignore
1package internal2
3import (4"bytes"5"embed"6"fmt"7"io"8"io/fs"9"path/filepath"10"strings"11)
12
13//go:embed templates/*
14var templates embed.FS15
16type TemplateRegistry struct {17templates fs.FS18}
19
20func NewTemplateRegistry() *TemplateRegistry {21return &TemplateRegistry{templates: templates}22}
23
24func (tr *TemplateRegistry) HasTemplate(name string) bool {25_, err := fs.Stat(tr.templates, fmt.Sprintf("templates/%s.gitignore", name))26return err == nil27}
28
29func (tr *TemplateRegistry) List() []string {30var templates []string31fs.WalkDir(tr.templates, ".", func(path string, d fs.DirEntry, _ error) error {32if d.IsDir() {33return nil34}35template := strings.TrimSuffix(filepath.Base(path), ".gitignore")36templates = append(templates, template)37return nil38})39return templates40}
41
42func (tr *TemplateRegistry) CopyTemplate(name string, dst io.Writer) error {43b, err := fs.ReadFile(tr.templates, fmt.Sprintf("templates/%s.gitignore", name))44if err != nil {45return err46}47io.Copy(dst, bytes.NewReader(b))48return nil49}
50