Files
kopia/cli/command_notification_template_show.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

74 lines
1.6 KiB
Go

package cli
import (
"context"
"os"
"path/filepath"
"strings"
"github.com/pkg/errors"
"github.com/skratchdot/open-golang/open"
"github.com/kopia/kopia/notification/notifytemplate"
"github.com/kopia/kopia/repo"
)
type commandNotificationTemplateShow struct {
notificationTemplateNameArg
templateFormat string
original bool
htmlOutput bool
out textOutput
}
func (c *commandNotificationTemplateShow) setup(svc appServices, parent commandParent) {
cmd := parent.Command("show", "Show template")
c.notificationTemplateNameArg.setup(svc, cmd)
cmd.Flag("format", "Template format").EnumVar(&c.templateFormat, "html", "md")
cmd.Flag("original", "Show original template").BoolVar(&c.original)
cmd.Flag("html", "Convert the output to HTML").BoolVar(&c.htmlOutput)
cmd.Action(svc.repositoryReaderAction(c.run))
c.out.setup(svc)
}
func (c *commandNotificationTemplateShow) run(ctx context.Context, rep repo.Repository) error {
var (
text string
err error
)
if c.original {
text, err = notifytemplate.GetEmbeddedTemplate(c.templateName)
} else {
var found bool
text, found, err = notifytemplate.GetTemplate(ctx, rep, c.templateName)
if !found {
text, err = notifytemplate.GetEmbeddedTemplate(c.templateName)
}
}
if err != nil {
return errors.Wrap(err, "error listing templates")
}
if c.htmlOutput {
tf := filepath.Join(os.TempDir(), "kopia-template-preview.html")
//nolint:gosec,mnd
if err := os.WriteFile(tf, []byte(text), 0o644); err != nil {
return errors.Wrap(err, "error writing template to file")
}
open.Run(tf) //nolint:errcheck
}
c.out.printStdout("%v\n", strings.TrimRight(text, "\n"))
return nil
}