github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/lifecycle/startup.go (about) 1 // Copyright (c) 2020-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package lifecycle 6 7 import ( 8 "encoding/json" 9 "fmt" 10 ) 11 12 // StartupEvent is a io.choria.lifecycle.v1.startup event 13 // 14 // In addition to the usually required fields it requires a Version() 15 // specified when producing this type of event 16 type StartupEvent struct { 17 basicEvent 18 Version string `json:"version"` 19 } 20 21 func init() { 22 eventTypes["startup"] = Startup 23 24 eventJSONParsers[Startup] = func(j []byte) (Event, error) { 25 return newStartupEventFromJSON(j) 26 } 27 28 eventFactories[Startup] = func(opts ...Option) Event { 29 return newStartupEvent(opts...) 30 } 31 } 32 33 func newStartupEvent(opts ...Option) *StartupEvent { 34 event := &StartupEvent{basicEvent: newBasicEvent("startup")} 35 36 for _, o := range opts { 37 o(event) 38 } 39 40 return event 41 } 42 43 func newStartupEventFromJSON(j []byte) (*StartupEvent, error) { 44 event := &StartupEvent{basicEvent: newBasicEvent("startup")} 45 46 err := json.Unmarshal(j, event) 47 if err != nil { 48 return nil, err 49 } 50 51 switch event.EventProtocol { 52 case "io.choria.lifecycle.v1.startup": 53 case "choria:lifecycle:startup:1": 54 event.EventProtocol = "io.choria.lifecycle.v1.startup" 55 default: 56 return nil, fmt.Errorf("invalid protocol '%s'", event.EventProtocol) 57 } 58 59 return event, nil 60 } 61 62 // String is text suitable to display on the console etc 63 func (e *StartupEvent) String() string { 64 return fmt.Sprintf("[startup] %s: %s version %s", e.Ident, e.Component(), e.Version) 65 } 66 67 // SetVersion sets the version for the event 68 func (e *StartupEvent) SetVersion(v string) { 69 e.Version = v 70 }