forked from gopasspw/gopass
/
templates.go
75 lines (63 loc) · 2.21 KB
/
templates.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package root
import (
"context"
"sort"
"github.com/gopasspw/gopass/pkg/out"
"github.com/gopasspw/gopass/pkg/store"
"github.com/gopasspw/gopass/pkg/tree"
"github.com/gopasspw/gopass/pkg/tree/simple"
"github.com/pkg/errors"
)
// LookupTemplate will lookup and return a template
func (r *Store) LookupTemplate(ctx context.Context, name string) ([]byte, bool) {
_, store, name := r.getStore(ctx, name)
return store.LookupTemplate(ctx, name)
}
// TemplateTree returns a tree of all templates
func (r *Store) TemplateTree(ctx context.Context) (tree.Tree, error) {
root := simple.New("gopass")
for _, t := range r.store.ListTemplates(ctx, "") {
out.Debug(ctx, "[<root>] Adding template %s", t)
if err := root.AddFile(t, "gopass/template"); err != nil {
out.Error(ctx, "Failed to add file to tree: %s", err)
}
}
mps := r.MountPoints()
sort.Sort(store.ByPathLen(mps))
for _, alias := range mps {
substore := r.mounts[alias]
if substore == nil {
continue
}
if err := root.AddMount(alias, substore.Path()); err != nil {
return nil, errors.Errorf("failed to add mount: %s", err)
}
for _, t := range substore.ListTemplates(ctx, alias) {
out.Debug(ctx, "[%s] Adding template %s", alias, t)
if err := root.AddFile(t, "gopass/template"); err != nil {
out.Error(ctx, "Failed to add file to tree: %s", err)
}
}
}
return root, nil
}
// HasTemplate returns true if the template exists
func (r *Store) HasTemplate(ctx context.Context, name string) bool {
_, store, name := r.getStore(ctx, name)
return store.HasTemplate(ctx, name)
}
// GetTemplate will return the content of the named template
func (r *Store) GetTemplate(ctx context.Context, name string) ([]byte, error) {
_, store, name := r.getStore(ctx, name)
return store.GetTemplate(ctx, name)
}
// SetTemplate will (over)write the content to the template file
func (r *Store) SetTemplate(ctx context.Context, name string, content []byte) error {
_, store, name := r.getStore(ctx, name)
return store.SetTemplate(ctx, name, content)
}
// RemoveTemplate will delete the named template if it exists
func (r *Store) RemoveTemplate(ctx context.Context, name string) error {
_, store, name := r.getStore(ctx, name)
return store.RemoveTemplate(ctx, name)
}