/
extensions.go
89 lines (78 loc) · 1.84 KB
/
extensions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package v1
import (
"fmt"
"log/slog"
"strings"
"time"
"github.com/ttacon/chalk"
"google.golang.org/protobuf/types/known/timestamppb"
)
func (s *HealthStatus) Summary() string {
if s.Status == nil || s.Health == nil {
return "Unknown"
}
if !s.Status.Connected {
return "Disconnected"
}
if len(s.Health.Conditions) > 0 {
return fmt.Sprintf("Unhealthy: %s", strings.Join(s.Health.Conditions, ", "))
}
if !s.Health.Ready {
return fmt.Sprintf("Not ready (unknown reason)")
}
return "Healthy"
}
type TimestampedLog interface {
GetTimestamp() *timestamppb.Timestamp
GetMsg() string
GetLogLevel() slog.Level
}
var _ TimestampedLog = (*LogEntry)(nil)
var _ TimestampedLog = (*StateTransition)(nil)
func (s *LogEntry) GetLogLevel() slog.Level {
return slog.Level(s.Level)
}
func (s *StateTransition) GetLogLevel() slog.Level {
return slog.LevelInfo
}
func (s *StateTransition) GetMsg() string {
return fmt.Sprintf("State changed to %s",
stateColor(s.State).Color(s.State.String()))
}
func stateColor(state TaskState) chalk.Color {
switch state {
case TaskState_Pending:
return chalk.Yellow
case TaskState_Running:
return chalk.Blue
case TaskState_Failed, TaskState_Canceled:
return chalk.Red
case TaskState_Completed:
return chalk.Green
default:
return chalk.White
}
}
func NewRevision(revision int64, maybeTimestamp ...time.Time) *Revision {
return &Revision{
Revision: &revision,
Timestamp: func() *timestamppb.Timestamp {
if len(maybeTimestamp) > 0 && !maybeTimestamp[0].IsZero() {
return timestamppb.New(maybeTimestamp[0])
}
return nil
}(),
}
}
// Set sets the revision to the given value, and clears the timestamp.
func (r *Revision) Set(revision int64) {
if r == nil {
panic("revision is nil")
}
if r.Revision == nil {
r.Revision = &revision
} else {
*r.Revision = revision
r.Timestamp = nil
}
}