code.cloudfoundry.org/cli@v7.1.0+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  	cookieCutter := regexp.MustCompile("Set-Cookie:.*")
    34  	sanitized = cookieCutter.ReplaceAllString(sanitized, "Set-Cookie: "+RedactedValue)
    35  	fmt.Fprintf(display.ui.Out, "%s\n", sanitized)
    36  	return nil
    37  }
    38  
    39  func (display *RequestLoggerTerminalDisplay) DisplayHeader(name string, value string) error {
    40  	fmt.Fprintf(display.ui.Out, "%s: %s\n", display.ui.TranslateText(name), value)
    41  	return nil
    42  }
    43  
    44  func (display *RequestLoggerTerminalDisplay) DisplayHost(name string) error {
    45  	fmt.Fprintf(display.ui.Out, "%s: %s\n", display.ui.TranslateText("Host"), name)
    46  	return nil
    47  }
    48  
    49  func (display *RequestLoggerTerminalDisplay) DisplayJSONBody(body []byte) error {
    50  	if len(body) == 0 {
    51  		return nil
    52  	}
    53  
    54  	sanitized, err := SanitizeJSON(body)
    55  	if err != nil {
    56  		fmt.Fprintf(display.ui.Out, "%s\n", string(body))
    57  		return nil
    58  	}
    59  
    60  	fmt.Fprintf(display.ui.Out, "%s\n", string(sanitized))
    61  
    62  	return nil
    63  }
    64  
    65  func (display *RequestLoggerTerminalDisplay) DisplayMessage(msg string) error {
    66  	fmt.Fprintf(display.ui.Out, "%s\n", msg)
    67  	return nil
    68  }
    69  
    70  func (display *RequestLoggerTerminalDisplay) DisplayRequestHeader(method string, uri string, httpProtocol string) error {
    71  	fmt.Fprintf(display.ui.Out, "%s %s %s\n", method, uri, httpProtocol)
    72  	return nil
    73  }
    74  
    75  func (display *RequestLoggerTerminalDisplay) DisplayResponseHeader(httpProtocol string, status string) error {
    76  	fmt.Fprintf(display.ui.Out, "%s %s\n", httpProtocol, status)
    77  	return nil
    78  }
    79  
    80  func (display *RequestLoggerTerminalDisplay) DisplayType(name string, requestDate time.Time) error {
    81  	text := fmt.Sprintf("%s: [%s]", name, requestDate.Format(time.RFC3339))
    82  	fmt.Fprintf(display.ui.Out, "%s\n", display.ui.modifyColor(display.ui.TranslateText(text), color.New(color.Bold)))
    83  	return nil
    84  }
    85  
    86  func (display *RequestLoggerTerminalDisplay) HandleInternalError(err error) {
    87  	fmt.Fprintf(display.ui.Err, "%s\n", display.ui.TranslateText(err.Error()))
    88  }
    89  
    90  func (display *RequestLoggerTerminalDisplay) Start() error {
    91  	display.lock.Lock()
    92  	return nil
    93  }
    94  
    95  func (display *RequestLoggerTerminalDisplay) Stop() error {
    96  	fmt.Fprintf(display.ui.Out, "\n")
    97  	display.lock.Unlock()
    98  	return nil
    99  }
   100  
   101  // RequestLoggerTerminalDisplay returns a RequestLoggerTerminalDisplay that
   102  // cannot overwrite another RequestLoggerTerminalDisplay or the current
   103  // display.
   104  func (ui *UI) RequestLoggerTerminalDisplay() *RequestLoggerTerminalDisplay {
   105  	return newRequestLoggerTerminalDisplay(ui, ui.terminalLock)
   106  }