findAndParseHTMLFiles recursively walks the file system passed finding all *.html files. The template returned has all html files parsed.
(files fs.FS)
| 623 | // findAndParseHTMLFiles recursively walks the file system passed finding all *.html files. |
| 624 | // The template returned has all html files parsed. |
| 625 | func findAndParseHTMLFiles(files fs.FS) (*template.Template, error) { |
| 626 | // root is the collection of html templates. All templates are named by their pathing. |
| 627 | // So './404.html' is named '404.html'. './subdir/index.html' is 'subdir/index.html' |
| 628 | root := template.New("") |
| 629 | |
| 630 | rootPath := "." |
| 631 | err := fs.WalkDir(files, rootPath, func(filePath string, directory fs.DirEntry, err error) error { |
| 632 | if err != nil { |
| 633 | return err |
| 634 | } |
| 635 | |
| 636 | if directory.IsDir() { |
| 637 | return nil |
| 638 | } |
| 639 | |
| 640 | if filepath.Ext(directory.Name()) != ".html" { |
| 641 | return nil |
| 642 | } |
| 643 | |
| 644 | file, err := files.Open(filePath) |
| 645 | if err != nil { |
| 646 | return err |
| 647 | } |
| 648 | |
| 649 | data, err := io.ReadAll(file) |
| 650 | if err != nil { |
| 651 | return err |
| 652 | } |
| 653 | |
| 654 | tPath := strings.TrimPrefix(filePath, rootPath+string(filepath.Separator)) |
| 655 | _, err = root.New(tPath).Parse(string(data)) |
| 656 | if err != nil { |
| 657 | return err |
| 658 | } |
| 659 | |
| 660 | return nil |
| 661 | }) |
| 662 | if err != nil { |
| 663 | return nil, err |
| 664 | } |
| 665 | return root, nil |
| 666 | } |
| 667 | |
| 668 | type installScriptState struct { |
| 669 | Origin string |