-
Notifications
You must be signed in to change notification settings - Fork 927
/
helpers.go
151 lines (124 loc) · 3.69 KB
/
helpers.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package application
import (
"cf"
"cf/terminal"
"fmt"
"github.com/cloudfoundry/loggregatorlib/logmessage"
"regexp"
"strings"
"time"
)
const (
TIMESTAMP_FORMAT = "2006-01-02T15:04:05.00-0700"
)
func simpleLogMessageOutput(msg *logmessage.Message) (msgText string) {
logMsg := msg.GetLogMessage()
msgText = string(logMsg.GetMessage())
reg, err := regexp.Compile("[\n\r]+$")
if err != nil {
return
}
msgText = reg.ReplaceAllString(msgText, "")
return
}
func logMessageOutput(msg *logmessage.Message) string {
logHeader, coloredLogHeader := extractLogHeader(msg)
logMsg := msg.GetLogMessage()
logContent := extractLogContent(logMsg, logHeader)
return fmt.Sprintf("%s%s", coloredLogHeader, logContent)
}
func extractLogHeader(msg *logmessage.Message) (logHeader, coloredLogHeader string) {
logMsg := msg.GetLogMessage()
sourceType := msg.GetShortSourceTypeName()
sourceId := logMsg.GetSourceId()
t := time.Unix(0, logMsg.GetTimestamp())
timeFormat := TIMESTAMP_FORMAT
timeString := t.Format(timeFormat)
logHeader = fmt.Sprintf("%s [%s]", timeString, sourceType)
coloredLogHeader = terminal.LogSysHeaderColor(logHeader)
if logMsg.GetSourceType() == logmessage.LogMessage_WARDEN_CONTAINER {
logHeader = fmt.Sprintf("%s [%s/%s]", timeString, sourceType, sourceId)
coloredLogHeader = terminal.LogAppHeaderColor(logHeader)
}
// Calculate padding
longestHeader := fmt.Sprintf("%s [App/0] ", timeFormat)
expectedHeaderLength := len(longestHeader)
padding := strings.Repeat(" ", expectedHeaderLength-len(logHeader))
logHeader = logHeader + padding
coloredLogHeader = coloredLogHeader + padding
return
}
func extractLogContent(logMsg *logmessage.LogMessage, logHeader string) (logContent string) {
msgText := string(logMsg.GetMessage())
reg, err := regexp.Compile("[\n\r]+$")
if err == nil {
msgText = reg.ReplaceAllString(msgText, "")
}
msgLines := strings.Split(msgText, "\n")
padding := strings.Repeat(" ", len(logHeader))
coloringFunc := terminal.LogStdoutColor
logType := "OUT"
if logMsg.GetMessageType() == logmessage.LogMessage_ERR {
coloringFunc = terminal.LogStderrColor
logType = "ERR"
}
logContent = fmt.Sprintf("%s %s", logType, msgLines[0])
for _, msgLine := range msgLines[1:] {
logContent = fmt.Sprintf("%s\n%s%s", logContent, padding, msgLine)
}
logContent = coloringFunc(logContent)
return
}
func envVarFound(varName string, existingEnvVars map[string]string) (found bool) {
for name, _ := range existingEnvVars {
if name == varName {
found = true
return
}
}
return
}
func coloredAppState(app cf.Application) string {
appState := strings.ToLower(app.State)
if app.RunningInstances == 0 {
if appState == "stopped" {
return appState
} else {
return terminal.CrashedColor(appState)
}
}
if app.RunningInstances < app.Instances {
return terminal.WarningColor(appState)
}
return appState
}
func coloredAppInstaces(app cf.Application) string {
healthString := fmt.Sprintf("%d/%d", app.RunningInstances, app.Instances)
if app.RunningInstances == 0 {
if strings.ToLower(app.State) == "stopped" {
return healthString
} else {
return terminal.CrashedColor(healthString)
}
}
if app.RunningInstances < app.Instances {
return terminal.WarningColor(healthString)
}
return healthString
}
func coloredInstanceState(instance cf.ApplicationInstance) (colored string) {
state := string(instance.State)
switch state {
case "started", "running":
colored = terminal.StartedColor("running")
case "stopped":
colored = terminal.StoppedColor("stopped")
case "flapping":
colored = terminal.WarningColor("crashing")
case "starting":
colored = terminal.AdvisoryColor("starting")
default:
colored = terminal.FailureColor(state)
}
return
}