github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/internal/store/actions.go (about) 1 package store 2 3 import ( 4 "fmt" 5 "strings" 6 "time" 7 8 v1 "k8s.io/api/core/v1" 9 10 "github.com/tilt-dev/tilt/pkg/logger" 11 "github.com/tilt-dev/tilt/pkg/model" 12 "github.com/tilt-dev/tilt/pkg/model/logstore" 13 "github.com/tilt-dev/wmclient/pkg/analytics" 14 ) 15 16 type ErrorAction struct { 17 Error error 18 } 19 20 func (ErrorAction) Action() {} 21 22 func NewErrorAction(err error) ErrorAction { 23 return ErrorAction{Error: err} 24 } 25 26 type LogAction struct { 27 mn model.ManifestName 28 spanID logstore.SpanID 29 timestamp time.Time 30 fields logger.Fields 31 msg []byte 32 level logger.Level 33 } 34 35 func (LogAction) Action() {} 36 37 func (LogAction) Summarize(s *ChangeSummary) { 38 s.Log = true 39 } 40 41 func (le LogAction) ManifestName() model.ManifestName { 42 return le.mn 43 } 44 45 func (le LogAction) Level() logger.Level { 46 return le.level 47 } 48 49 func (le LogAction) Time() time.Time { 50 return le.timestamp 51 } 52 53 func (le LogAction) Fields() logger.Fields { 54 return le.fields 55 } 56 57 func (le LogAction) Message() []byte { 58 return le.msg 59 } 60 61 func (le LogAction) SpanID() logstore.SpanID { 62 return le.spanID 63 } 64 65 func (le LogAction) String() string { 66 return fmt.Sprintf("manifest: %s, spanID: %s, msg: %q", le.mn, le.spanID, le.msg) 67 } 68 69 func NewLogAction(mn model.ManifestName, spanID logstore.SpanID, level logger.Level, fields logger.Fields, b []byte) LogAction { 70 return LogAction{ 71 mn: mn, 72 spanID: spanID, 73 level: level, 74 timestamp: time.Now(), 75 msg: append([]byte{}, b...), 76 fields: fields, 77 } 78 } 79 80 func NewGlobalLogAction(level logger.Level, b []byte) LogAction { 81 return LogAction{ 82 mn: "", 83 spanID: "", 84 level: level, 85 timestamp: time.Now(), 86 msg: append([]byte{}, b...), 87 } 88 } 89 90 type K8sEventAction struct { 91 Event *v1.Event 92 ManifestName model.ManifestName 93 } 94 95 func (K8sEventAction) Action() {} 96 97 func NewK8sEventAction(event *v1.Event, manifestName model.ManifestName) K8sEventAction { 98 return K8sEventAction{event, manifestName} 99 } 100 101 func (kEvt K8sEventAction) ToLogAction(mn model.ManifestName) LogAction { 102 msg := fmt.Sprintf("[event: %s] %s\n", 103 objRefHumanReadable(kEvt.Event.InvolvedObject), 104 strings.TrimSpace(kEvt.Event.Message)) 105 106 return LogAction{ 107 mn: mn, 108 spanID: logstore.SpanID(fmt.Sprintf("events:%s", mn)), 109 level: logger.InfoLvl, 110 timestamp: kEvt.Event.LastTimestamp.Time, 111 msg: []byte(msg), 112 } 113 } 114 115 func objRefHumanReadable(obj v1.ObjectReference) string { 116 kind := strings.ToLower(obj.Kind) 117 if obj.Namespace == "" || obj.Namespace == "default" { 118 return fmt.Sprintf("%s %s", kind, obj.Name) 119 } 120 return fmt.Sprintf("%s %s/%s", kind, obj.Namespace, obj.Name) 121 } 122 123 type AnalyticsUserOptAction struct { 124 Opt analytics.Opt 125 } 126 127 func (AnalyticsUserOptAction) Action() {} 128 129 type AnalyticsNudgeSurfacedAction struct{} 130 131 func (AnalyticsNudgeSurfacedAction) Action() {} 132 133 type TiltCloudStatusReceivedAction struct { 134 SuggestedTiltVersion string 135 } 136 137 func (TiltCloudStatusReceivedAction) Action() {} 138 139 type PanicAction struct { 140 Err error 141 } 142 143 func (PanicAction) Action() {} 144 145 type AppendToTriggerQueueAction struct { 146 Name model.ManifestName 147 Reason model.BuildReason 148 } 149 150 func (AppendToTriggerQueueAction) Action() {}