-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.go
More file actions
46 lines (37 loc) · 1.1 KB
/
loader.go
File metadata and controls
46 lines (37 loc) · 1.1 KB
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
package theme
import (
"context"
"fmt"
"github.com/gowool/theme/internal"
)
var _ Loader = (*RepositoryLoader)(nil)
type RepositoryLoader struct {
repository Repository
}
func NewRepositoryLoader(repository Repository) *RepositoryLoader {
return &RepositoryLoader{repository: repository}
}
func (l *RepositoryLoader) Get(ctx context.Context, name string) (*Source, error) {
if template, err := l.repository.FindByName(ctx, name); err == nil {
return &Source{
Name: name,
Code: template.Code(),
}, nil
}
// if the template is not available, it should return a dummy source.
return &Source{
Name: name,
Code: internal.Bytes(fmt.Sprintf("{{raw \"<!-- ====== template `%s` is disabled ====== -->\"}}", name)),
}, nil
}
func (l *RepositoryLoader) IsFresh(ctx context.Context, name string, t int64) (bool, error) {
item, err := l.repository.FindByName(ctx, name)
if err != nil {
return false, err
}
return item.Changed().Unix() < t, nil
}
func (l *RepositoryLoader) Exists(ctx context.Context, name string) (bool, error) {
_, err := l.repository.FindByName(ctx, name)
return err == nil, err
}