github.com/nemunaire/dnscontrol@v0.2.8/pkg/notifications/notifications.go (about)

     1  package notifications
     2  
     3  // Notifier is a type that can send a notification
     4  type Notifier interface {
     5  	// Notify will be called after a correction is performed.
     6  	// It will be given the correction's message, the result of executing it,
     7  	// and a flag for whether this is a preview or if it actually ran.
     8  	// If preview is true, err will always be nil.
     9  	Notify(domain, provider string, message string, err error, preview bool)
    10  	// Done will be called exactly once after all notifications are done. This will allow "batched" notifiers to flush and send
    11  	Done()
    12  }
    13  
    14  // new notification types should add themselves to this array
    15  var initers = []func(map[string]string) Notifier{}
    16  
    17  // Init will take the given config map (from creds.json notifications key) and create a single Notifier with
    18  // all notifications it has full config for.
    19  func Init(config map[string]string) Notifier {
    20  	notifiers := multiNotifier{}
    21  	for _, i := range initers {
    22  		n := i(config)
    23  		if n != nil {
    24  			notifiers = append(notifiers, n)
    25  		}
    26  	}
    27  	return notifiers
    28  }
    29  
    30  type multiNotifier []Notifier
    31  
    32  func (m multiNotifier) Notify(domain, provider string, message string, err error, preview bool) {
    33  	for _, n := range m {
    34  		n.Notify(domain, provider, message, err, preview)
    35  	}
    36  }
    37  func (m multiNotifier) Done() {
    38  	for _, n := range m {
    39  		n.Done()
    40  	}
    41  }