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

73 lines
1.7 KiB
Go

package cli
import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/kopia/kopia/notification"
"github.com/kopia/kopia/notification/notifyprofile"
"github.com/kopia/kopia/notification/sender"
"github.com/kopia/kopia/repo"
)
type commandNotificationProfileList struct {
out textOutput
jo jsonOutput
}
func (c *commandNotificationProfileList) setup(svc appServices, parent commandParent) {
cmd := parent.Command("list", "List notification profiles").Alias("ls")
c.out.setup(svc)
c.jo.setup(svc, cmd)
cmd.Action(svc.repositoryReaderAction(c.run))
}
func (c *commandNotificationProfileList) run(ctx context.Context, rep repo.Repository) error {
var jl jsonList
if c.jo.jsonOutput {
jl.begin(&c.jo)
defer jl.end()
}
profileConfigs, err := notifyprofile.ListProfiles(ctx, rep)
if err != nil {
return errors.Wrap(err, "unable to list notification profiles")
}
for i, pc := range profileConfigs {
var summ notifyprofile.Summary
summ.ProfileName = pc.ProfileName
summ.Type = string(pc.MethodConfig.Type)
summ.MinSeverity = int32(pc.MinSeverity)
// Provider returns a new instance of the notification provider.
if prov, err := sender.GetSender(ctx, pc.ProfileName, pc.MethodConfig.Type, pc.MethodConfig.Config); err == nil {
summ.Summary = prov.Summary()
} else {
summ.Summary = fmt.Sprintf("%v - invalid", pc.MethodConfig.Type)
}
if c.jo.jsonOutput {
jl.emit(summ)
} else {
if i > 0 {
c.out.printStdout("\n")
}
c.out.printStdout("Profile %q Type %q Minimum Severity: %v\n %v\n",
summ.ProfileName,
pc.MethodConfig.Type,
notification.SeverityToString[pc.MinSeverity],
summ.Summary)
}
}
return nil
}