github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/lifecycle/tally/options.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 tally 6 7 import ( 8 "fmt" 9 10 "github.com/sirupsen/logrus" 11 ) 12 13 // options are options that configure the tool 14 type options struct { 15 Component string 16 Debug bool 17 Log *logrus.Entry 18 Connector Connector 19 StatPrefix string 20 Election string 21 } 22 23 // Option configures Options 24 type Option func(*options) 25 26 // Validate validates options meet minimal requirements, also assigns defaults 27 // for optional settings 28 func (o *options) Validate() error { 29 if o.Component == ">" { 30 return fmt.Errorf("invalid component %s", o.Component) 31 } 32 33 if o.Connector == nil { 34 return fmt.Errorf("needs a connector") 35 } 36 37 if o.StatPrefix == "" { 38 o.StatPrefix = "lifecycle_tally" 39 } 40 41 if o.Log == nil { 42 o.Log = logrus.NewEntry(logrus.New()) 43 } 44 45 return nil 46 } 47 48 // Component sets the component to tally 49 func Component(c string) Option { 50 return func(o *options) { 51 o.Component = c 52 } 53 } 54 55 // Debug enable debug logging 56 func Debug() Option { 57 return func(o *options) { 58 o.Debug = true 59 } 60 } 61 62 // Logger is the logger to use 63 func Logger(l *logrus.Entry) Option { 64 return func(o *options) { 65 o.Log = l 66 } 67 } 68 69 // Connection is the middleware to receive events on 70 func Connection(c Connector) Option { 71 return func(o *options) { 72 o.Connector = c 73 } 74 } 75 76 // StatsPrefix is the space to create stat entries in 77 func StatsPrefix(p string) Option { 78 return func(o *options) { 79 o.StatPrefix = p 80 } 81 } 82 83 // Election enables leader election between tally instances 84 func Election(name string) Option { 85 return func(o *options) { 86 o.Election = name 87 } 88 }