forked from gopasspw/gopass
/
templates.go
112 lines (93 loc) · 2.9 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package action
import (
"bytes"
"context"
"fmt"
"github.com/gopasspw/gopass/pkg/editor"
"github.com/urfave/cli"
)
const (
templateExample = `{{ .Content }}
# This is an example of the available template operations
# Predefined variables:
# - .Content: The secret payload, usually a generated password
# - .Name: The name of this secret
# - .Path: The path to this secret
# - .Dir: The dir of this secret
#
# Available Template functions:
# - md5sum: e.g. {{ .Content | md5sum }}
# - sha1sum: e.g. {{ .Content | sha1sum }}
# - md5crypt: e.g. {{ .Content | md5crypt }}
# - ssha: e.g. {{ .Content | ssha }}
# - ssha256: e.g. {{ .Content | ssha256 }}
# - ssha512: e.g. {{ .Content | ssha512 }}
# - get "key": e.g. {{ get "path/to/some/other/secret" | md5sum }}
`
)
// TemplatesPrint will pretty-print a tree of templates
func (s *Action) TemplatesPrint(ctx context.Context, c *cli.Context) error {
tree, err := s.Store.TemplateTree(ctx)
if err != nil {
return ExitError(ctx, ExitList, err, "failed to list templates: %s", err)
}
fmt.Fprintln(stdout, tree.Format(0))
return nil
}
// TemplatePrint will lookup and print a single template
func (s *Action) TemplatePrint(ctx context.Context, c *cli.Context) error {
name := c.Args().First()
content, err := s.Store.GetTemplate(ctx, name)
if err != nil {
return ExitError(ctx, ExitIO, err, "failed to retrieve template: %s", err)
}
fmt.Fprintln(stdout, string(content))
return nil
}
// TemplateEdit will load and existing or new template into an
// editor
func (s *Action) TemplateEdit(ctx context.Context, c *cli.Context) error {
name := c.Args().First()
var content []byte
if s.Store.HasTemplate(ctx, name) {
var err error
content, err = s.Store.GetTemplate(ctx, name)
if err != nil {
return ExitError(ctx, ExitIO, err, "failed to retrieve template: %s", err)
}
} else {
content = []byte(templateExample)
}
ed := editor.Path(c)
nContent, err := editor.Invoke(ctx, ed, content)
if err != nil {
return ExitError(ctx, ExitUnknown, err, "failed to invoke editor %s: %s", ed, err)
}
// If content is equal, nothing changed, exiting
if bytes.Equal(content, nContent) {
return nil
}
return s.Store.SetTemplate(ctx, name, nContent)
}
// TemplateRemove will remove a single template
func (s *Action) TemplateRemove(ctx context.Context, c *cli.Context) error {
name := c.Args().First()
if name == "" {
return ExitError(ctx, ExitUsage, nil, "usage: %s templates remove [name]", s.Name)
}
if !s.Store.HasTemplate(ctx, name) {
return ExitError(ctx, ExitNotFound, nil, "template '%s' not found", name)
}
return s.Store.RemoveTemplate(ctx, name)
}
// TemplatesComplete prints a list of all templates for bash completion
func (s *Action) TemplatesComplete(ctx context.Context, c *cli.Context) {
tree, err := s.Store.TemplateTree(ctx)
if err != nil {
fmt.Fprintln(stdout, err)
return
}
for _, v := range tree.List(0) {
fmt.Fprintln(stdout, v)
}
}