github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/kvwatcher/state_notification.go (about)

     1  // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package kvwatcher
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	cloudevents "github.com/cloudevents/sdk-go/v2"
    12  
    13  	"github.com/choria-io/go-choria/aagent/watchers/event"
    14  )
    15  
    16  // StateNotification describes the current state of the watcher
    17  // described by io.choria.machine.timer.exec.v1.state
    18  type StateNotification struct {
    19  	event.Event
    20  
    21  	State  string `json:"state"`
    22  	Bucket string `json:"bucket"`
    23  	Key    string `json:"key,omitempty"`
    24  	Mode   string `json:"mode"`
    25  }
    26  
    27  // JSON creates a JSON representation of the notification
    28  func (s *StateNotification) JSON() ([]byte, error) {
    29  	return json.Marshal(s.CloudEvent())
    30  }
    31  
    32  // CloudEvent creates a CloudEvent from the state notification
    33  func (s *StateNotification) CloudEvent() cloudevents.Event {
    34  	return s.Event.CloudEvent(s)
    35  }
    36  
    37  // String is a string representation of the notification suitable for printing
    38  func (s *StateNotification) String() string {
    39  	if s.Key != "" {
    40  		return fmt.Sprintf("%s key-value %s#%s %sing bucket: %s key: %s state: %s", s.Identity, s.Machine, s.Name, s.Mode, s.Bucket, s.Key, s.State)
    41  	} else {
    42  		return fmt.Sprintf("%s key-value %s#%s %sing bucket: %s state: %s", s.Identity, s.Machine, s.Name, s.Mode, s.Bucket, s.State)
    43  	}
    44  }