github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/api/logs/log_cache_repository.go (about)

     1  package logs
     2  
     3  import (
     4  	"context"
     5  
     6  	"code.cloudfoundry.org/cli/actor/sharedaction"
     7  	"code.cloudfoundry.org/cli/cf/terminal"
     8  )
     9  
    10  type terminalColorLogger struct {
    11  }
    12  
    13  func (t terminalColorLogger) LogSysHeaderColor(message string) string {
    14  	return terminal.LogSysHeaderColor(message)
    15  }
    16  
    17  func (t terminalColorLogger) LogStdoutColor(message string) string {
    18  	return terminal.LogStdoutColor(message)
    19  }
    20  
    21  func (t terminalColorLogger) LogStderrColor(message string) string {
    22  	return terminal.LogStderrColor(message)
    23  }
    24  
    25  type logCacheRepository struct {
    26  	client               sharedaction.LogCacheClient
    27  	cancelFunc           context.CancelFunc
    28  	recentLogsFunc       func(appGUID string, client sharedaction.LogCacheClient) ([]sharedaction.LogMessage, error)
    29  	getStreamingLogsFunc func(appGUID string, client sharedaction.LogCacheClient) (<-chan sharedaction.LogMessage, <-chan error, context.CancelFunc)
    30  }
    31  
    32  func NewLogCacheRepository(
    33  	client sharedaction.LogCacheClient,
    34  	recentLogsFunc func(appGUID string, client sharedaction.LogCacheClient) ([]sharedaction.LogMessage, error),
    35  	getStreamingLogsFunc func(appGUID string, client sharedaction.LogCacheClient) (<-chan sharedaction.LogMessage, <-chan error, context.CancelFunc),
    36  ) *logCacheRepository {
    37  	return &logCacheRepository{client: client, recentLogsFunc: recentLogsFunc, getStreamingLogsFunc: getStreamingLogsFunc, cancelFunc: func() {}}
    38  }
    39  
    40  func (r *logCacheRepository) RecentLogsFor(appGUID string) ([]Loggable, error) {
    41  	logs, err := r.recentLogsFunc(appGUID, r.client)
    42  	if err != nil {
    43  		return nil, err
    44  	}
    45  
    46  	loggables := make([]Loggable, len(logs))
    47  	for i, v := range logs {
    48  		loggables[i] = NewLogCacheMessage(&terminalColorLogger{}, v)
    49  	}
    50  
    51  	return loggables, nil
    52  }
    53  
    54  func (r *logCacheRepository) TailLogsFor(appGUID string, onConnect func(), logChan chan<- Loggable, errChan chan<- error) {
    55  	messages, logErrs, stopStreaming := r.getStreamingLogsFunc(appGUID, r.client)
    56  
    57  	r.cancelFunc = stopStreaming
    58  
    59  	defer close(logChan)
    60  	defer close(errChan)
    61  
    62  	for {
    63  		select {
    64  		case message, ok := <-messages:
    65  			if !ok {
    66  				return
    67  			}
    68  			logChan <- NewLogCacheMessage(&terminalColorLogger{}, message)
    69  		case logErr, ok := <-logErrs:
    70  			if !ok {
    71  				return
    72  			}
    73  			errChan <- logErr
    74  		}
    75  	}
    76  }
    77  
    78  func (r *logCacheRepository) Close() {
    79  	r.cancelFunc()
    80  }