Files
kopia/cli/command_notification_template_set.go
Jarek Kowalski c0bd372d29 feat(cli): support for defining notification profiles and templates via CLI (#4034)
* feat(cli): support for defining notification profiles via CLI

Profile management:

```
$ kopia notification profile configure email \
    --profile-name=X \
    --smtp-server=smtp.gmail.com \
    --smtp-port=587 \
    --smtp-username=X \
    --smtp-password=X \
    --mail-from=X \
    --mail-to=X \
    --format=html|txt \
    [--send-test-notification]

$ kopia notification profile configure pushover --profile-name=X \
    --user-key=X \
    --app-token=X \
    --format=html|txt \
    [--send-test-notification]

$ kopia notification profile configure webhook --profile-name=X \
    --endpooint=http://some-address:port/path \
    --method=POST|PUT \
    --format=html|txt \
    [--send-test-notification]

$ kopia notification profile test --profile-name=X

$ kopia notification profile delete --profile-name=X

$ kopia notification profile list
```

Template management:

```
$ kopia notification template show X

$ kopia notification template set X \
   --from-stdin | --from-file=X | --editor

$ kopia notification template remove X

$ kopia notification template list

```

Implements #1958

* additional refactoring for testability, various naming tweaks
2024-10-06 16:28:39 +00:00

94 lines
2.3 KiB
Go

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)
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)
}