forked from kopia/kopia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_notification_template_set.go
More file actions
93 lines (74 loc) · 2.32 KB
/
Copy pathcommand_notification_template_set.go
File metadata and controls
93 lines (74 loc) · 2.32 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
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
package cli
import (
"context"
"io"
"os"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/editor"
"github.com/kopia/kopia/notification/notifytemplate"
"github.com/kopia/kopia/repo"
)
type commandNotificationTemplateSet struct {
notificationTemplateNameArg
fromStdin bool
fromFileName string
editor bool
out textOutput
svc appServices
}
func (c *commandNotificationTemplateSet) setup(svc appServices, parent commandParent) {
cmd := parent.Command("set", "Set the notification template")
c.notificationTemplateNameArg.setup(svc, cmd)
cmd.Flag("from-stdin", "Read new template from stdin").BoolVar(&c.fromStdin)
cmd.Flag("from-file", "Read new template from file").ExistingFileVar(&c.fromFileName)
cmd.Flag("editor", "Edit template using default editor").BoolVar(&c.editor)
cmd.Action(svc.repositoryWriterAction(c.run))
c.svc = svc
c.out.setup(svc)
}
func (c *commandNotificationTemplateSet) run(ctx context.Context, rep repo.RepositoryWriter) error {
var (
data []byte
err error
)
switch {
case c.fromStdin:
data, err = io.ReadAll(c.svc.stdin())
case c.fromFileName != "":
data, err = os.ReadFile(c.fromFileName)
case c.editor:
return c.launchEditor(ctx, rep)
default:
return errors.Errorf("must specify either --from-file, --from-stdin or --editor")
}
if err != nil {
return errors.Wrap(err, "error reading template")
}
//nolint:wrapcheck
return notifytemplate.SetTemplate(ctx, rep, c.templateName, string(data))
}
func (c *commandNotificationTemplateSet) launchEditor(ctx context.Context, rep repo.RepositoryWriter) error {
s, found, err := notifytemplate.GetTemplate(ctx, rep, c.templateName)
if err != nil {
return errors.Wrap(err, "unable to get template")
}
if !found {
s, err = notifytemplate.GetEmbeddedTemplate(c.templateName)
if err != nil {
return errors.Wrap(err, "unable to get template")
}
}
var lastUpdated string
if err := editor.EditLoop(ctx, "template.md", s, false, func(updated string) error {
_, err := notifytemplate.ParseTemplate(updated, notifytemplate.DefaultOptions)
if err == nil {
lastUpdated = updated
return nil
}
return errors.Wrap(err, "invalid template")
}); err != nil {
return errors.Wrap(err, "unable to edit template")
}
//nolint:wrapcheck
return notifytemplate.SetTemplate(ctx, rep, c.templateName, lastUpdated)
}