• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / modo / 13102537246

02 Feb 2025 09:14PM CUT coverage: 73.324%. Remained the same
13102537246

Pull #187

github

web-flow
Merge 44bfc2087 into a737b2703
Pull Request #187: Update CLI help examples for v0.10.0 features

1509 of 2058 relevant lines covered (73.32%)

24.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.21
/document/util.go
1
package document
2

3
import (
4
        "errors"
5
        "fmt"
6
        "os"
7
        "path/filepath"
8
        "text/template"
9

10
        "github.com/mlange-42/modo/assets"
11
)
12

13
// appends to a slice, but guaranties to return a new one and not alter the original.
14
func appendNew[T any](sl []T, elems ...T) []T {
692✔
15
        sl2 := make([]T, len(sl), len(sl)+len(elems))
692✔
16
        copy(sl2, sl)
692✔
17
        sl2 = append(sl2, elems...)
692✔
18
        return sl2
692✔
19
}
692✔
20

21
func warnOrError(strict bool, pattern string, args ...any) error {
2✔
22
        if strict {
3✔
23
                return fmt.Errorf(pattern, args...)
1✔
24
        }
1✔
25
        fmt.Printf("WARNING: "+pattern+"\n", args...)
1✔
26
        return nil
1✔
27
}
28

29
func LoadTemplates(f Formatter, additional ...string) (*template.Template, error) {
13✔
30
        templ := template.New("all")
13✔
31
        templ = templ.Funcs(template.FuncMap{
13✔
32
                "toLink": f.ToLinkPath,
13✔
33
        })
13✔
34
        templ, err := templ.ParseFS(assets.Templates, "templates/*.*", "templates/**/*.*")
13✔
35
        if err != nil {
13✔
36
                return nil, err
×
37
        }
×
38

39
        for _, dir := range additional {
14✔
40
                if dir == "" {
1✔
41
                        continue
×
42
                }
43
                exists, isDir, err := fileExists(dir)
1✔
44
                if err != nil {
1✔
45
                        return nil, err
×
46
                }
×
47
                if !exists || !isDir {
1✔
48
                        return nil, fmt.Errorf("template directory '%s' does not exist", dir)
×
49
                }
×
50
                moreTemplates, err := findTemplates(dir)
1✔
51
                if err != nil {
1✔
52
                        return nil, err
×
53
                }
×
54
                templ, err = templ.ParseFiles(moreTemplates...)
1✔
55
                if err != nil {
1✔
56
                        return nil, err
×
57
                }
×
58
        }
59
        return templ, nil
13✔
60
}
61

62
func fileExists(file string) (exists, isDir bool, err error) {
1✔
63
        var s os.FileInfo
1✔
64
        if s, err = os.Stat(file); err == nil {
2✔
65
                exists = true
1✔
66
                isDir = s.IsDir()
1✔
67
                return
1✔
68
        } else if !errors.Is(err, os.ErrNotExist) {
1✔
69
                return
×
70
        }
×
71
        err = nil
×
72
        return
×
73
}
74

75
func findTemplates(dir string) ([]string, error) {
1✔
76
        allTemplates := []string{}
1✔
77
        err := filepath.WalkDir(dir,
1✔
78
                func(path string, info os.DirEntry, err error) error {
6✔
79
                        if err != nil {
5✔
80
                                return err
×
81
                        }
×
82
                        if !info.IsDir() {
9✔
83
                                allTemplates = append(allTemplates, path)
4✔
84
                        }
4✔
85
                        return nil
5✔
86
                })
87
        if err != nil {
1✔
88
                return nil, err
×
89
        }
×
90
        return allTemplates, nil
1✔
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc