github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/util/ui/request_logger_terminal_display.go (about) 1 package ui 2 3 import ( 4 "fmt" 5 "regexp" 6 "sync" 7 "time" 8 9 "github.com/fatih/color" 10 ) 11 12 type RequestLoggerTerminalDisplay struct { 13 ui *UI 14 lock *sync.Mutex 15 dumpSanitizer *regexp.Regexp 16 } 17 18 func newRequestLoggerTerminalDisplay(ui *UI, lock *sync.Mutex) *RequestLoggerTerminalDisplay { 19 return &RequestLoggerTerminalDisplay{ 20 ui: ui, 21 lock: lock, 22 dumpSanitizer: regexp.MustCompile(tokenRegexp), 23 } 24 } 25 26 func (display *RequestLoggerTerminalDisplay) DisplayBody([]byte) error { 27 fmt.Fprintf(display.ui.Out, "%s\n", RedactedValue) 28 return nil 29 } 30 31 func (display *RequestLoggerTerminalDisplay) DisplayDump(dump string) error { 32 sanitized := display.dumpSanitizer.ReplaceAllString(dump, RedactedValue) 33 fmt.Fprintf(display.ui.Out, "%s\n", sanitized) 34 return nil 35 } 36 37 func (display *RequestLoggerTerminalDisplay) DisplayHeader(name string, value string) error { 38 fmt.Fprintf(display.ui.Out, "%s: %s\n", display.ui.TranslateText(name), value) 39 return nil 40 } 41 42 func (display *RequestLoggerTerminalDisplay) DisplayHost(name string) error { 43 fmt.Fprintf(display.ui.Out, "%s: %s\n", display.ui.TranslateText("Host"), name) 44 return nil 45 } 46 47 func (display *RequestLoggerTerminalDisplay) DisplayJSONBody(body []byte) error { 48 if len(body) == 0 { 49 return nil 50 } 51 52 sanitized, err := SanitizeJSON(body) 53 if err != nil { 54 fmt.Fprintf(display.ui.Out, "%s\n", string(body)) 55 return nil 56 } 57 58 fmt.Fprintf(display.ui.Out, "%s\n", string(sanitized)) 59 60 return nil 61 } 62 63 func (display *RequestLoggerTerminalDisplay) DisplayMessage(msg string) error { 64 fmt.Fprintf(display.ui.Out, "%s\n", msg) 65 return nil 66 } 67 68 func (display *RequestLoggerTerminalDisplay) DisplayRequestHeader(method string, uri string, httpProtocol string) error { 69 fmt.Fprintf(display.ui.Out, "%s %s %s\n", method, uri, httpProtocol) 70 return nil 71 } 72 73 func (display *RequestLoggerTerminalDisplay) DisplayResponseHeader(httpProtocol string, status string) error { 74 fmt.Fprintf(display.ui.Out, "%s %s\n", httpProtocol, status) 75 return nil 76 } 77 78 func (display *RequestLoggerTerminalDisplay) DisplayType(name string, requestDate time.Time) error { 79 text := fmt.Sprintf("%s: [%s]", name, requestDate.Format(time.RFC3339)) 80 fmt.Fprintf(display.ui.Out, "%s\n", display.ui.modifyColor(display.ui.TranslateText(text), color.New(color.Bold))) 81 return nil 82 } 83 84 func (display *RequestLoggerTerminalDisplay) HandleInternalError(err error) { 85 fmt.Fprintf(display.ui.Err, "%s\n", display.ui.TranslateText(err.Error())) 86 } 87 88 func (display *RequestLoggerTerminalDisplay) Start() error { 89 display.lock.Lock() 90 return nil 91 } 92 93 func (display *RequestLoggerTerminalDisplay) Stop() error { 94 fmt.Fprintf(display.ui.Out, "\n") 95 display.lock.Unlock() 96 return nil 97 } 98 99 // RequestLoggerTerminalDisplay returns a RequestLoggerTerminalDisplay that 100 // cannot overwrite another RequestLoggerTerminalDisplay or the current 101 // display. 102 func (ui *UI) RequestLoggerTerminalDisplay() *RequestLoggerTerminalDisplay { 103 return newRequestLoggerTerminalDisplay(ui, ui.terminalLock) 104 }