MCPcopy
hub / github.com/kopia/kopia / configureNotificationAction

Function configureNotificationAction

cli/command_notification_configure_common.go:34–104  ·  view source on GitHub ↗

configureNotificationAction is a helper function that creates a Kingpin action that configures a notification method. it will read the existing profile, merge the provided options, and save the profile back or send a test notification based on the flags.

(
	svc appServices,
	c *commonNotificationOptions,
	senderMethod sender.Method,
	opt *T,
	merge func(ctx context.Context, src T, dst *T, isUpdate bool) error,
)

Source from the content-addressed store, hash-verified

32// it will read the existing profile, merge the provided options, and save the profile back
33// or send a test notification based on the flags.
34func configureNotificationAction[T any](
35 svc appServices,
36 c *commonNotificationOptions,
37 senderMethod sender.Method,
38 opt *T,
39 merge func(ctx context.Context, src T, dst *T, isUpdate bool) error,
40) func(ctx *kingpin.ParseContext) error {
41 return svc.directRepositoryWriteAction(func(ctx context.Context, rep repo.DirectRepositoryWriter) error {
42 var (
43 defaultT T
44 mergedOptions *T
45 )
46
47 // read the existing profile, if any.
48 oldProfile, err := notifyprofile.GetProfile(ctx, rep, c.profileName)
49 if err != nil && !errors.Is(err, notifyprofile.ErrNotFound) {
50 return errors.Wrap(err, "unable to get notification profile")
51 }
52
53 sev := notification.SeverityDefault
54 exists := err == nil
55
56 if exists {
57 if oldProfile.MethodConfig.Type != senderMethod {
58 return errors.Errorf("profile %q already exists but is not of type %q", c.profileName, senderMethod)
59 }
60
61 var parsedT T
62
63 if err := oldProfile.MethodConfig.Options(&parsedT); err != nil {
64 return errors.Wrapf(err, "profile %q already exists but is not of type %q", c.profileName, senderMethod)
65 }
66
67 mergedOptions = &parsedT
68 sev = oldProfile.MinSeverity
69 } else {
70 mergedOptions = &defaultT
71 }
72
73 if err := merge(ctx, *opt, mergedOptions, exists); err != nil {
74 return errors.Wrap(err, "unable to merge options")
75 }
76
77 if c.minSeverity != "" {
78 // severity is specified on the command line, override the one from the profile.
79 sev = notification.SeverityToNumber[c.minSeverity]
80 }
81
82 s, err := sender.GetSender(ctx, c.profileName, senderMethod, mergedOptions)
83 if err != nil {
84 return errors.Wrap(err, "unable to get notification provider")
85 }
86
87 if c.sendTestNotification {
88 if err := notification.SendTestNotification(ctx, rep, s); err != nil {
89 return errors.Wrap(err, "unable to send test notification")
90 }
91 }

Callers 4

setupMethod · 0.85
setupMethod · 0.85
setupMethod · 0.85
setupMethod · 0.85

Calls 7

GetProfileFunction · 0.92
GetSenderFunction · 0.92
SendTestNotificationFunction · 0.92
SaveProfileFunction · 0.92
ErrorfMethod · 0.80
OptionsMethod · 0.45

Tested by

no test coverage detected