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

52 lines
1.3 KiB
Go

package cli
import (
"context"
"strings"
"github.com/alecthomas/kingpin/v2"
"github.com/kopia/kopia/notification/notifyprofile"
"github.com/kopia/kopia/repo"
)
type commandNotificationProfile struct {
config commandNotificationProfileConfigure
list commandNotificationProfileList
delete commandNotificationProfileDelete
test commandNotificationProfileTest
}
func (c *commandNotificationProfile) setup(svc appServices, parent commandParent) {
cmd := parent.Command("profile", "Manage notification profiles")
c.config.setup(svc, cmd)
c.delete.setup(svc, cmd)
c.test.setup(svc, cmd)
c.list.setup(svc, cmd)
}
type notificationProfileFlag struct {
profileName string
}
func (c *notificationProfileFlag) setup(svc appServices, cmd *kingpin.CmdClause) {
cmd.Flag("profile-name", "Profile name").Required().HintAction(svc.repositoryHintAction(c.listNotificationProfiles)).StringVar(&c.profileName)
}
func (c *notificationProfileFlag) listNotificationProfiles(ctx context.Context, rep repo.Repository) []string {
profiles, err := notifyprofile.ListProfiles(ctx, rep)
if err != nil {
return nil
}
var hints []string
for _, ti := range profiles {
if strings.HasPrefix(ti.ProfileName, c.profileName) {
hints = append(hints, ti.ProfileName)
}
}
return hints
}