forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_cache_repository.go
80 lines (64 loc) · 2.24 KB
/
log_cache_repository.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
package logs
import (
"context"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/cf/terminal"
)
type terminalColorLogger struct {
}
func (t terminalColorLogger) LogSysHeaderColor(message string) string {
return terminal.LogSysHeaderColor(message)
}
func (t terminalColorLogger) LogStdoutColor(message string) string {
return terminal.LogStdoutColor(message)
}
func (t terminalColorLogger) LogStderrColor(message string) string {
return terminal.LogStderrColor(message)
}
type logCacheRepository struct {
client sharedaction.LogCacheClient
cancelFunc context.CancelFunc
recentLogsFunc func(appGUID string, client sharedaction.LogCacheClient) ([]sharedaction.LogMessage, error)
getStreamingLogsFunc func(appGUID string, client sharedaction.LogCacheClient) (<-chan sharedaction.LogMessage, <-chan error, context.CancelFunc)
}
func NewLogCacheRepository(
client sharedaction.LogCacheClient,
recentLogsFunc func(appGUID string, client sharedaction.LogCacheClient) ([]sharedaction.LogMessage, error),
getStreamingLogsFunc func(appGUID string, client sharedaction.LogCacheClient) (<-chan sharedaction.LogMessage, <-chan error, context.CancelFunc),
) *logCacheRepository {
return &logCacheRepository{client: client, recentLogsFunc: recentLogsFunc, getStreamingLogsFunc: getStreamingLogsFunc, cancelFunc: func() {}}
}
func (r *logCacheRepository) RecentLogsFor(appGUID string) ([]Loggable, error) {
logs, err := r.recentLogsFunc(appGUID, r.client)
if err != nil {
return nil, err
}
loggables := make([]Loggable, len(logs))
for i, v := range logs {
loggables[i] = NewLogCacheMessage(&terminalColorLogger{}, v)
}
return loggables, nil
}
func (r *logCacheRepository) TailLogsFor(appGUID string, onConnect func(), logChan chan<- Loggable, errChan chan<- error) {
messages, logErrs, stopStreaming := r.getStreamingLogsFunc(appGUID, r.client)
r.cancelFunc = stopStreaming
defer close(logChan)
defer close(errChan)
for {
select {
case message, ok := <-messages:
if !ok {
return
}
logChan <- NewLogCacheMessage(&terminalColorLogger{}, message)
case logErr, ok := <-logErrs:
if !ok {
return
}
errChan <- logErr
}
}
}
func (r *logCacheRepository) Close() {
r.cancelFunc()
}